java zip压缩/解压工具类 ZipUtil

2023-05-16

public class ZipUtil {

    public static void main(String[] args) throws Exception {
        // String[] ss = new String[2];
        // ss[0] = "D:\\adManageUpload\\excel\\1552116277671_.xlsx";
        // ss[1] = "D:\\adManageUpload\\excel\\1552120508265_.xlsx";
        // zip("C:/Users/Administrator/Desktop/111.zip", ss);
        // zip("C:\\Users\\Administrator\\Desktop\\temp.zip", "C:\\Users\\Administrator\\Desktop\\temp");
    }

    /**
     * @param zipName
     *            输出的压缩文件名称
     * @param pathList
     *            文件路径列表
     */
    public static void zip(String zipName, String[] pathList) {
        ZipOutputStream out = null;
        BufferedOutputStream bos = null;
        try {
            File file = new File(zipName);
            out = new ZipOutputStream(new FileOutputStream(zipName), StandardCharsets.UTF_8);// 创建zip输出流
            bos = new BufferedOutputStream(out);// 创建缓冲输出流
            compress(out, bos, file.getName(), pathList);// 调用函数
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bos)
                    bos.close();
                if (null != out)
                    out.close();
            } catch (IOException e) {
            }
        }
    }

    /**
     * @param out
     *            输出流
     * @param bos
     *            缓冲流
     * @param sourceFile
     *            压缩源文件
     * @param base
     *            当前被压缩文件的文件名,形成压缩文件时候的目录层级
     * @throws Exception
     */
    private static void compress(ZipOutputStream out, BufferedOutputStream bos, String zipName, String[] pathList) throws Exception {
        out.putNextEntry(new ZipEntry(zipName + File.separator));
        Set<String> set = new HashSet<String>();
        for (String string : pathList) {
            if (set.contains(string)) {
                continue;
            }
            File f = new File(string);
            out.putNextEntry(new ZipEntry(zipName + File.separator + f.getName()));
            FileInputStream fos = new FileInputStream(f);
            byte[] bs = new byte[1024];
            // 将源文件写入到zip文件中
            while (fos.read(bs) != -1) {
                bos.write(bs);
                bos.flush();
            }
            fos.close();
            set.add(string);
        }
    }

    /**
     * @param zipName
     *            输出的压缩文件名称
     * @param sourceName
     *            输入的文件或者文件夹
     */
    public static void zip(String zipName, String sourceName) {
        ZipOutputStream out = null;
        BufferedOutputStream bos = null;
        try {
            out = new ZipOutputStream(new FileOutputStream(zipName), StandardCharsets.UTF_8);// 创建zip输出流
            bos = new BufferedOutputStream(out);// 创建缓冲输出流
            File sourceFile = new File(sourceName);
            compress(out, bos, sourceFile, sourceFile.getName());// 调用函数
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bos)
                    bos.close();
                if (null != out)
                    out.close();
            } catch (IOException e) {
            }
        }
    }

    /**
     * @param out
     *            输出流
     * @param bos
     *            缓冲流
     * @param sourceFile
     *            压缩源文件
     * @param base
     *            当前被压缩文件的文件名,形成压缩文件时候的目录层级
     * @throws Exception
     */
    private static void compress(ZipOutputStream out, BufferedOutputStream bos, File sourceFile, String base) throws Exception {
        // 如果路径为目录(文件夹)
        if (sourceFile.isDirectory()) {
            // 取出文件夹中的文件(或子文件夹)
            File[] flist = sourceFile.listFiles();
            if (flist.length == 0) {// 如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
                out.putNextEntry(new ZipEntry(base + File.separator));
            } else {// 如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
                for (int i = 0; i < flist.length; i++) {
                    compress(out, bos, flist[i], base + File.separator + flist[i].getName());
                }
            }
        } else {// 如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
            out.putNextEntry(new ZipEntry(base));
            FileInputStream fos = new FileInputStream(sourceFile);
            byte[] bs = new byte[1024];
            // 将源文件写入到zip文件中
            while (fos.read(bs) != -1) {
                bos.write(bs);
                bos.flush();
            }
            fos.close();
        }
    }

    // /***
    // * 压缩GZip
    // *
    // * @param data
    // * @return
    // */
    // public static byte[] gZip(byte[] data) {
    // byte[] b = null;
    // try {
    // ByteArrayOutputStream bos = new ByteArrayOutputStream();
    // GZIPOutputStream gzip = new GZIPOutputStream(bos);
    // gzip.write(data);
    // gzip.finish();
    // gzip.close();
    // b = bos.toByteArray();
    // bos.close();
    // } catch (Exception ex) {
    // ex.printStackTrace();
    // }
    // return b;
    // }
    //
    // /***
    // * 解压GZip
    // *
    // * @param data
    // * @return
    // */
    // public static byte[] unGZip(byte[] data) {
    // byte[] b = null;
    // try {
    // ByteArrayInputStream bis = new ByteArrayInputStream(data);
    // GZIPInputStream gzip = new GZIPInputStream(bis);
    // byte[] buf = new byte[1024];
    // int num = -1;
    // ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // while ((num = gzip.read(buf, 0, buf.length)) != -1) {
    // baos.write(buf, 0, num);
    // }
    // b = baos.toByteArray();
    // baos.flush();
    // baos.close();
    // gzip.close();
    // bis.close();
    // } catch (Exception ex) {
    // ex.printStackTrace();
    // }
    // return b;
    // }
    //
    // /***
    // * 解压GZip
    // *
    // * @param data
    // * @return
    // */
    // public static byte[] unZip(byte[] data) {
    // byte[] b = null;
    // try {
    // ByteArrayInputStream bis = new ByteArrayInputStream(data);
    // ZipInputStream gzip = new ZipInputStream(bis);
    // byte[] buf = new byte[data.length];
    // int num = -1;
    // ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // while ((num = gzip.read(buf, 0, buf.length)) != -1) {
    // baos.write(buf, 0, num);
    // }
    // b = baos.toByteArray();
    // baos.flush();
    // baos.close();
    // gzip.close();
    // bis.close();
    // } catch (Exception ex) {
    // ex.printStackTrace();
    // }
    // return b;
    // }
}

package com.xj.hhjk.common.util;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java zip压缩/解压工具类 ZipUtil 的相关文章

随机推荐

  • BACnet协议简要说明及组网简介

    主题概要BACnet协议BACnet协议简要说明 xff0c 组网简介编辑时间新建20160217序号参考资料1BACnet协议正文1995版2http www bacnet org Tutorial BACnetIP 1 协议说明 BAC
  • Tesseract-OCR字符训练工具及方法

    主题概要Tesseract OCRTesseract OCR训练工具编辑时间新建20161008更正训练步骤序号20161225序号参考资料1http vietocr sourceforge net training html2 最近参加了
  • 基于easyPR和openalpr的车牌识别研究

    主题概要车牌识别车牌识别流程及算法编辑时间新建20161216序号参考资料1https github com openalpr openalpr2https github com liuruoze EasyPR3学习openCV Cary
  • c# Winform程序实现多sheet的Excel文件导入与导出

    主题概要C excel导入到mysql xff0c mysql导出到excel编辑时间新建20170123序号参考资料1http download csdn net detail nanzhaonan 5403457 xff08 左侧导航菜
  • JS统计页面访问次数并传递可选参数

    主题概要JS用JS统计网站页面的访问次数 xff0c 并传递可选参数编辑时间新建20171218序号参考资料1 网站有些页面需要统计用户的访问次数 xff0c 下面是一个简单的JS xff0c 引用在需要统计的页面类 网站上有个Handle
  • QT UDP Socket数据接收与解析

    主题概要QTUDP Socket编辑时间新建20180331序号参考资料1https doc qt io qt 5 qudpsocket html 做直升机航电系统仿真 xff0c 类似GPS导航接收机的按钮很多 xff0c 显示的仪表也很
  • 个人博客搬迁

    个人博客网址 xff1a 个人博客 可以在浏览器上输入 shao zheng com 后续不再在CSDN上更新博客 忽略新博客的页面 xff0c 会用心记录开发中的各种内容 xff0c 也会写一些飞行仿真相关的原理性的东西
  • 在sublime中配置gcc/g++环境

    在sublime中配置gcc g 43 43 环境 第一次使用sublime进行c 43 43 学习的过程中遇到的一些问题 xff0c 包括代码的编译与运行需要用到gcc g 43 43 的编译环境 xff0c 现在已经顺利解决 本文是对问
  • Android-调用微信支付失败的情况

    最近工作中 xff0c 开发的App版本需要加入微信支付 支付宝支付 QQ钱包支付功能 一般情况下 xff0c 我们支付功能都是封装好的支付模块 xff0c 支付方面一般不会有重大的更新 但是我却遇到了 xff1a 微信支付一直调不起微信客
  • Rust异步编程async/.await原理解析(一)

    在这个教程中我们将详细分析rust异步代码async await的内部运行机制 我们将使用async std库而不是tokio xff0c 因为这是第一个支持async await语法的rust库 async await原理解析教程分为两部
  • 数控机床G-Code在线仿真器

    G code是 CNC xff08 计算机数控 xff09 机床的编程语言 G code指的是Geometric Code xff0c 即几何代码 我们使用这种语言告诉机器做什么或怎么做某事 G code命令指示机器移动位置 移动速度以及要
  • ubuntu误删文件造成软件包信息列表损坏无法更新或安装文件

    因为一开始不知到如何彻底卸载软件包 xff0c 误把文件目录删除 xff0c 造成了软件包信息列表损坏 xff0c 系统无法使用apt get 命令 xff0c 无论实施更新或安装还是卸载都会提示软件包信息列表损坏 xff0c 建议重新安装
  • Docker镜像转换成Dockerfile

    通常我们会通过dockerfile构建docker xff0c 但是我们需要使用dockerfile时 xff0c 想还原其dockerfile xff0c 我们可以使用如下命令 docker run v var run docker so
  • 使用iperf测试网络速度--windows

    下载 下载iperf的Windows版本 xff1a iperf官网地址 这里下载的版本是iperf 3 1 3 win32 xff0c 电脑使用的是XP系统 下载文件中有cygwin1 dll xff0c cyggcc s 1 dll x
  • [最新]ubuntu22.04安装kubernetes1.25 k8s1.25

    条件 2G内存2 CPU网络连接 xff08 内网公网都可以 xff09 唯一的主机名 xff0c mac地址6443 xff0c 10650等端口禁用Swap20 GB var目录磁盘空间Sudo 用户权限 主机准备 Master Nod
  • hadoop目录介绍(各目录功能介绍)

    hadoop目录 1 bin xff1a 2 sbin3 etc4 lib5 logs6 include7 libexec8 share目录 介绍hadoop的目录 首先进入hadoop查看目录 挑重要的介绍 1 bin xff1a bin
  • Linux操作文件的系统调用

    操作文件的系统调用 1 文件操作有关的系统调用 open xff08 xff09 read xff08 xff09 write xff08 xff09 close xff08 xff09 1 xff09 open xff08 xff09 i
  • MySQL 分组查询

    文章目录 分组查询分组函数1 功能2 分类3 特点3 简单的使用4 和distinct搭配使用5 count函数的具体介绍6 和分组函数一同查询的字段有要求 分组查询1 语法2 简单的分组查询3 添加筛选条件3 1添加分组前的筛选3 2添加
  • Android-推荐一个视频播放库JZVideo

    最近项目当中有用到视频播放器这个功能 xff0c 分享一个我的开发心得 一般情况下 xff0c 我们不会去写一个视频播放器 xff0c 因为需要很多知识和播放场景考虑 分享一个我使用到的视频播放器的库 xff0c JZVideo 俗称饺子播
  • java zip压缩/解压工具类 ZipUtil

    public class ZipUtil public static void main String args throws Exception String ss 61 new String 2 ss 0 61 34 D adManag