Java中对文件的解压缩

 需要用到的jar包: apache-ant-1.9.3.jar

 下载地址:http://ant.apache.org/bindownload.cgi

压缩方法

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.    * @desc 将源文件/文件夹生成指定格式的压缩文件,格式zip 
  3.    * @param resourePath  
  4.    *                        源文件/文件夹 
  5.    * @param targetPath   
  6.    *                        目的压缩文件保存路径 
  7.    *                        压缩到的位置,如果为null或空字符串则默认解压缩到跟源文件夹或者文件所在的同一目录 
  8.    * @return void 
  9.    *                 
  10.    * @throws Exception  
  11.    */  
  12. public static void compressedFile(String resourcesPath,String targetPath) throws Exception{  
  13.     File resourcesFile = new File(resourcesPath);     //源文件  
  14.     String directoryPath = "";  
  15.     //如果为null或空字符串则默认解压缩到跟源文件夹或者文件所在的同一目录  
  16.     if (StringUtil.isEmpty(targetPath))   
  17.         directoryPath = resourcesPath.substring(0,resourcesPath.replaceAll("\\*""/").lastIndexOf("/"));  
  18.     else   
  19.         directoryPath = targetPath;  
  20.     File targetFile = new File(directoryPath);           //目的  
  21.     //如果目的路径不存在,则新建  
  22.     if(!targetFile.exists())  
  23.     {       
  24.         targetFile.mkdirs();    
  25.     }  
  26.     String targetName = resourcesFile.getName()+".zip";   //目的压缩文件名  
  27.     FileOutputStream outputStream = new FileOutputStream(directoryPath+File.separator+targetName);  
  28.     ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));  
  29.     createCompressedFile(out, resourcesFile, "");  
  30.     out.close();    
  31. }  
  32.  /** 
  33.   * @desc 生成压缩文件。 如果是文件夹,则使用递归,进行文件遍历、压缩 
  34.   *                     如果是文件,直接压缩 
  35.   * @param out   
  36.   *                     输出流 
  37.   * @param file   
  38.   *                     目标文件 
  39.   * @return void 
  40.   * @throws Exception  
  41.   */  
  42. public static void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{  
  43.     //如果当前的是文件夹,则进行进一步处理  
  44.     if(file.isDirectory()){  
  45.         //得到文件列表信息  
  46.         File[] files = file.listFiles();  
  47.         //将文件夹添加到下一级打包目录  
  48.         out.putNextEntry(new ZipEntry(dir+"/"));  
  49.         dir = dir.length() == 0 ? "" : dir +"/";  
  50.         //循环将文件夹中的文件打包  
  51.         for(int i = 0 ; i < files.length ; i++)  
  52.         {  
  53.             createCompressedFile(out, files[i], dir + files[i].getName());         //递归处理  
  54.         }  
  55.     }  
  56.     else  
  57.     {   //当前的是文件,打包处理  
  58.         //文件输入流  
  59.         InputStream is = new FileInputStream(file);  
  60.         out.putNextEntry(new ZipEntry(dir));  
  61.         //进行写操作  
  62.         int len =  0;  
  63.         byte[] buffer = new byte[1024];  
  64.         while((len = is.read(buffer)) > 0)  
  65.         {  
  66.             out.write(buffer,0,len);  
  67.         }  
  68.         //关闭输入流  
  69.         is.close();  
  70.     }  
  71. }  

解压缩方法

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * 解压缩zip包 
  3.  *  
  4.  * @param zipFilePath 
  5.  *            zip文件路径 
  6.  * @param targetPath 
  7.  *            解压缩到的位置,如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下 
  8.  * @throws IOException 
  9.  */  
  10. public static void decompressionFile(String zipFilePath, String targetPath)throws IOException {  
  11.     ZipFile zipFile = new ZipFile(zipFilePath);  
  12.     String directoryPath = "";  
  13.     //如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下  
  14.     if (StringUtil.isEmpty(targetPath))   
  15.         directoryPath = zipFilePath.substring(0,zipFilePath.lastIndexOf("."));  
  16.     else   
  17.         directoryPath = targetPath;  
  18.       
  19.     Enumeration<ZipEntry> entryEnum = zipFile.getEntries();  
  20.     if (entryEnum != null)   
  21.     {  
  22.         ZipEntry zipEntry = null;  
  23.         while (entryEnum.hasMoreElements())   
  24.         {  
  25.             zipEntry = (ZipEntry) entryEnum.nextElement();  
  26.             //如果为目录  
  27.             if (zipEntry.isDirectory())   
  28.             {  
  29.                 System.out.println("文件夹路劲--------------"+directoryPath + File.separator+ zipEntry.getName());  
  30.                 File target = new File(directoryPath + File.separator+ zipEntry.getName());  
  31.                 target.mkdirs();  
  32.                 continue;  
  33.             }  
  34.             if (zipEntry.getSize() !=-1)   
  35.             {  
  36.                 // 文件  
  37.                 File targetFile = new File(directoryPath+ File.separator + zipEntry.getName());  
  38.                 //如果目的文件夹的父目录为空,则创建  
  39.                 if (!targetFile.getParentFile().exists()) {  
  40.                     targetFile.getParentFile().mkdirs();  
  41.                     targetFile = new File(targetFile.getAbsolutePath());  
  42.                 }  
  43.                 OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile));  
  44.                 InputStream is = zipFile.getInputStream(zipEntry);  
  45.                 byte[] buffer = new byte[1024];  
  46.                 int len = 0;  
  47.                 while ((len = is.read(buffer, 01024)) >= 0)   
  48.                 {  
  49.                     os.write(buffer, 0, len);  
  50.                 }  
  51.                 os.close();  
  52.                 is.close();  
  53.             }   
  54.         }  
  55.     }  
  56. }  

这里提到一点为什么只能解压缩zip格式,其原因是windows和linux两者都兼容的只有zip文件。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值