java压缩zip文件

来源:互联网 发布:php print 编辑:程序博客网 时间:2024/04/30 12:15
  1. public File doZip(String sourceDir, String zipFilePath)   
  2.  throws IOException {  
  3.     
  4.   File file = new File(sourceDir);  
  5.   File zipFile = new File(zipFilePath);  
  6.   ZipOutputStream zos = null;  
  7.   try {  
  8.    // 创建写出流操作  
  9.    OutputStream os = new FileOutputStream(zipFile);  
  10.    BufferedOutputStream bos = new BufferedOutputStream(os);  
  11.    zos = new ZipOutputStream(bos);  
  12.      
  13.    String basePath = null;  
  14.      
  15.    // 获取目录  
  16.    if(file.isDirectory()) {  
  17.     basePath = file.getPath();  
  18.    }else {  
  19.     basePath = file.getParent();  
  20.    }  
  21.      
  22.    zipFile(file, basePath, zos);  
  23.   }finally {  
  24.    if(zos != null) {  
  25.     zos.closeEntry();  
  26.     zos.close();  
  27.    }  
  28.   }  
  29.     
  30.   return zipFile;  
  31.  }  
  32.   
  33.  /**  
  34.   * @param source 源文件 
  35.   * @param basePath  
  36.   * @param zos  
  37.   */  
  38.  private void zipFile(File source, String basePath, ZipOutputStream zos)   
  39.  throws IOException {  
  40.   File[] files = null;  
  41.   if (source.isDirectory()) {  
  42.    files = source.listFiles();  
  43.   } else {  
  44.    files = new File[1];  
  45.    files[0] = source;  
  46.   }  
  47.     
  48.   InputStream is = null;  
  49.   String pathName;  
  50.   byte[] buf = new byte[1024];  
  51.   int length = 0;  
  52.   try{  
  53.    for(File file : files) {  
  54.     if(file.isDirectory()) {  
  55.      pathName = file.getPath().substring(basePath.length() + 1) + "/";  
  56.      zos.putNextEntry(new ZipEntry(pathName));  
  57.      zipFile(file, basePath, zos);  
  58.     }else {  
  59.      pathName = file.getPath().substring(basePath.length() + 1);  
  60.      is = new FileInputStream(file);  
  61.      BufferedInputStream bis = new BufferedInputStream(is);  
  62.      zos.putNextEntry(new ZipEntry(pathName));  
  63.      while ((length = bis.read(buf)) > 0) {  
  64.       zos.write(buf, 0, length);  
  65.      }  
  66.     }  
  67.    }  
  68.   }finally {  
  69.    if(is != null) {  
  70.     is.close();  
  71.    }  
  72.   }  
  73.   
  74.  }  

 

  • 使用org.apache.tools.zip.ZipOutputStream,代码如下
Java代码  收藏代码
  1. public class ZipCompressor {     
  2.     static final int BUFFER = 8192;     
  3.     
  4.     private File zipFile;     
  5.     
  6.     public ZipCompressor(String pathName) {     
  7.         zipFile = new File(pathName);     
  8.     }     
  9.     
  10.     public void compress(String srcPathName) {     
  11.         File file = new File(srcPathName);     
  12.         if (!file.exists())     
  13.             throw new RuntimeException(srcPathName + "不存在!");     
  14.         try {     
  15.             FileOutputStream fileOutputStream = new FileOutputStream(zipFile);     
  16.             CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,     
  17.                     new CRC32());     
  18.             ZipOutputStream out = new ZipOutputStream(cos);     
  19.             String basedir = "";     
  20.             compress(file, out, basedir);     
  21.             out.close();     
  22.         } catch (Exception e) {     
  23.             throw new RuntimeException(e);     
  24.         }     
  25.     }     
  26.     
  27.     private void compress(File file, ZipOutputStream out, String basedir) {     
  28.         /* 判断是目录还是文件 */    
  29.         if (file.isDirectory()) {     
  30.             System.out.println("压缩:" + basedir + file.getName());     
  31.             this.compressDirectory(file, out, basedir);     
  32.         } else {     
  33.             System.out.println("压缩:" + basedir + file.getName());     
  34.             this.compressFile(file, out, basedir);     
  35.         }     
  36.     }     
  37.     
  38.     /** 压缩一个目录 */    
  39.     private void compressDirectory(File dir, ZipOutputStream out, String basedir) {     
  40.         if (!dir.exists())     
  41.             return;     
  42.     
  43.         File[] files = dir.listFiles();     
  44.         for (int i = 0; i < files.length; i++) {     
  45.             /* 递归 */    
  46.             compress(files[i], out, basedir + dir.getName() + "/");     
  47.         }     
  48.     }     
  49.     
  50.     /** 压缩一个文件 */    
  51.     private void compressFile(File file, ZipOutputStream out, String basedir) {     
  52.         if (!file.exists()) {     
  53.             return;     
  54.         }     
  55.         try {     
  56.             BufferedInputStream bis = new BufferedInputStream(     
  57.                     new FileInputStream(file));     
  58.             ZipEntry entry = new ZipEntry(basedir + file.getName());     
  59.             out.putNextEntry(entry);     
  60.             int count;     
  61.             byte data[] = new byte[BUFFER];     
  62.             while ((count = bis.read(data, 0, BUFFER)) != -1) {     
  63.                 out.write(data, 0, count);     
  64.             }     
  65.             bis.close();     
  66.         } catch (Exception e) {     
  67.             throw new RuntimeException(e);     
  68.         }     
  69.     }     

  1. }  
    • 可以用ant中的org.apache.tools.ant.taskdefs.Zip来实现,更加简单
    Java代码  收藏代码
    1. public class ZipCompressorByAnt {     
    2.     
    3.     private File zipFile;     
    4.     
    5.     public ZipCompressorByAnt(String pathName) {     
    6.         zipFile = new File(pathName);     
    7.     }     
    8.          
    9.     public void compress(String srcPathName) {     
    10.         File srcdir = new File(srcPathName);     
    11.         if (!srcdir.exists())     
    12.             throw new RuntimeException(srcPathName + "不存在!");     
    13.              
    14.         Project prj = new Project();     
    15.         Zip zip = new Zip();     
    16.         zip.setProject(prj);     
    17.         zip.setDestFile(zipFile);     
    18.         FileSet fileSet = new FileSet();     
    19.         fileSet.setProject(prj);     
    20.         fileSet.setDir(srcdir);     
    21.         //fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夹 eg:zip.setIncludes("*.java");     
    22.         //fileSet.setExcludes(...); 排除哪些文件或文件夹     
    23.         zip.addFileset(fileSet);     
    24.              
    25.         zip.execute();     
    26.     }     
    27. }    
    28. 测试一下   
    29. Java代码  
    30. package net.szh.zip;     
    31.     
    32. public class TestZip {     
    33.     public static void main(String[] args) {     
    34.         ZipCompressor zc = new  ZipCompressor("E:\\szhzip.zip");     
    35.         zc.compress("E:\\test");     
    36.              
    37.         ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\\szhzipant.zip");     
    38.         zca.compress("E:\\test");     
    39.     }     
    40. }  

原址:http://411373793.iteye.com/blog/2181118

0 0
原创粉丝点击