用java流 压缩文件与解压缩文件代码

[java]  view plain  copy
 print ?
  1. package com.study.zip;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.FileOutputStream;  
  8. import java.io.IOException;  
  9. import java.util.zip.CRC32;  
  10. import java.util.zip.CheckedOutputStream;  
  11. import java.util.zip.Deflater;  
  12. import java.util.zip.ZipEntry;  
  13. import java.util.zip.ZipInputStream;  
  14.   
  15. import org.apache.tools.zip.ZipOutputStream;  
  16.   
  17. /** 
  18.  * 压缩与解压缩文件,目录工具类练习使用 
  19.  * 注意: 在java中,不管是目录还是文件实体,java都是把它当做一个ZipEntry对待的,ZipEntry的名字是相对于源头路径的全称 
  20.  * @author 乔宇鹏 
  21.  * 
  22.  */  
  23. public class ZipUtil {  
  24.       
  25.     /** 
  26.      * 将给定的文件压缩的指定的目录中,并指定压缩后文件的名称 
  27.      * @param src 源文件 
  28.      * @param targetDir 目标目录 
  29.      * @param zipFileName 给定压缩文件名称 
  30.      * @throws IOException 
  31.      */  
  32.     public static void copress(File src,File targetDir,String zipFileName) throws IOException{  
  33.         if( src.isDirectory())  
  34.             compress(src.listFiles(),targetDir,zipFileName);  
  35.         else   
  36.             compress(new File[]{src},targetDir,zipFileName);  
  37.     }  
  38.       
  39.     /** 
  40.      * 将多个文件压缩到给定的目录中,并指定压缩文件名称 
  41.      */  
  42.       
  43.     public static void compress(File[] files,File targetDir,String zipFileName ) throws IOException{  
  44.         File zipFile = createZip(files,targetDir,zipFileName);  
  45.         CheckedOutputStream cos = new CheckedOutputStream(  
  46.                 new BufferedOutputStream( new FileOutputStream(zipFile)), new CRC32());  
  47.         ZipOutputStream zipWriter = new ZipOutputStream(cos);  
  48.         zipWriter.setEncoding("GBK");  
  49.         zipWriter.setLevel(Deflater.BEST_COMPRESSION);  
  50.         zipWriter.setMethod(ZipOutputStream.DEFLATED);  
  51.           
  52.         String parentPath = "";  
  53.         for( File file : files ){  
  54.             if( file.isDirectory() )  
  55.                 compressDir(file,zipWriter,parentPath);  
  56.             else   
  57.                 compressFile(file,zipWriter,parentPath);  
  58.         }  
  59.           
  60.         zipWriter.flush();  
  61.         zipWriter.close();  
  62.           
  63.           
  64.     }  
  65.   
  66.     /** 进行压缩前的检查  **/  
  67.     private static File createZip(File[] files,File targetDir,String zipFileName) {  
  68.         if( files == null ) throw new RuntimeException("请选择被压缩的文件");  
  69.         if( zipFileName == null || "".equals(zipFileName)) zipFileName = "New ZipFile.zip";  
  70.         if( targetDir == null ) targetDir = files[0].getParentFile();  
  71.         if( targetDir.isFile() ) throw new RuntimeException("请选择正确的目录");  
  72.         File zipFile = new File(targetDir,zipFileName);  
  73.         if( zipFile.exists() )  
  74.             zipFile.delete();  
  75.         return zipFile;  
  76.     }  
  77.   
  78.     /** 压缩一个文件 */  
  79.     private static void compressFile(File srcFile, ZipOutputStream zipWriter, String parentPath) throws IOException {  
  80.         StringBuilder sb = new StringBuilder();  
  81.         sb.append(parentPath)  
  82.             .append(srcFile.getName());  
  83.         String path = sb.toString();  
  84.         org.apache.tools.zip.ZipEntry entry = entry = new org.apache.tools.zip.ZipEntry(path);  
  85.         entry.setTime(srcFile.lastModified());  
  86.         entry.setComment(srcFile.getName());  
  87.         entry.setCompressedSize(srcFile.length()/2);  
  88.         entry.setMethod(ZipEntry.DEFLATED);  
  89.         zipWriter.putNextEntry(entry);  
  90.         BufferedInputStream reader = new BufferedInputStream(  
  91.                 new FileInputStream(srcFile));  
  92.         byte[] cash = new byte[1024];  
  93.         int readed = 0;  
  94.         while( (readed = reader.read(cash)) > -1){  
  95.             zipWriter.write(cash,0,readed);  
  96.         }  
  97.         zipWriter.flush();  
  98.         zipWriter.closeEntry();  
  99.     }  
  100.      
  101.     /**  压缩一个目录   **/  
  102.     private static void compressDir(File srcDir, ZipOutputStream zipWriter,String parentPath) throws IOException {  
  103.         StringBuilder sb = new StringBuilder();  
  104.         sb.append(parentPath)  
  105.             .append(srcDir.getName())  
  106.             .append("/");  
  107.         String path = sb.toString();  
  108.         org.apache.tools.zip.ZipEntry entry = new org.apache.tools.zip.ZipEntry(path);  
  109.         entry.setTime(srcDir.lastModified());  
  110.         entry.setComment(srcDir.getName());  
  111.         zipWriter.putNextEntry(entry);  
  112.         File[] files = srcDir.listFiles();  
  113.         for( File file : files ){  
  114.             if( file.isDirectory())  
  115.                 compressDir(file, zipWriter, path);  
  116.             else   
  117.                 compressFile(file, zipWriter, path);  
  118.         }  
  119.         zipWriter.closeEntry();  
  120.     }  
  121.   
  122.    /**  完成解压缩任务 的方法 **/  
  123.     public static void uncompress(ZipInputStream zipReader ,File targetDir) throws IOException{  
  124.         ZipEntry zipEntry = null;  
  125.         while ((zipEntry = zipReader.getNextEntry()) != null) {  
  126.             if (zipEntry.isDirectory()) {  
  127.                 File tempDir = new File(targetDir, zipEntry.getName());  
  128.                 tempDir.mkdirs();  
  129.                 zipReader.closeEntry();  
  130.                 uncompress(zipReader, targetDir);  
  131.             } else {  
  132.                 BufferedOutputStream zipWriter  = new BufferedOutputStream(new FileOutputStream(  
  133.                         new File(targetDir,zipEntry.getName())));  
  134.                 byte[] cash = new byte[2048];  
  135.                 int readed = 0;  
  136.                 while ((readed = zipReader.read(cash)) > 0) {  
  137.                     zipWriter.write(cash, 0, readed);  
  138.                 }  
  139.                 zipWriter.flush();  
  140.                 zipWriter.close();  
  141.             }  
  142.   
  143.         }  
  144.     }  
  145. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值