一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 解決Java原生壓縮組件不支持中文文件名亂碼的問題

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

2020-08-23 15:03楚驤 Java教程

本篇文章主要介紹了解決Java原生壓縮組件不支持中文文件名亂碼的問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。

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

具體使用方法:

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

?
1
2
3
4
5
<dependency>
  <groupId>org.apache.ant</groupId>
  <artifactId>ant</artifactId>
  <version>1.9.3</version>
</dependency>

并在頭部引用用到的類:

?
1
2
3
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")方法),解決了中文編碼的問題:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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):

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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)過測(cè)試,可以直接使用。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.jianshu.com/p/36f87ab9a55b

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 激情视频激情小说 | 99热资源 | 黑人好大好硬好深好爽想要h | b站免费 | 香蕉久久高清国产精品免费 | 日韩精品欧美高清区 | 国产盗摄wc厕所撒尿视频 | 男女男精品网站 | 久久精品午夜一区二区福利 | 婷婷国产在线 | 欧美精品亚洲精品日韩专区va | 向日葵视频app下载18岁以下勿看 | 亚洲高清无在码在线电影 | 草莓茄子丝瓜番茄小蝌蚪 | 91会员| 国产肥臀| 无敌在线视频观看免费 | 福利视频一区二区三区 | porno18hd老师| 亚洲精品国产一区二区第一页 | 亚洲男人的天堂成人 | 午夜福利体验免费体验区 | 欧美日韩精品一区二区三区视频播放 | 久久精品国产只有精品 | 国产一区二区免费福利片 | 高清国产精品久久久久 | 91综合在线视频 | 国产ay| 嫩草影院地址一地址二 | 免费观看小视频 | 国产nv精品你懂得 | 国产51社区精品视频资源 | 国产在亚洲线视频观看 | 国产精品久久久久久久午夜片 | 青苹果乐园影院免费观看完整版 | 美女脱一净二净不带胸罩 | 逼逼狗影院| 九九国产在线观看 | 国产欧美亚洲精品第一页青草 | 大胆国模一区二区三区伊人 | 久久日韩精品无码一区 |