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 長春語林科技 閱讀(1737) 評論(1)  編輯  收藏 所屬分類: java

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

           

          長春語林科技歡迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊

          收藏夾

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 天镇县| 淮北市| 高邑县| 资兴市| 云霄县| 家居| 扶绥县| 崇明县| 广汉市| 葵青区| 河东区| 宁国市| 中牟县| 双峰县| 青川县| 嘉黎县| 安塞县| 辽阳市| 福海县| 南木林县| 巴青县| 隆昌县| 古田县| 嵊泗县| 游戏| 尼玛县| 南雄市| 抚远县| 襄汾县| 靖西县| 开封市| 吴忠市| 东丰县| 上林县| 大兴区| 稷山县| 弋阳县| 遂溪县| 湖南省| 清镇市| 杂多县|