小菜毛毛技術分享

          與大家共同成長

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks
          關鍵字: java zip
          本來是寫到spaces live上的,可是代碼的顯示效果確實不怎么好看。在javaeye上試了試代碼顯示的順眼多了。

          今天寫了個用java壓縮的功能,可以實現對文件和目錄的壓縮。

          由于java.util.zip.ZipOutputStream有中文亂碼問題,所以采用org.apache.tools.zip.ZipOutputStream。
          以下是代碼:
          Java代碼 復制代碼
          1. package net.szh.zip;   
          2.   
          3. import java.io.BufferedInputStream;   
          4. import java.io.File;   
          5. import java.io.FileInputStream;   
          6. import java.io.FileOutputStream;   
          7. import java.util.zip.CRC32;   
          8. import java.util.zip.CheckedOutputStream;   
          9.   
          10. import org.apache.tools.zip.ZipEntry;   
          11. import org.apache.tools.zip.ZipOutputStream;   
          12.   
          13. public class ZipCompressor {   
          14.     static final int BUFFER = 8192;   
          15.   
          16.     private File zipFile;   
          17.   
          18.     public ZipCompressor(String pathName) {   
          19.         zipFile = new File(pathName);   
          20.     }   
          21.   
          22.     public void compress(String srcPathName) {   
          23.         File file = new File(srcPathName);   
          24.         if (!file.exists())   
          25.             throw new RuntimeException(srcPathName + "不存在!");   
          26.         try {   
          27.             FileOutputStream fileOutputStream = new FileOutputStream(zipFile);   
          28.             CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,   
          29.                     new CRC32());   
          30.             ZipOutputStream out = new ZipOutputStream(cos);   
          31.             String basedir = "";   
          32.             compress(file, out, basedir);   
          33.             out.close();   
          34.         } catch (Exception e) {   
          35.             throw new RuntimeException(e);   
          36.         }   
          37.     }   
          38.   
          39.     private void compress(File file, ZipOutputStream out, String basedir) {   
          40.         /* 判斷是目錄還是文件 */  
          41.         if (file.isDirectory()) {   
          42.             System.out.println("壓縮:" + basedir + file.getName());   
          43.             this.compressDirectory(file, out, basedir);   
          44.         } else {   
          45.             System.out.println("壓縮:" + basedir + file.getName());   
          46.             this.compressFile(file, out, basedir);   
          47.         }   
          48.     }   
          49.   
          50.     /** 壓縮一個目錄 */  
          51.     private void compressDirectory(File dir, ZipOutputStream out, String basedir) {   
          52.         if (!dir.exists())   
          53.             return;   
          54.   
          55.         File[] files = dir.listFiles();   
          56.         for (int i = 0; i < files.length; i++) {   
          57.             /* 遞歸 */  
          58.             compress(files[i], out, basedir + dir.getName() + "/");   
          59.         }   
          60.     }   
          61.   
          62.     /** 壓縮一個文件 */  
          63.     private void compressFile(File file, ZipOutputStream out, String basedir) {   
          64.         if (!file.exists()) {   
          65.             return;   
          66.         }   
          67.         try {   
          68.             BufferedInputStream bis = new BufferedInputStream(   
          69.                     new FileInputStream(file));   
          70.             ZipEntry entry = new ZipEntry(basedir + file.getName());   
          71.             out.putNextEntry(entry);   
          72.             int count;   
          73.             byte data[] = new byte[BUFFER];   
          74.             while ((count = bis.read(data, 0, BUFFER)) != -1) {   
          75.                 out.write(data, 0, count);   
          76.             }   
          77.             bis.close();   
          78.         } catch (Exception e) {   
          79.             throw new RuntimeException(e);   
          80.         }   
          81.     }   
          82. }  


          后來發現原來可以用ant中的org.apache.tools.ant.taskdefs.Zip來實現,更加簡單。
          Java代碼 復制代碼
          1. package net.szh.zip;   
          2.   
          3. import java.io.File;   
          4.   
          5. import org.apache.tools.ant.Project;   
          6. import org.apache.tools.ant.taskdefs.Zip;   
          7. import org.apache.tools.ant.types.FileSet;   
          8.   
          9. public class ZipCompressorByAnt {   
          10.   
          11.     private File zipFile;   
          12.   
          13.     public ZipCompressorByAnt(String pathName) {   
          14.         zipFile = new File(pathName);   
          15.     }   
          16.        
          17.     public void compress(String srcPathName) {   
          18.         File srcdir = new File(srcPathName);   
          19.         if (!srcdir.exists())   
          20.             throw new RuntimeException(srcPathName + "不存在!");   
          21.            
          22.         Project prj = new Project();   
          23.         Zip zip = new Zip();   
          24.         zip.setProject(prj);   
          25.         zip.setDestFile(zipFile);   
          26.         FileSet fileSet = new FileSet();   
          27.         fileSet.setProject(prj);   
          28.         fileSet.setDir(srcdir);   
          29.         //fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夾 eg:zip.setIncludes("*.java");   
          30.         //fileSet.setExcludes(...); 排除哪些文件或文件夾   
          31.         zip.addFileset(fileSet);   
          32.            
          33.         zip.execute();   
          34.     }   
          35. }  

          測試一下
          Java代碼 復制代碼
          1. package net.szh.zip;   
          2.   
          3. public class TestZip {   
          4.     public static void main(String[] args) {   
          5.         ZipCompressor zc = new  ZipCompressor("E:\\szhzip.zip");   
          6.         zc.compress("E:\\test");   
          7.            
          8.         ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\\szhzipant.zip");   
          9.         zca.compress("E:\\test");   
          10.     }   
          11. }  
          posted on 2010-01-18 17:06 小菜毛毛 閱讀(588) 評論(0)  編輯  收藏 所屬分類: J2EE相關技術與框架
          主站蜘蛛池模板: 长阳| 文安县| 彰武县| 铅山县| 九江市| 德安县| 林西县| 梁平县| 昔阳县| 斗六市| 微山县| 渝北区| 泸定县| 南岸区| 石景山区| 应用必备| 东辽县| 响水县| 明星| 吴旗县| 墨竹工卡县| 赞皇县| 沙湾县| 鄂温| 红原县| 白城市| 高邮市| 蒲江县| 阿城市| 随州市| 中超| 肥乡县| 瑞安市| 尚志市| 全椒县| 陇川县| 布尔津县| 福海县| 廉江市| 英山县| 长春市|