这两天,因为项目需要,涉及到文件夹的上传,想了很久,在网上也找了一些资料,都没有什么很好的办法,都是用插件之类的解决,后面就想到了上传压缩 文件,说到压缩文件,自然就会遇到,文件的解压缩问题,大家都知道,比较常见的压缩文件有rar,zip,然而rar,zip的区别又在哪?说一 点,zip 压缩算法是免费开放的,任何人可以免费使用。但是 RAR 就不一样了,
这个压缩算法已经受到专利权的保护,如果要使用 RAR 算法必须向其专利所有人支付费用。所以在一般的开源网站,像apache,sourceforge等开源网站上的开源项目一般都用zip格式.本人所学语 言主要为java自然会想用java去解压,用java去解压zip比较容易,有apache提供的开源项目ant,我在网上找一下,找到了 sourceforge的开源项目unrar专用于压缩,解压rar.只可惜没有文档(让人即喜,又悲).

下面是一个对zip,rar进行解压的程序(从http://topic.csdn.net/u/20090227/11/fd8c30ee-ce56-49be-bdea-d19d22a0da37.html 转 载,供大家一起享用,另外希望对unrar有比较发解的朋友,能发表一个人见解),

备注:你需要java-unrar.zip<解压rar用> 和ant.jar<解压zip用> 两个jar包

Java代码
  1. import  org.apache.tools.tar.TarEntry;
  2. import  org.apache.tools.tar.TarOutputStream;
  3. import  org.apache.tools.zip.ZipEntry;
  4. import  org.apache.tools.zip.ZipFile;
  5. import  org.apache.tools.zip.ZipOutputStream;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarOutputStream;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
Java代码
  1. import  de.innosystec.unrar.Archive;
import de.innosystec.unrar.Archive;
Java代码
  1. /**
  2. *  *
  3. * @version 创建时间:Feb 26, 2009 6:01:11 PM
  4. * 类说明:压缩、解压文件公用类
  5. *
  6. */
  7. public   class  Decompression {
  8. private   static   final   int  BUFFEREDSIZE =  1024 ;
  9. /**
  10. * 解 压zip格式的压缩文件到指定位置
  11. * @param zipFileName 压 缩文件
  12. * @param extPlace 解 压目录
  13. * @throws Exception
  14. */
  15. @SuppressWarnings ( "unchecked" )
  16. public   synchronized   void  unzip(String zipFileName, String extPlace)  throws  Exception {
  17. try  {
  18. (new  File(extPlace)).mkdirs();
  19. File f = new  File(zipFileName);
  20. ZipFile zipFile = new  ZipFile(zipFileName);
  21. if ((!f.exists()) && (f.length() <=  0 )) {
  22. throw   new  Exception( "要解压的文件不存在!" );
  23. }
  24. String strPath, gbkPath, strtemp;
  25. File tempFile = new  File(extPlace);
  26. strPath = tempFile.getAbsolutePath();
  27. java.util.Enumeration e = zipFile.getEntries();
  28. while (e.hasMoreElements()){
  29. org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
  30. gbkPath=zipEnt.getName();
  31. if (zipEnt.isDirectory()){
  32. strtemp = strPath + File.separator + gbkPath;
  33. File dir = new  File(strtemp);
  34. dir.mkdirs();
  35. continue ;
  36. else  {
  37. //读写文件
  38. InputStream is = zipFile.getInputStream(zipEnt);
  39. BufferedInputStream bis = new  BufferedInputStream(is);
  40. gbkPath=zipEnt.getName();
  41. strtemp = strPath + File.separator + gbkPath;
  42. //建目录
  43. String strsubdir = gbkPath;
  44. for ( int  i =  0 ; i < strsubdir.length(); i++) {
  45. if (strsubdir.substring(i, i +  1 ).equalsIgnoreCase( "/" )) {
  46. String temp = strPath + File.separator + strsubdir.substring(0 , i);
  47. File subdir = new  File(temp);
  48. if (!subdir.exists())
  49. subdir.mkdir();
  50. }
  51. }
  52. FileOutputStream fos = new  FileOutputStream(strtemp);
  53. BufferedOutputStream bos = new  BufferedOutputStream(fos);
  54. int  c;
  55. while ((c = bis.read()) != - 1 ) {
  56. bos.write((byte ) c);
  57. }
  58. bos.close();
  59. fos.close();
  60. }
  61. }
  62. catch (Exception e) {
  63. e.printStackTrace();
  64. throw  e;
  65. }
  66. }
  67. /**
  68. * 解 压zip格式的压缩文件到指定位置
  69. * @param zipFileName 压 缩文件
  70. * @param extPlace 解 压目录
  71. * @throws Exception
  72. */
  73. @SuppressWarnings ( "unchecked" )
  74. public   synchronized   void  unzip(String zipFileName, String extPlace, boolean  whether)  throws  Exception {
  75. try  {
  76. (new  File(extPlace)).mkdirs();
  77. File f = new  File(zipFileName);
  78. ZipFile zipFile = new  ZipFile(zipFileName);
  79. if ((!f.exists()) && (f.length() <=  0 )) {
  80. throw   new  Exception( "要解压的文件不存在!" );
  81. }
  82. String strPath, gbkPath, strtemp;
  83. File tempFile = new  File(extPlace);
  84. strPath = tempFile.getAbsolutePath();
  85. java.util.Enumeration e = zipFile.getEntries();
  86. while (e.hasMoreElements()){
  87. org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement();
  88. gbkPath=zipEnt.getName();
  89. if (zipEnt.isDirectory()){
  90. strtemp = strPath + File.separator + gbkPath;
  91. File dir = new  File(strtemp);
  92. dir.mkdirs();
  93. continue ;
  94. else  {
  95. //读写文件
  96. InputStream is = zipFile.getInputStream(zipEnt);
  97. BufferedInputStream bis = new  BufferedInputStream(is);
  98. gbkPath=zipEnt.getName();
  99. strtemp = strPath + File.separator + gbkPath;
  100. //建目录
  101. String strsubdir = gbkPath;
  102. for ( int  i =  0 ; i < strsubdir.length(); i++) {
  103. if (strsubdir.substring(i, i +  1 ).equalsIgnoreCase( "/" )) {
  104. String temp = strPath + File.separator + strsubdir.substring(0 , i);
  105. File subdir = new  File(temp);
  106. if (!subdir.exists())
  107. subdir.mkdir();
  108. }
  109. }
  110. FileOutputStream fos = new  FileOutputStream(strtemp);
  111. BufferedOutputStream bos = new  BufferedOutputStream(fos);
  112. int  c;
  113. while ((c = bis.read()) != - 1 ) {
  114. bos.write((byte ) c);
  115. }
  116. bos.close();
  117. fos.close();
  118. }
  119. }
  120. catch (Exception e) {
  121. e.printStackTrace();
  122. throw  e;
  123. }
  124. }
  125. /**
  126. * 压 缩zip格式的压缩文件
  127. * @param inputFilename 压 缩的文件或文件夹及详细路径
  128. * @param zipFilename 输 出文件名称及详细路径
  129. * @throws IOException
  130. */
  131. public   synchronized   void  zip(String inputFilename, String zipFilename)  throws  IOException {
  132. zip(new  File(inputFilename), zipFilename);
  133. }
  134. /**
  135. * 压 缩zip格式的压缩文件
  136. * @param inputFile 需 压缩文件
  137. * @param zipFilename 输 出文件及详细路径
  138. * @throws IOException
  139. */
  140. public   synchronized   void  zip(File inputFile, String zipFilename)  throws  IOException {
  141. ZipOutputStream out = new  ZipOutputStream( new  FileOutputStream(zipFilename));
  142. try  {
  143. zip(inputFile, out, "" );
  144. catch  (IOException e) {
  145. throw  e;
  146. finally  {
  147. out.close();
  148. }
  149. }
  150. /**
  151. * 压 缩zip格式的压缩文件
  152. * @param inputFile 需 压缩文件
  153. * @param out 输 出压缩文件
  154. * @param base 结 束标识
  155. * @throws IOException
  156. */
  157. @SuppressWarnings ( "unused" )
  158. private   synchronized   void  zip(File inputFile, ZipOutputStream out, String base)  throws  IOException {
  159. if  (inputFile.isDirectory()) {
  160. File[] inputFiles = inputFile.listFiles();
  161. out.putNextEntry(new  ZipEntry(base +  "/" ));
  162. base = base.length() == 0  ?  ""  : base +  "/" ;
  163. for  ( int  i =  0 ; i < inputFiles.length; i++) {
  164. zip(inputFiles[i], out, base + inputFiles[i].getName());
  165. }
  166. else  {
  167. if  (base.length() >  0 ) {
  168. out.putNextEntry(new  ZipEntry(base));
  169. else  {
  170. out.putNextEntry(new  ZipEntry(inputFile.getName()));
  171. }
  172. FileInputStream in = new  FileInputStream(inputFile);
  173. try  {
  174. int  c;
  175. byte [] by =  new   byte [BUFFEREDSIZE];
  176. while  ((c = in.read(by)) != - 1 ) {
  177. out.write(by, 0 , c);
  178. }
  179. catch  (IOException e) {
  180. throw  e;
  181. finally  {
  182. in.close();
  183. }
  184. }
  185. }
  186. /**
  187. * 解 压rar格式的压缩文件到指定目录下
  188. * @param rarFileName 压 缩文件
  189. * @param extPlace 解 压目录
  190. * @throws Exception
  191. */
  192. public   synchronized   void  unrar(String rarFileName, String extPlace)  throws  Exception{
  193. try  {
  194. (new  File(extPlace)).mkdirs();
  195. // 构建测解压缩类
  196. Archive archive = new  Archive();
  197. archive.setEnabledLog(false );  //不输出 日志
  198. // 设置rar文件
  199. archive.setFile(rarFileName);
  200. archive.setExtractPath(extPlace);
  201. archive.extractAllFile();
  202. catch  (Exception e) {
  203. // TODO: handle exception
  204. }
  205. }}

java解压zip与rar相关推荐

  1. php 判断是rar或是zip文件,PHP解压zip和rar文件的方法介绍

    PHP解压zip和rar文件的方法介绍?实例代码举例php语言如何解压文件.php开发经常会遇到解压zip和rar文件,以下方法供大家参考,判断文件是zip类型的还是rar类型,调用相关组件执行解压缩 ...

  2. Linux 下载安装 rar 并解压rar压缩包(Linux下如何解压.zip和.rar文件)

    Linux下如何解压.zip和.rar文件,对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们: 1)对于zip linux下提供了zip和unzip程序,zip是 ...

  3. linux 压缩7z文件夹,Linux 下压缩与解压.zip和.rar及.7z文件

    Linux 下压缩与解压.zip和.rar及.7z文件对于Window下的常见压缩文件.zip和.rar,Linux也有相应的方法来解压它们: 1)对于.zip linux下提供了zip和unzip程 ...

  4. 解压后java文字乱码_怎么解决java解压zip包出现乱码

    怎么解决java解压zip包出现乱码 发布时间:2020-06-23 09:02:42 来源:亿速云 阅读:107 作者:Leah 怎么解决java解压zip包出现乱码?相信很多没有经验的人对此束手无 ...

  5. 解压zip、rar、gz格式文件

    rar解压版本:需要使用0.7,其他版本尝试了,不行,而且rar压缩的时候,也需要指定rar4,高版本不支持 <!--解压rar压缩--><dependency><gro ...

  6. Java解压zip文件(文本)压缩包

    2019独角兽企业重金招聘Python工程师标准>>> 说明:由于我们的日志收集到指定服务器上,会按天压缩成一个zip格式的压缩包,但是有时候需要对这些日志进行处理,人工解压在处理, ...

  7. 使用Python解压zip、rar文件

    解压 zip 文件 基本解压操作 import zipfile''' 基本格式:zipfile.ZipFile(filename[,mode[,compression[,allowZip64]]]) ...

  8. Ubuntu 14 如何解压 .zip、.rar 文件

    .zip 和 .rar 是Windows下常用的压缩文件,在Ubuntu中如何解压. [解压.zip文件] Ubuntu中貌似已经安装了unzip软件,解压命令如下: unzip ./FileName ...

  9. java util zip.zipexc,JAVA解压zip压缩文件的实例

    今天在弄一个东西,需要在PL/SQL中解压zip的压缩包,刚开始的时候是想着直接在PLSQL中调用java,在java里面调用unzip的shell命令来解析压缩文件,但是比较悲剧,一直老是失败,在尝 ...

最新文章

  1. Django整理(二) - 视图和模板的初步使用
  2. mfc程序转化为qt_以“企鹅电竞小程序”改版为例,讲解如何将用户核心路径转化为设计语言...
  3. android+5.0+小米手环,小米手环5和荣耀手环6哪个好-参数对比
  4. linux 编辑器vim配置
  5. BZOJ 4259: 残缺的字符串 [FFT]
  6. 《深入理解 Spring Cloud 与微服务构建》第十五章 微服务监控 Spring Boot Admin
  7. R 梯度提升算法①
  8. 无法连接Elasticsearch解决方案
  9. Centos7 搭建 hadoop3.1.1 集群教程
  10. goback history 传递参数_vue-router go(-1)后退时怎么带参数?
  11. 手把手教你做短视频去水印微信小程序(4-解析结果页)
  12. nowcoder16638 carpet
  13. React高级(五)
  14. 小区宽带网络解决方案
  15. 如何以活动价在官网购买百度网盘会员
  16. 测量耐力也有算法了!仅需锻炼20分钟,就能知晓自己能跑多久
  17. 软件测试工程师岗位个人简历怎么写
  18. EMC测试仪器_EMC测试整改流程及常见问题
  19. 软件压力测试的手段有注入错误吗,软件错误注入测试技术研究_陈锦富.pdf
  20. 欧几里得算法(扩展欧几里得、欧拉定理、费马小定理)

热门文章

  1. 在Latex的$$符号中无法显示数学加号
  2. less的基础用法介绍(快速上手 )
  3. 一加(oneplus)7 pro刷nethunter与Linux 下刷新版Android 9(P)手机(root)
  4. Mono和IL2CPP
  5. CNCF热烈欢迎浪潮升级成为黄金级会员
  6. Problem - 1042D - D. Petya and Array(树状数组)
  7. vue项目 在style标签中引入css,less,sass的方法
  8. MATLAB实现基本的遗传算法(写成函数形式,可调用),优化目标函数,并举例展示
  9. turtle八角图形绘制
  10. java 圈复杂度_控制圈复杂度的9种重构技术