0
点赞
收藏
分享

微信扫一扫

Java中对文件的解压缩


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

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

压缩方法


​​



  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. new File(resourcesPath);     //源文件  
  14. "";  
  15. //如果为null或空字符串则默认解压缩到跟源文件夹或者文件所在的同一目录  
  16. if (StringUtil.isEmpty(targetPath))   
  17. 0,resourcesPath.replaceAll("\\*", "/").lastIndexOf("/"));  
  18. else   
  19.         directoryPath = targetPath;  
  20. new File(directoryPath);           //目的  
  21. //如果目的路径不存在,则新建  
  22. if(!targetFile.exists())  
  23.     {       
  24.         targetFile.mkdirs();    
  25.     }  
  26. ".zip";   //目的压缩文件名  
  27. new FileOutputStream(directoryPath+File.separator+targetName);  
  28. new ZipOutputStream(new BufferedOutputStream(outputStream));  
  29. "");  
  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. new ZipEntry(dir+"/"));  
  49. 0 ? "" : dir +"/";  
  50. //循环将文件夹中的文件打包  
  51. for(int i = 0 ; i < files.length ; i++)  
  52.         {  
  53. //递归处理  
  54.         }  
  55.     }  
  56. else  
  57. //当前的是文件,打包处理  
  58. //文件输入流  
  59. new FileInputStream(file);  
  60. new ZipEntry(dir));  
  61. //进行写操作  
  62. int len =  0;  
  63. byte[] buffer = new byte[1024];  
  64. while((len = is.read(buffer)) > 0)  
  65.         {  
  66. 0,len);  
  67.         }  
  68. //关闭输入流  
  69.         is.close();  
  70.     }  
  71. }  



解压缩方法


​​



  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. new ZipFile(zipFilePath);  
  12. "";  
  13. //如果为null或空字符串则默认解压缩到跟zip包同目录跟zip包同名的文件夹下  
  14. if (StringUtil.isEmpty(targetPath))   
  15. 0,zipFilePath.lastIndexOf("."));  
  16. else   
  17.         directoryPath = targetPath;  
  18.       
  19.     Enumeration<ZipEntry> entryEnum = zipFile.getEntries();  
  20. if (entryEnum != null)   
  21.     {  
  22. null;  
  23. while (entryEnum.hasMoreElements())   
  24.         {  
  25.             zipEntry = (ZipEntry) entryEnum.nextElement();  
  26. //如果为目录  
  27. if (zipEntry.isDirectory())   
  28.             {  
  29. "文件夹路劲--------------"+directoryPath + File.separator+ zipEntry.getName());  
  30. new File(directoryPath + File.separator+ zipEntry.getName());  
  31.                 target.mkdirs();  
  32. continue;  
  33.             }  
  34. if (zipEntry.getSize() !=-1)   
  35.             {  
  36. // 文件  
  37. new File(directoryPath+ File.separator + zipEntry.getName());  
  38. //如果目的文件夹的父目录为空,则创建  
  39. if (!targetFile.getParentFile().exists()) {  
  40.                     targetFile.getParentFile().mkdirs();  
  41. new File(targetFile.getAbsolutePath());  
  42.                 }  
  43. 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, 0, 1024)) >= 0)   
  48.                 {  
  49. 0, len);  
  50.                 }  
  51.                 os.close();  
  52.                 is.close();  
  53.             }   
  54.         }  
  55.     }  
  56. }  



这里提到一点为什么只能解压缩zip格式,其原因是windows和linux两者都兼容的只有zip文件。

举报

相关推荐

0 条评论