首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 實作 Zip 壓縮解壓工具類(支援中文檔名)

     編輯:Java 相關知識

設計目標

參考 WinRAR/WinZip 行為,實作單一工具類支援:

  • 混合壓縮多個檔案與目錄(遞迴處理子目錄)
  • 解壓縮至指定目錄
  • 支援中文檔名(需改用 Apache Ant 的 Zip 類別)

JDK 內建 java.util.zip 僅支援 ZIP/GZIP,且中文編碼有缺陷;GZIP 單檔案特性不適合打包場景。

核心實作 (ZipUtils.java)

package util;

import java.io.*;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.*;
// import org.apache.tools.zip.ZipEntry;      // 支援中文請改用這兩行
// import org.apache.tools.zip.ZipOutputStream;

public class ZipUtils {
    private static final int BUFF_SIZE = 1024 * 1024; // 1MB 緩衝區

    /** 批次壓縮檔案/目錄 */
    public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
        zipFiles(resFileList, zipFile, "");
    }

    /** 批次壓縮並加入註解 */
    public static void zipFiles(Collection<File> resFileList, File zipFile, String comment) throws IOException {
        ZipOutputStream zipout = new ZipOutputStream(
            new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
        for (File resFile : resFileList) {
            zipFile(resFile, zipout, "");
        }
        zipout.setComment(comment);
        zipout.close();
    }

    /** 解壓縮單一 ZIP 檔 */
    public static void upZipFile(File zipFile, String folderPath) throws IOException {
        ZipFile zf = new ZipFile(zipFile);
        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            InputStream in = zf.getInputStream(entry);
            File outFile = new File(folderPath + File.separator + entry.getName());
            if (entry.isDirectory()) {
                outFile.mkdirs();
            } else {
                outFile.getParentFile().mkdirs();
                OutputStream out = new FileOutputStream(outFile);
                byte[] buffer = new byte[BUFF_SIZE];
                int realLength;
                while ((realLength = in.read(buffer)) > 0) {
                    out.write(buffer, 0, realLength);
                }
                in.close();
                out.close();
            }
        }
    }

    /** 遞迴處理單一檔案/目錄 */
    private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws IOException {
        rootpath = rootpath + (rootpath.trim().isEmpty() ? "" : File.separator) + resFile.getName();
        if (resFile.isDirectory()) {
            for (File file : resFile.listFiles()) {
                zipFile(file, zipout, rootpath);
            }
        } else {
            byte[] buffer = new byte[BUFF_SIZE];
            BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE);
            zipout.putNextEntry(new ZipEntry(rootpath));
            int realLength;
            while ((realLength = in.read(buffer)) != -1) {
                zipout.write(buffer, 0, realLength);
            }
            in.close();
            zipout.flush();
            zipout.closeEntry();
        }
    }
}

測試程式碼

public class Test {
    public static void main(String[] args) throws IOException {
        Collection<File> list = new ArrayList<>();
        list.add(new File("C:\\new.gif"));
        list.add(new File("C:\\HelloWorld.java"));
        list.add(new File("C:\\crebas.sql"));
        list.add(new File("E:\\log.log"));
        list.add(new File("C:\\ooo\\upx\\"));  // 目錄
        File zip = new File("C:\\txxxt.zip");
        ZipUtils.zipFiles(list, zip);
        System.out.println("壓縮成功!");
    }
}

中文檔名問題與解決

JDK 內建 ZIP 類別預設使用 UTF-8 編碼項目名稱,但在 Windows 中文環境常出現亂碼。解決方案:

  1. 引入 ant.jar (Apache Ant 庫)
  2. 將 import java.util.zip.ZipEntry;、ZipOutputStream 改為 org.apache.tools.zip.ZipEntry、ZipOutputStream
  3. 其餘程式碼無需修改,即可完美支援中文檔名。
實測:替換後壓縮/解壓中文檔名皆正常,無亂碼。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved