隨筆-25  評論-6  文章-0  trackbacks-0

          用java.util.zip制作zip壓縮文件時,如果制作的壓縮文件有中文文件名或中文目錄,用WinZip、WinRar解壓時會有亂碼, 同樣,用java.util.zip解壓WinZip、WinRar打包的壓縮文件時,中文也是亂碼,主要原因是因為java.util.zip使用編碼和WinZip和WinRar使用的不同,在網上找了半天,發現只有兩種解決方法:
          1、修改import java.util.zip.ZipInputStream和ZipOutputStream。
          2、使用Apache Ant里提供的zip工具。
          -------------------------------------
          已找到解決方法,就是用Ant的zip來實現,Ant里面可以指定編碼,而java.util.zip是寫死了的,只能用UTF-8,呵呵
          -------------------------------
          不要使用java.util.zip的包,用ant.jar放到classpath中

          //在classpath中引入ant.jar的包
          import java.io.*;?
          import java.util.*;
          import java.io.File;
          import org.apache.tools.zip.*;

          public? class? CDGGzip?
          {?
          ? //boolean? packFrame? =? false;?
          ? private File srcPath =new File("d:"+File.separator+"邢曉寧");
          ? private String outFilename=new String("d:"+File.separator+"邢曉寧.zip");
          private int len=srcPath.listFiles().length;
          private String[] filenames = new String[len];
          public void setSrcPath(String src){
          ?? srcPath=new File(src);
          ? }
          ? public File getSrcPath(){
          ? return srcPath;
          ? }
          public void setOutFilename(String out){
          ?? outFilename=out;
          ? }
          ? public String getOutFilename(){
          ? return outFilename;
          ? }?

          ? public void gzip(){
          byte[] buf = new byte[1024];
          ? try {
          ? File[]? files? =? srcPath.listFiles();
          ? for(int? i=0;i<len;i++)
          ? {
          ? //if(!files[i].isDirectory())
          filenames[i]=srcPath.getPath()+File.separator+files[i].getName();
          }
          ? ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
          ? 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 (IOException e)
          ??? {
          ??? System.out.println(e);
          ??? }
          ? }
          ?
          ? public static void main(String arg[]){
          ? CDGGzip cdggzip=new CDGGzip();
          ? cdggzip.gzip();
          ? }
          ?
          }

          posted on 2006-05-29 19:08 MyJavaWorld 閱讀(436) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 新泰市| 河津市| 监利县| 白山市| 五原县| 云安县| 崇礼县| 冷水江市| 庄河市| 汕头市| 微博| 碌曲县| 外汇| 文山县| 武山县| 长白| 盈江县| 榆林市| 绵竹市| 南雄市| 桃源县| 镇赉县| 普定县| 苍梧县| 锡林浩特市| 泊头市| 忻城县| 正定县| 宜宾市| 闽侯县| 元朗区| 遂平县| 沙田区| 讷河市| 修水县| 南岸区| 泾川县| 吴堡县| 禄劝| 嘉义县| 星座|