隨筆-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 閱讀(435) 評論(0)  編輯  收藏

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


          網站導航:
           
          主站蜘蛛池模板: 泗阳县| 晋中市| 庆阳市| 佛教| 玉门市| 秭归县| 扶沟县| 太原市| 山阴县| 祁阳县| 堆龙德庆县| 邵东县| 广宁县| 周至县| 石阡县| 繁峙县| 信丰县| 钟祥市| 当阳市| 兴国县| 绩溪县| 黑龙江省| 临高县| 商丘市| 嘉善县| 嘉荫县| 黄平县| 陆河县| 文安县| 桓仁| 潞西市| 五常市| 南召县| 仙桃市| 白沙| 江油市| 合阳县| 乌审旗| 衡南县| 同德县| 连南|