Java操作压缩与解压缩

861 阅读2分钟

打包多个文件(夹)

    /**
     * 打包多个文件或文件夹
     *
     * @author 曲元涛
     * @date 2020/12/20 下午3:44
     */
    @Test
    public void test6() throws IOException {
        zipCompress("/Users/quyuantao/Documents/资料,/Users/quyuantao/Documents/项目", 
                "/Users/quyuantao/Desktop/资料汇总.zip");
    }

    public static void zipCompress(String input, String output) throws IOException {
        // 创建zip输出流
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output));
        // 创建缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(zos);
        String[] paths = input.split(",");
        for (String path : paths) {
            File file = new File(path);
            compress(zos, bos, file, null);
        }
        bos.close();
    }

    /**
     * 压缩文件(夹)
     *
     * @author 曲元涛
     * @date 2020/12/22 上午1:46
     * @param zos   压缩输出流
     * @param bos   输出缓冲流
     * @param input 待压缩的文件(夹)
     * @param name  指定的压缩后文件名
     */
    public static void compress(ZipOutputStream zos, 
                                BufferedOutputStream bos, 
                                File input, 
                                String name) throws IOException {
        if (name == null) {
            // 如果未指定压缩包内的文件(夹)名称,默认使用文件(夹)名
            name = input.getName();
        }
        if (input.isDirectory()) {
            // 当前读取到的是文件夹,获取文件夹下的文件
            File[] listFiles = input.listFiles();
            if (listFiles == null || listFiles.length == 0) {
                // 空文件夹
                zos.putNextEntry(new ZipEntry(name + File.separator));
            } else {
                // 递归写入当前文件夹下的文件
                for (File file : listFiles) {
                    compress(zos, bos, file, name + File.separator + file.getName());
                }
            }
        } else {
            // 当前读取到的是文件,读取文件输入流,写入输出流
            zos.putNextEntry(new ZipEntry(name));
            FileInputStream fis = new FileInputStream(input);
            BufferedInputStream bis = new BufferedInputStream(fis);

            byte[] bytes = new byte[1024];
            int len;
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            bis.close();
            // 写入完当前文件,立刻flush!! 否则会出现文件内容错位,刷新到到其他文件的bug。
            bos.flush();
        }
    }

解压缩

    /**
     * Zip解压缩包
     *
     * @author 曲元涛
     * @date 2020/12/20 下午5:03
     */
    @Test
    public void test7() throws IOException {
        zipUnCompress("/Users/quyuantao/Desktop/资料汇总.zip", "/Users/quyuantao/Desktop/资料汇总");
    }

    /**
     * 解压缩文件(夹)
     *
     * @author 曲元涛
     * @date 2020/12/22 上午1:48
     * @param inputZip      压缩包路径
     * @param destFilePath  解压后保存地址
     */
    public static void zipUnCompress(String inputZip, String destFilePath) throws IOException {
        File srcZip = new File(inputZip);
        if (!srcZip.exists()) {
            throw new FileNotFoundException("未找到压缩包 " + inputZip);
        }
        try (ZipInputStream zis = new ZipInputStream(new FileInputStream(srcZip))) {
            ZipEntry zipEntry;
            while ((zipEntry = zis.getNextEntry()) != null) {
                if (!zipEntry.isDirectory()) {
                    File file = new File(destFilePath, zipEntry.getName());
                    if (!file.exists()) {
                        // 创建此文件的上级目录
                        boolean parDir = new File(file.getParent()).mkdirs();
                    }
                    try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
                        byte[] bytes = new byte[1024];
                        int len;
                        while ((len = zis.read(bytes)) != -1) {
                            bos.write(bytes, 0, len);
                        }
                    }
                }
            }
        }
    }