对ZIP文件的解压缩工具(支持中文压缩)

公司分给了我一个难搞的需求,需要处理ZIP的相关操作,在处理需求时遇到各种问题,大家仅供参考,可以避坑,遇到其他问题可以评论区留言

1.解压ZIP压缩文件

/**
     *
     * @autor wangsz
     * @param zipFilePath   zip文件路径
     * @param destDirectory   zip文件解压到哪里
     * @param charset  指定字符编码
     * @return
     * @throws IOException
     */
    public static boolean unzip(String zipFilePath, String destDirectory, Charset charset) throws IOException {
        File destDir = new File(destDirectory);
        if (!destDir.exists()) {
            destDir.mkdirs();
        }

        try (ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath), charset)) {
            ZipEntry entry = zipIn.getNextEntry();
            while (entry != null) {
                String filePath = destDirectory + File.separator + entry.getName();
                if (!entry.isDirectory()) {
                    extractFile(zipIn, filePath);
                } else {
                    File dir = new File(filePath);
                    dir.mkdirs();
                }
                zipIn.closeEntry();
                entry = zipIn.getNextEntry();
            }
        }
        return true;
    }


    private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
        byte[] bytesIn = new byte[BUFFER_SIZE];
        int read;
        while ((read = zipIn.read(bytesIn)) != -1) {
            bos.write(bytesIn, 0, read);
        }
        bos.close();
    }

2.压缩zip文件

 /**
     * 压缩文件夹到zip
     * @param sourceDirectory   文件夹路径
     * @param zipFilePath   zip的位置
     * @param charset   指定字符编码
     * @throws IOException
     */
    public static void zip(String sourceDirectory, String zipFilePath, Charset charset) throws IOException {
        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath), charset)) {
            File fileToZip = new File(sourceDirectory);
            zipFile(fileToZip, fileToZip.getName(), zipOut);
        }
    }

   
    private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
        if (fileToZip.isHidden()) {
            return;
        }
        if (fileToZip.isDirectory()) {
            if (fileName.endsWith("/")) {
                zipOut.putNextEntry(new ZipEntry(fileName));
                zipOut.closeEntry();
            } else {
                zipOut.putNextEntry(new ZipEntry(fileName + "/"));
                zipOut.closeEntry();
            }
            File[] children = fileToZip.listFiles();
            for (File childFile : children) {
                zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
            }
            return;
        }

        FileInputStream fis = new FileInputStream(fileToZip);
        ZipEntry zipEntry = new ZipEntry(fileName);
        zipOut.putNextEntry(zipEntry);
        byte[] bytes = new byte[BUFFER_SIZE];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zipOut.write(bytes, 0, length);
        }
        fis.close();
    }

3. 解压包含中文的压缩包,需将字符编码指定成GBK,实测utf-8不好用

 Charset charset = Charset.forName("GBK"); // 使用指定的字符编码(例如GBK)

4.压缩文件为zip后,写入输出流供前端下载

    /**
     * 压缩文件夹到zip
     * @param sourceDirectory   文件夹路径
     * @param charset   指定字符编码
     * @throws IOException
     */
    public static void zip(String sourceDirectory,  Charset charset, HttpServletResponse response) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try (ZipOutputStream zipOut = new ZipOutputStream(baos, charset)) {
            File fileToZip = new File(sourceDirectory);
            zipFile(fileToZip, fileToZip.getName(), zipOut);
        }

        response.setContentType("application/zip");
        response.setHeader("Content-Disposition", "attachment; filename=\"compressed.zip\"");

        try (OutputStream os = response.getOutputStream()) {
            os.write(baos.toByteArray());
            os.flush();
        }
    }

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Amo@骄纵

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值