<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    小菜毛毛技術分享

    與大家共同成長

      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. }  


    后來發(fā)現原來可以用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 小菜毛毛 閱讀(590) 評論(0)  編輯  收藏 所屬分類: J2EE相關技術與框架
    主站蜘蛛池模板: 午夜视频免费在线观看| 老妇激情毛片免费| 日韩中文字幕免费视频| 国产亚洲精品一品区99热| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | 久久亚洲AV无码精品色午夜麻豆| 国产精品亚洲精品久久精品| 成年女人看片免费视频播放器| 亚洲Av无码一区二区二三区| 国产一卡二卡3卡四卡免费| 亚洲精品不卡视频| 免费a级毛片无码a∨蜜芽试看| 亚洲中文字幕无码一去台湾| 在线视频观看免费视频18| 亚洲国产日产无码精品| 久久精品网站免费观看| 亚洲精品无码mⅴ在线观看| 日本视频免费在线| 无码的免费不卡毛片视频| 国产亚洲人成A在线V网站| 大地影院MV在线观看视频免费| 亚洲成亚洲乱码一二三四区软件| 国内精品免费在线观看| 亚洲熟妇av一区二区三区下载| 久草免费在线观看视频| 亚洲日韩中文字幕无码一区| 四虎永久免费观看| 日韩av无码免费播放| 亚洲春黄在线观看| 国产美女a做受大片免费| 国产美女视频免费观看的网站| 免费人成大片在线观看播放电影| 亚洲伊人久久成综合人影院| 免费精品久久天干天干| 亚洲中文字幕无码av在线| 四虎永久在线精品视频免费观看| 在线看片免费人成视频播| 亚洲一卡2卡3卡4卡乱码 在线 | 国产gav成人免费播放视频| 大地资源中文在线观看免费版| 亚洲ts人妖网站|