java zip压缩/解压工具类 ZipUtil

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;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

长青风

赏一块,发大财!赏两块,惹人爱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值