整理一个zip的压缩/解压工具类

压缩

自定义添加文件打包

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtil {

    /**
     * 自定义添加文件生成zip包,zip包中没有目录结构
     * <p>
     * 用法:ZipUtil.zipFiles("/Users/hh/git/job/circle/tmp.zip",
     * "/Users/hh/git/job/circle/tmp/aa.txt",
     * "/Users/hh/Downloads/test_2.jpg",
     * "/Users/hh/git/job/circle/tmp/bb.txt");
     *
     * @param zipFileName 全路径的zip文件包的名字
     * @param files       要添加到zip包中的文件的全路径
     */
    public static void zipFiles(String zipFileName, String... files) {
        try {
            byte[] buffer = new byte[1024];
            File zipFile = new File(zipFileName);
            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
            for (String file : files) {
                File file1 = new File(file);
                String fileName = file1.getName();
                ZipEntry zipEntry = new ZipEntry(fileName);
                zos.putNextEntry(zipEntry);
                FileInputStream fis = new FileInputStream(file1);
                int len;
                while ((len = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
                fis.close();
            }
            zos.closeEntry();
            zos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

对文件夹打zip包, 被压缩的文件夹里可以有多层文件夹

链接文原: https://tomoya92.github.io/2019/09/03/java-zip/

/**
  * 压缩文件夹
  *
  * @param zipFileName 全路径的zip文件包的名字
  * @param dir         要压缩的文件夹的路径
  */
public static void zipDir(String zipFileName, String dir) {
    try {
        File zipFile = new File(zipFileName);
        FileOutputStream fos = new FileOutputStream(zipFile);
        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
        File dirFile = new File(dir);
        addEntry(dirFile, zos, "");
        zos.closeEntry();
        zos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static void addEntry(File file, ZipOutputStream zos, String root) throws IOException {
    byte[] buffer = new byte[1024];
    for (File file1 : file.listFiles()) {
        System.out.println(file1.getName());
        if (file1.isDirectory()) {
            addEntry(file1, zos, file1.getName());
        } else {
            String fileName = file1.getName();
            ZipEntry zipEntry = new ZipEntry(root + "/" + fileName);
            zos.putNextEntry(zipEntry);
            FileInputStream fis = new FileInputStream(file1);
            int len;
            while ((len = fis.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }
            fis.close();
        }
    }
}

解压

public static void unzip(String zipFilePath, String destDir) {
    File dir = new File(destDir);
    // create output directory if it doesn't exist
    if (!dir.exists()) dir.mkdirs();
    FileInputStream fis;
    //buffer for read and write data to file
    byte[] buffer = new byte[1024];
    try {
        fis = new FileInputStream(zipFilePath);
        ZipInputStream zis = new ZipInputStream(fis);
        ZipEntry ze = zis.getNextEntry();
        while (ze != null) {
            String fileName = ze.getName();
            File newFile = new File(destDir + File.separator + fileName);
            System.out.println("Unzipping to " + newFile.getAbsolutePath());
            //create directories for sub directories in zip
            new File(newFile.getParent()).mkdirs();
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            //close this ZipEntry
            zis.closeEntry();
            ze = zis.getNextEntry();
        }
        //close last ZipEntry
        zis.closeEntry();
        zis.close();
        fis.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
原文链接: https://tomoya92.github.io/2019/09/03/java-zip/