ava压缩文件--关于压缩文件名是乱码的解决方案

方法一:truezip.jar
参阅:http://jwin.javaeye.com/blog/23859
使用java.util.zip下的类进行zip压缩,它使用的是uft-8的编码方式,这样会引起中文名变成乱码的情况,解决的方法是用truezip.jar下的类,truezip的相关介绍和下载见https://truezip.dev.java.net/

注意:CopyUtils.copy(fis,zout); 所以不推荐使用,或者相应的替代。

方法二:ant.jar (推荐使用)
参阅:http://topic.csdn.net/t/20050628/00/4108770.html
用java.util.zip制作zip压缩文件时,如果制作的压缩文件有中文文件名或中文目录,用WinZip、WinRar解压时会有乱码,同样,用java.util.zip解压WinZip、WinRar打包的压缩文件时,中文也是乱码,主要原因是因为java.util.zip使用编码和WinZip和WinRar使用的不同(java.util.zip是写死了的,只能用UTF-8),解决方法:使用Apache   Ant里提供的zip工具(Ant里面可以指定编码)。 

示例:
import java.io.*;

import org.apache.commons.io.CopyUtils;

import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.FileOutputStream;
import de.schlichtherle.util.zip.ZipEntry;
import de.schlichtherle.util.zip.ZipOutputStream;

public class TestTrueZip {
   public static void main(String[] args) throws Exception {
      String needtozipfilepath = "E:/zip-temp/11/";     String zipfilepath = "E:/zip-temp/测试压缩-truezip.rar";
      TestTrueZip test = new TestTrueZip();
      test.createDownLoadZipFileByTrueZip(needtozipfilepath, zipfilepath);
   }

   public void createDownLoadZipFileByTrueZip(String needtozipfilepath,
        String zipFileName) throws Exception {
      File needtozipfile = new File(needtozipfilepath);
      File zipfile = new File(zipFileName);
      FileOutputStream fout = new FileOutputStream(zipfile);
      ZipOutputStream zout = new ZipOutputStream(fout, "GBK"); // 解决中文问题的关键所在
      try {
        for (File in : needtozipfile.listFiles()) {
           ZipEntry ze = new ZipEntry(in.getName());
           zout.putNextEntry(ze);
           FileInputStream fis = new FileInputStream(in);
           try {
              CopyUtils.copy(fis, zout);
           } catch (IOException e) {         
           } finally {
              if (fis != null)
                 fis.close();
              zout.closeEntry();
           }
        }
      } catch (IOException e) {
      } finally {
        if (zout != null)
        {  zout.close();
           fout.close();
           }
      }
   }
}

import java.io.*;

import org.apache.tools.zip.ZipOutputStream;

public class TestAntZip {
   public static void main(String arg[]) {
      String srcPath = "E:/zip-temp/11/";
      String outFilename = new String("E:/zip-temp/测试压缩-antzip.rar");   
      TestAntZip cdggzip = new TestAntZip();
      cdggzip.createDownLoadZipFileByAntZip(srcPath,outFilename);
   }
 
   public void createDownLoadZipFileByAntZip(String needtozipfilepath, String zipFileName){
      try {
        File srcPath = new File(needtozipfilepath);
        int len1 = srcPath.listFiles().length;
        String[] filenames = new String[len1];
        byte[] buf = new byte[1024];
        File[] files = srcPath.listFiles();
        for (int i = 0; i < len1; i++) {        
           filenames[i] = srcPath.getPath() + File.separator
                 + files[i].getName();
        }
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
              zipFileName));
        for (int i = 0; i < filenames.length; i++) {
           FileInputStream in = new FileInputStream(filenames[i]);
           out.putNextEntry(new org.apache.tools.zip.ZipEntry(files[i]
                 .getName()));
           int len;
           while ((len = in.read(buf)) > 0) {
              out.write(buf, 0, len);
           }
           out.closeEntry();
           in.close();
        }
        out.close();
      } catch (Exception e) {
      }  }
}

你可能感兴趣的:(解决方案)