posts - 22,comments - 35,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中

          ?

          ?1 // 在classpath中引入ant.jar的包
          ?2 import ?java.io. * ;??
          ?3 import ?java.util. * ;
          ?4 import ?java.io.File;
          ?5 import ?org.apache.tools.zip. * ;
          ?6
          ?7 public ?? class ??CDGGzip??
          ?8 {??
          ?9 ?? // boolean??packFrame??=??false;??
          10 ?? private ?File?srcPath? = new ?File( " d: " + File.separator + " 張任全 " );
          11 ?? private ?String?outFilename = new ?String( " d: " + File.separator + " 張任全.zip " );
          12 private ? int ?len = srcPath.listFiles().length;
          13 private ?String[]?filenames? = ? new ?String[len];
          14 public ? void ?setSrcPath(String?src) {
          15 ???srcPath = new ?File(src);
          16 ??}

          17 ?? public ?File?getSrcPath() {
          18 ?? return ?srcPath;
          19 ??}

          20 public ? void ?setOutFilename(String?out) {
          21 ???outFilename = out;
          22 ??}

          23 ?? public ?String?getOutFilename() {
          24 ?? return ?outFilename;
          25 ??}
          ??
          26
          27 ?? public ? void ?gzip() {
          28 byte []?buf? = ? new ? byte [ 1024 ];
          29 ?? try ? {
          30 ??File[]??files?? = ??srcPath.listFiles();?
          31 ?? for ( int ??i = 0 ;i < len;i ++ )
          32 ?? {
          33 ?? // if(!files[i].isDirectory())
          34 filenames[i] = srcPath.getPath() + File.separator + files[i].getName();
          35 }

          36 ??ZipOutputStream?out? = ? new ?ZipOutputStream( new ?FileOutputStream(outFilename));
          37 ?? for ?( int ?i = 0 ;?i < filenames.length;?i ++ )?
          38 ?? {
          39 ??FileInputStream?in? = ? new ?FileInputStream(filenames[i]);
          40 ??out.putNextEntry( new ?org.apache.tools.zip.ZipEntry(files[i].getName()));
          41 ?? int ?len;
          42 ?? while ?((len? = ?in.read(buf))? > ? 0 )?
          43 ?? {
          44 ??out.write(buf,? 0 ,?len);
          45 ??}

          46
          47 ??out.closeEntry();
          48 ??in.close();
          49 ??????}

          50
          51 ??????out.close();
          52 ????}
          ?
          53 ???? catch ?(IOException?e)?
          54 ???? {
          55 ????System.out.println(e);?
          56 ????}

          57 ??}

          58 ??
          59 ?? public ? static ? void ?main(String?arg[]) {
          60 ??CDGGzip?cdggzip = new ?CDGGzip();
          61 ??cdggzip.gzip();
          62 ??}

          63 }

          64
          65

          posted on 2006-04-21 18:24 kelven 閱讀(393) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 台中县| 阳山县| 乌拉特前旗| 田东县| 临桂县| 正蓝旗| 夹江县| 巴林左旗| 韩城市| 江阴市| 大渡口区| 元朗区| 绍兴县| 来安县| 上思县| 库车县| 车险| 清苑县| 化隆| 南川市| 平邑县| 阳东县| 乐平市| 荆门市| 绥芬河市| 屯留县| 新津县| 阳东县| 和平区| 从化市| 永济市| 什邡市| 沁源县| 咸丰县| 澄江县| 偏关县| 舒兰市| 日照市| 灵山县| 乐东| 苏尼特右旗|