解決Java原生壓縮組件不支持中文文件名的問題

最近發(fā)現(xiàn)Java原生的Zip壓縮組件在壓縮過程中,不支持文件名的中文編碼,會在壓縮過程中把中文文件名變成亂碼。Apache的ant包中的壓縮組件修復(fù)了這個問題,如果你在使用壓縮功能時需要支持中文文件名,建議你直接使用Apache的壓縮組件來實現(xiàn)這個功能。

具體使用方法:

1.在你的pom文件中增加對Apache的ant工具包的dependency:

        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.9.3</version>
        </dependency>

并在頭部引用用到的類:

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

2.壓縮的方法實現(xiàn),大家注意,本組件支持設(shè)置編碼(setEncoding("GBK")方法),解決了中文編碼的問題:

public static void compress(String srcPath , String dstPath) throws IOException{
        File srcFile = new File(srcPath);
        File dstFile = new File(dstPath);
        if (!srcFile.exists()) {
            throw new FileNotFoundException(srcPath + "does not exists");
        }

        FileOutputStream out = null;
        ZipOutputStream zipOut = null;
        try {
            out = new FileOutputStream(dstFile);
            zipOut = new ZipOutputStream(new BufferedOutputStream(out));
            zipOut.setEncoding("GBK");
            String baseDir = "";
            compress(srcFile, zipOut, baseDir);
        }
        catch (Throwable ex){
            throw new RuntimeException(ex);
        }
        finally {
            if(null != zipOut){
                zipOut.close();
                out = null;
            }

            if(null != out){
                out.close();
            }
        }
    }


private static void compress(File file, ZipOutputStream zipOut, String baseDir) throws IOException{
        if (file.isDirectory()) {
            compressDirectory(file, zipOut, baseDir);
        } else {
            compressFile(file, zipOut, baseDir);
        }
    }

    /** 壓縮一個目錄 */
    private static void compressDirectory(File dir, ZipOutputStream zipOut, String baseDir) throws IOException{
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            compress(files[i], zipOut, baseDir + dir.getName() + "/");
        }
    }

    /** 壓縮一個文件 */
    private static void compressFile(File file, ZipOutputStream zipOut, String baseDir)  throws IOException{
        if (!file.exists()){
            return;
        }

        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(baseDir + file.getName());
            zipOut.putNextEntry(entry);
            int count;
            byte data[] = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                zipOut.write(data, 0, count);
            }

        }finally {
            if(null != bis){
                bis.close();
            }
        }
    }

3.解壓縮的實現(xiàn):

public static void decompress(String zipFile , String dstPath)throws IOException{
        File pathFile = new File(dstPath);
        if(!pathFile.exists()){
            pathFile.mkdirs();
        }
        ZipFile zip = new ZipFile(zipFile);
        for(Enumeration entries = zip.getEntries();entries.hasMoreElements();){
            ZipEntry entry = (ZipEntry)entries.nextElement();
            String zipEntryName = entry.getName();
            InputStream in = null;
            OutputStream out = null;
            try{
                in =  zip.getInputStream(entry);
                String outPath = (dstPath+"/"+zipEntryName).replaceAll("\\*", "/");;
                //判斷路徑是否存在,不存在則創(chuàng)建文件路徑
                File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
                if(!file.exists()){
                    file.mkdirs();
                }
                //判斷文件全路徑是否為文件夾,如果是上面已經(jīng)上傳,不需要解壓
                if(new File(outPath).isDirectory()){
                    continue;
                }

                out = new FileOutputStream(outPath);
                byte[] buf1 = new byte[1024];
                int len;
                while((len=in.read(buf1))>0){
                    out.write(buf1,0,len);
                }
            }
            finally {
                if(null != in){
                    in.close();
                }

                if(null != out){
                    out.close();
                }
            }
        }
    }

以上代碼經(jīng)過測試,可以直接使用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,048評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,568評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • 三月霧濃蝶戲,五更天曉鶯鳴。陌上黃牛辭倦客,檐下金童聚摯朋。春聲靜靜聽。 柳影無心亂舞,花香有意常馨。弦顫應(yīng)知稀客...
    深藍(lán)色木魚閱讀 357評論 0 5
  • 你是我的美麗的簕杜鵑 百花開的時候,你開; 百花敗的時候,你不敗。 只要有了適合你的溫度, 只要有了適合你的濕度 ...
    白咖啡鐘文萍閱讀 358評論 0 1

友情鏈接更多精彩內(nèi)容