posts - 325,  comments - 25,  trackbacks - 0

          java.util.zip.ZipEntry
          java.util.zip.ZipOutputStream

          如果要解決中文文件名問題,用到ant.jar

          這兩個類。
          ZipOutputStream.putNextEntry(ZipEntry);就可以了,然后ZipOutputStream.wirte();就得了。

          package net.blogjava.chenlb.zip;

          import java.io.BufferedOutputStream;
          import java.io.File;
          import java.io.FileInputStream;
          import java.io.FileNotFoundException;
          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.io.OutputStream;
          //import java.util.zip.ZipEntry;
          //import java.util.zip.ZipOutputStream;
          //用ant.jar的zip.*可以解決中文文件名問題
          import org.apache.tools.zip.ZipEntry;
          import org.apache.tools.zip.ZipOutputStream;

          /**
           * 壓縮文件.
           * 2007-10-17 下午11:19:50
           * 
          @author chenlb
           
          */
          public class RecursiveZip {

              
              
          public static void main(String[] args) {

                  RecursiveZip recursiveZip 
          = new RecursiveZip();
                  System.out.println(
          "====開始====");
                  
          try {
                      OutputStream os 
          = new FileOutputStream("e:/doc-recursive.zip");
                      BufferedOutputStream bs 
          = new BufferedOutputStream(os);
                      ZipOutputStream zo 
          = new ZipOutputStream(bs);
                      
                      
          //recursiveZip.zip("e:/recursive-zip/中文文件名.txt", new File("e:/recursive-zip"), zo, true, true);
                      recursiveZip.zip("e:/recursive-zip"new File("e:/recursive-zip"), zo, truetrue);
                      
                      zo.closeEntry();
                      zo.close();
                  } 
          catch (FileNotFoundException e) {
                      e.printStackTrace();
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  }
                  System.out.println(
          "====完成====");
              }

              
          /**
               * 
          @param path 要壓縮的路徑, 可以是目錄, 也可以是文件.
               * 
          @param basePath 如果path是目錄,它一般為new File(path), 作用是:使輸出的zip文件以此目錄為根目錄, 如果為null它只壓縮文件, 不解壓目錄.
               * 
          @param zo 壓縮輸出流
               * 
          @param isRecursive 是否遞歸
               * 
          @param isOutBlankDir 是否輸出空目錄, 要使輸出空目錄為true,同時baseFile不為null.
               * 
          @throws IOException
               
          */
              
          public void zip(String path, File basePath, ZipOutputStream zo, boolean isRecursive, boolean isOutBlankDir) throws IOException {
                  
                  File inFile 
          = new File(path);

                  File[] files 
          = new File[0];
                  
          if(inFile.isDirectory()) {    //是目錄
                      files = inFile.listFiles();
                  } 
          else if(inFile.isFile()) {    //是文件
                      files = new File[1];
                      files[
          0= inFile;
                  }
                  
          byte[] buf = new byte[1024];
                  
          int len;
                  
          //System.out.println("baseFile: "+baseFile.getPath());
                  for(int i=0; i<files.length; i++) {
                      String pathName 
          = "";
                      
          if(basePath != null) {
                          
          if(basePath.isDirectory()) {
                              pathName 
          = files[i].getPath().substring(basePath.getPath().length()+1);
                          } 
          else {//文件
                              pathName = files[i].getPath().substring(basePath.getParent().length()+1);
                          }
                      } 
          else {
                          pathName 
          = files[i].getName();
                      }
                      System.out.println(pathName);
                      
          if(files[i].isDirectory()) {
                          
          if(isOutBlankDir && basePath != null) {    
                              zo.putNextEntry(
          new ZipEntry(pathName+"/"));    //可以使空目錄也放進去
                          }
                          
          if(isRecursive) {    //遞歸
                              zip(files[i].getPath(), basePath, zo, isRecursive, isOutBlankDir);
                          }
                      } 
          else {
                          FileInputStream fin 
          = new FileInputStream(files[i]);
                          zo.putNextEntry(
          new ZipEntry(pathName));
                          
          while((len=fin.read(buf))>0) {
                              zo.write(buf,
          0,len);
                          }
                          fin.close();
                      }
                  }
              }
          }
          posted on 2008-09-02 14:10 長春語林科技 閱讀(1735) 評論(1)  編輯  收藏 所屬分類: java

          FeedBack:
          # re: java 壓縮目錄為 zip
          2012-03-20 15:08 | 888
          hao  回復  更多評論
            
          <2008年9月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011

           

          長春語林科技歡迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新兴县| 黔西县| 扬中市| 木兰县| 启东市| 临武县| 乐山市| 电白县| 德化县| 洞口县| 克东县| 延庆县| 红河县| 洛阳市| 德化县| 长治县| 雷山县| 肥东县| 怀安县| 夏津县| 鄂伦春自治旗| 永城市| 北川| 南城县| 白玉县| 精河县| 遂昌县| 龙岩市| 凤山县| 紫金县| 广元市| 百色市| 青海省| 凤山市| 关岭| 泸西县| 盐源县| 玉环县| 霍林郭勒市| 五大连池市| 抚顺县|