posts - 325,  comments - 25,  trackbacks - 0

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

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

          這兩個(gè)類。
          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,同時(shí)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+"/"));    //可以使空目錄也放進(jìn)去
                          }
                          
          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 長(zhǎng)春語(yǔ)林科技 閱讀(1735) 評(píng)論(1)  編輯  收藏 所屬分類: java

          FeedBack:
          # re: java 壓縮目錄為 zip
          2012-03-20 15:08 | 888
          <2012年3月>
          26272829123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

           

          長(zhǎng)春語(yǔ)林科技?xì)g迎您!

          常用鏈接

          留言簿(6)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          相冊(cè)

          收藏夾

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 永仁县| 宜昌市| 武宣县| 武陟县| 晋宁县| 禄丰县| 永福县| 广汉市| 兴业县| 云霄县| 苏尼特左旗| 黄冈市| 保康县| 花莲县| 平邑县| 磴口县| 西畴县| 扎鲁特旗| 周至县| 海伦市| 德兴市| 读书| 竹溪县| 建水县| 屯门区| 宁河县| 清水河县| 胶南市| 洞头县| 贵溪市| 三门县| 衢州市| 罗源县| 同江市| 兴安盟| 元阳县| 茶陵县| 东乌珠穆沁旗| 盘山县| 五家渠市| 蒙山县|