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


    后來發現原來可以用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 小菜毛毛 閱讀(587) 評論(0)  編輯  收藏 所屬分類: J2EE相關技術與框架
    主站蜘蛛池模板: 亚洲AV无码成人精品区天堂| 国产大片91精品免费观看男同 | 精品国产日韩亚洲一区| 真正全免费视频a毛片| 日本免费人成视频播放| 久久人午夜亚洲精品无码区| 免费乱码中文字幕网站| 又大又硬又粗又黄的视频免费看| 亚洲国产综合久久天堂| 两个人看的www免费视频中文| 亚洲热线99精品视频| 高清一区二区三区免费视频| 91亚洲国产成人久久精品网站| 24小时日本电影免费看| 亚洲综合校园春色| 日韩免费高清一级毛片在线| 特级毛片全部免费播放a一级| 亚洲中文字幕视频国产| 国产精品视频白浆免费视频| 亚洲人成伊人成综合网久久| 日本免费的一级v一片| 中文字幕免费人成乱码中国| 中文字幕亚洲第一在线| 最近2019中文字幕mv免费看 | 免费看国产精品麻豆| 亚洲免费在线观看| 内射干少妇亚洲69XXX| 麻豆国产VA免费精品高清在线| 国产黄在线观看免费观看不卡| 老司机亚洲精品影院无码| 午夜一区二区免费视频| jizz在线免费播放| 国产成人精品日本亚洲11| 亚洲精品456播放| 91久久精品国产免费一区| 久久亚洲精品无码网站| 亚洲精品免费观看| 免费永久看黄在线观看app| 无码日韩精品一区二区三区免费| 亚洲乱码中文字幕在线| 久久久久亚洲AV无码专区首|