使用zip对文件或文件夹进行压缩, 解压缩

使用zip对文件或文件夹进行压缩, 解压缩
Java代码 复制代码 收藏代码
  1. import java.io.File; 
  2. import java.io.FileInputStream; 
  3. import java.io.FileOutputStream;
  4. import java.io.IOException; 
  5. import java.util.zip.ZipEntry; 
  6. import java.util.zip.ZipInputStream; 
  7. import java.util.zip.ZipOutputStream;
  8.  
  9. /**
  10. * 对文件或文件夹进行压缩和解压
  11. *
  12. */ 
  13. public class ZipUtil { 
  14.     /**得到当前系统的分隔符*/ 
  15. //  private static String separator = System.getProperty("file.separator"); 
  16.  
  17.     /**
  18.      * 添加到压缩文件中
  19.      * @param out
  20.      * @param f
  21.      * @param base
  22.      * @throws Exception
  23.      */ 
  24.     private void directoryZip(ZipOutputStream out, File f, String base) throws Exception { 
  25.         // 如果传入的是目录 
  26.         if (f.isDirectory()) { 
  27.             File[] fl = f.listFiles(); 
  28.             // 创建压缩的子目录 
  29.             out.putNextEntry(new ZipEntry(base + "/")); 
  30.             if (base.length() == 0) { 
  31.                 base = ""
  32.             } else
  33.                 base = base + "/"
  34.             } 
  35.             for (int i = 0; i < fl.length; i++) { 
  36.                 directoryZip(out, fl[i], base + fl[i].getName()); 
  37.             } 
  38.         } else
  39.             // 把压缩文件加入rar中 
  40.             out.putNextEntry(new ZipEntry(base)); 
  41.             FileInputStream in = new FileInputStream(f); 
  42.             byte[] bb = new byte[10240]; 
  43.             int aa = 0
  44.             while ((aa = in.read(bb)) != -1) { 
  45.                 out.write(bb, 0, aa); 
  46.             } 
  47.             in.close(); 
  48.         } 
  49.     } 
  50.  
  51.     /**
  52.      * 压缩文件
  53.      *
  54.      * @param zos
  55.      * @param file
  56.      * @throws Exception
  57.      */ 
  58.     private void fileZip(ZipOutputStream zos, File file) throws Exception { 
  59.         if (file.isFile()) { 
  60.             zos.putNextEntry(new ZipEntry(file.getName())); 
  61.             FileInputStream fis = new FileInputStream(file); 
  62.             byte[] bb = new byte[10240]; 
  63.             int aa = 0
  64.             while ((aa = fis.read(bb)) != -1) { 
  65.                 zos.write(bb, 0, aa); 
  66.             } 
  67.             fis.close(); 
  68.             System.out.println(file.getName()); 
  69.         } else
  70.             directoryZip(zos, file, ""); 
  71.         } 
  72.     } 
  73.  
  74.     /**
  75.      * 解压缩文件
  76.      *
  77.      * @param zis
  78.      * @param file
  79.      * @throws Exception
  80.      */ 
  81.     private void fileUnZip(ZipInputStream zis, File file) throws Exception { 
  82.         ZipEntry zip = zis.getNextEntry(); 
  83.         if (zip == null
  84.             return
  85.         String name = zip.getName(); 
  86.         File f = new File(file.getAbsolutePath() + "/" + name); 
  87.         if (zip.isDirectory()) { 
  88.             f.mkdirs(); 
  89.             fileUnZip(zis, file); 
  90.         } else
  91.             f.createNewFile(); 
  92.             FileOutputStream fos = new FileOutputStream(f); 
  93.             byte b[] = new byte[10240]; 
  94.             int aa = 0
  95.             while ((aa = zis.read(b)) != -1) { 
  96.                 fos.write(b, 0, aa); 
  97.             } 
  98.             fos.close(); 
  99.             fileUnZip(zis, file); 
  100.         } 
  101.     } 
  102.      
  103.     /**
  104.      * 根据filePath创建相应的目录
  105.      * @param filePath
  106.      * @return
  107.      * @throws IOException
  108.      */ 
  109.     private File mkdirFiles(String filePath) throws IOException{ 
  110.         File file = new File(filePath); 
  111.         if(!file.getParentFile().exists()){ 
  112.             file.getParentFile().mkdirs(); 
  113.         } 
  114.         file.createNewFile(); 
  115.          
  116.         return file; 
  117.     } 
  118.  
  119.     /**
  120.      * 对zipBeforeFile目录下的文件压缩,保存为指定的文件zipAfterFile
  121.      *
  122.      * @param zipBeforeFile     压缩之前的文件
  123.      * @param zipAfterFile      压缩之后的文件
  124.      */ 
  125.     public void zip(String zipBeforeFile, String zipAfterFile) { 
  126.         try
  127.              
  128.             ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(mkdirFiles(zipAfterFile))); 
  129.             fileZip(zos, new File(zipBeforeFile)); 
  130.             zos.close(); 
  131.         } catch (Exception e) { 
  132.             e.printStackTrace(); 
  133.         } 
  134.     } 
  135.  
  136.     /**
  137.      * 解压缩文件unZipBeforeFile保存在unZipAfterFile目录下
  138.      *
  139.      * @param unZipBeforeFile       解压之前的文件
  140.      * @param unZipAfterFile        解压之后的文件
  141.      */ 
  142.     public void unZip(String unZipBeforeFile, String unZipAfterFile) { 
  143.         try
  144.             ZipInputStream zis = new ZipInputStream(new FileInputStream(unZipBeforeFile)); 
  145.             File f = new File(unZipAfterFile); 
  146.             f.mkdirs(); 
  147.             fileUnZip(zis, f); 
  148.             zis.close(); 
  149.         } catch (Exception e) { 
  150.             e.printStackTrace(); 
  151.         } 
  152.     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值