zoukankan      html  css  js  c++  java
  • 用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. }  
  • 相关阅读:
    几种常见的软件体系结构及特点分析
    mvc模式
    代理模式 补充
    软件架构体系风格
    大道至简之编程的精义读后感-Java伪代码
    MVC架构模式实例
    浅谈模型-视图-控制器模式
    《大型网站技术架构:核心原理与案例分析》读后感
    质量属性分析
    构架漫谈读后感之软件架构师的工作
  • 原文地址:https://www.cnblogs.com/langtianya/p/4820093.html
Copyright © 2011-2022 走看看