解決Java原生壓縮組件不支持中文文件名亂碼的問(wèn)題
最近發(fā)現(xiàn)Java原生的Zip壓縮組件在壓縮過(guò)程中,不支持文件名的中文編碼,會(huì)在壓縮過(guò)程中把中文文件名變成亂碼。Apache的ant包中的壓縮組件修復(fù)了這個(gè)問(wèn)題,如果你在使用壓縮功能時(shí)需要支持中文文件名,建議你直接使用Apache的壓縮組件來(lái)實(shí)現(xiàn)這個(gè)功能。
具體使用方法:
1.在你的pom文件中增加對(duì)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.壓縮的方法實(shí)現(xiàn),大家注意,本組件支持設(shè)置編碼(setEncoding("GBK")方法),解決了中文編碼的問(wèn)題:
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); } } /** 壓縮一個(gè)目錄 */ 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() + "/"); } } /** 壓縮一個(gè)文件 */ 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.解壓縮的實(shí)現(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)過(guò)測(cè)試,可以直接使用。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis查詢返回Map<String,Object>類型實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于Mybatis查詢返回Map<String,Object>類型的相關(guān)資料,平時(shí)沒(méi)太注意怎么用,今天又遇到了總結(jié)記錄一下,方便以后處理此類問(wèn)題,需要的朋友可以參考下2022-07-07Java自動(dòng)化測(cè)試中多數(shù)據(jù)源的切換(實(shí)例講解)
下面小編就為大家?guī)?lái)一篇Java自動(dòng)化測(cè)試中多數(shù)據(jù)源的切換(實(shí)例講解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10Java數(shù)據(jù)類型轉(zhuǎn)換的示例詳解
Java程序中要求參與的計(jì)算的數(shù)據(jù),必須要保證數(shù)據(jù)類型的一致性,如果數(shù)據(jù)類型不一致將發(fā)生類型的轉(zhuǎn)換。本文將通過(guò)示例詳細(xì)說(shuō)說(shuō)Java中數(shù)據(jù)類型的轉(zhuǎn)換,感興趣的可以了解一下2022-10-10SpringBoot整合ip2region獲取客戶端IP地理位置信息
在我們?nèi)粘EB開(kāi)發(fā)工作中,經(jīng)常會(huì)有需要獲取客戶端地理位置的需求,本文主要介紹了SpringBoot整合ip2region獲取客戶端IP地理位置信息,具有一定的參考價(jià)值,感興趣的可以了解一下2024-08-08java Date獲取年月日時(shí)分秒的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇java Date獲取年月日時(shí)分秒的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06Java IO創(chuàng)建目錄和文件實(shí)例代碼
本篇文章給大家分享了Java IO創(chuàng)建目錄和文件的實(shí)例代碼,過(guò)程很簡(jiǎn)單,大家可以測(cè)試參考下。2018-02-02SpringBoot使用Redisson實(shí)現(xiàn)延遲執(zhí)行的完整示例
這篇文章主要介紹了SpringBoot使用Redisson實(shí)現(xiàn)延遲執(zhí)行的完整示例,文中通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-06-06