- 浏览: 429404 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (144)
- java (32)
- Flex (11)
- linux (15)
- 思维至上 (2)
- 生活 (4)
- spring2.5.5 (3)
- Design pattern (0)
- jdk1.6 (1)
- erlang (14)
- mysql (5)
- nginx (10)
- 互联网 (1)
- 用户体验 (0)
- jvm (8)
- hibernate (0)
- oracle (0)
- mvn (2)
- 23种常用设计模式详细讲解及实例 (1)
- nosql (1)
- mongodb (2)
- thrift (1)
- 负载均衡 (1)
- Objective-c (1)
- fuck (1)
- resin (3)
- log4j (2)
- android (1)
- jps (0)
- lucene (2)
- rsync (1)
- python (1)
- java性能分析 (1)
- spring (1)
- zookeeper (2)
- error (1)
- 算法 (1)
- hudson (1)
- svn (1)
- maven (1)
- svn server (2)
- swap linux (1)
- 协同过滤 (1)
- 邮件系统 (1)
最新评论
-
cheetah747:
我也遇到这个问题了,不过我是直接把第三方jar放到工程文件的l ...
JAVA NoClassDefFoundError: -
dxm1986:
hillfallshore 写道亲 你这是杜撰的小说吗?完全是 ...
面试有感 -
dxm1986:
mengda1027 写道从实习生到高级java到架构师,是在 ...
面试有感 -
dxm1986:
kljjack 写道一个编程十几年,每年看几十本技术书籍,技术 ...
面试有感 -
dxm1986:
轻指飞扬 写道呵呵,挺好玩的~ 我怎么感觉你招人不是很有诚意嘛 ...
面试有感
这两天,因为项目需要,涉及到文件夹的上传,想了很久,在网上也找了一些资料,都没有什么很好的办法,都是用插件之类的解决,后面就想到了上传压缩文件,说到压缩文件,自然就会遇到,文件的解压缩问题,大家都知道,比较常见的压缩文件有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有比较发解的朋友,能发表一个人见解),
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;
import de.innosystec.unrar.Archive;
/** * * * @version 创建时间:Feb 26, 2009 6:01:11 PM * 类说明:压缩、解压文件公用类 * */ public class Decompression { private static final int BUFFEREDSIZE = 1024; /** * 解压zip格式的压缩文件到指定位置 * @param zipFileName 压缩文件 * @param extPlace 解压目录 * @throws Exception */ @SuppressWarnings("unchecked") public synchronized void unzip(String zipFileName, String extPlace) throws Exception { try { (new File(extPlace)).mkdirs(); File f = new File(zipFileName); ZipFile zipFile = new ZipFile(zipFileName); if((!f.exists()) && (f.length() <= 0)) { throw new Exception("要解压的文件不存在!"); } String strPath, gbkPath, strtemp; File tempFile = new File(extPlace); strPath = tempFile.getAbsolutePath(); java.util.Enumeration e = zipFile.getEntries(); while(e.hasMoreElements()){ org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement(); gbkPath=zipEnt.getName(); if(zipEnt.isDirectory()){ strtemp = strPath + File.separator + gbkPath; File dir = new File(strtemp); dir.mkdirs(); continue; } else { //读写文件 InputStream is = zipFile.getInputStream(zipEnt); BufferedInputStream bis = new BufferedInputStream(is); gbkPath=zipEnt.getName(); strtemp = strPath + File.separator + gbkPath; //建目录 String strsubdir = gbkPath; for(int i = 0; i < strsubdir.length(); i++) { if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) { String temp = strPath + File.separator + strsubdir.substring(0, i); File subdir = new File(temp); if(!subdir.exists()) subdir.mkdir(); } } FileOutputStream fos = new FileOutputStream(strtemp); BufferedOutputStream bos = new BufferedOutputStream(fos); int c; while((c = bis.read()) != -1) { bos.write((byte) c); } bos.close(); fos.close(); } } } catch(Exception e) { e.printStackTrace(); throw e; } } /** * 解压zip格式的压缩文件到指定位置 * @param zipFileName 压缩文件 * @param extPlace 解压目录 * @throws Exception */ @SuppressWarnings("unchecked") public synchronized void unzip(String zipFileName, String extPlace,boolean whether) throws Exception { try { (new File(extPlace)).mkdirs(); File f = new File(zipFileName); ZipFile zipFile = new ZipFile(zipFileName); if((!f.exists()) && (f.length() <= 0)) { throw new Exception("要解压的文件不存在!"); } String strPath, gbkPath, strtemp; File tempFile = new File(extPlace); strPath = tempFile.getAbsolutePath(); java.util.Enumeration e = zipFile.getEntries(); while(e.hasMoreElements()){ org.apache.tools.zip.ZipEntry zipEnt = (ZipEntry) e.nextElement(); gbkPath=zipEnt.getName(); if(zipEnt.isDirectory()){ strtemp = strPath + File.separator + gbkPath; File dir = new File(strtemp); dir.mkdirs(); continue; } else { //读写文件 InputStream is = zipFile.getInputStream(zipEnt); BufferedInputStream bis = new BufferedInputStream(is); gbkPath=zipEnt.getName(); strtemp = strPath + File.separator + gbkPath; //建目录 String strsubdir = gbkPath; for(int i = 0; i < strsubdir.length(); i++) { if(strsubdir.substring(i, i + 1).equalsIgnoreCase("/")) { String temp = strPath + File.separator + strsubdir.substring(0, i); File subdir = new File(temp); if(!subdir.exists()) subdir.mkdir(); } } FileOutputStream fos = new FileOutputStream(strtemp); BufferedOutputStream bos = new BufferedOutputStream(fos); int c; while((c = bis.read()) != -1) { bos.write((byte) c); } bos.close(); fos.close(); } } } catch(Exception e) { e.printStackTrace(); throw e; } } /** * 压缩zip格式的压缩文件 * @param inputFilename 压缩的文件或文件夹及详细路径 * @param zipFilename 输出文件名称及详细路径 * @throws IOException */ public synchronized void zip(String inputFilename, String zipFilename) throws IOException { zip(new File(inputFilename), zipFilename); } /** * 压缩zip格式的压缩文件 * @param inputFile 需压缩文件 * @param zipFilename 输出文件及详细路径 * @throws IOException */ public synchronized void zip(File inputFile, String zipFilename) throws IOException { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename)); try { zip(inputFile, out, ""); } catch (IOException e) { throw e; } finally { out.close(); } } /** * 压缩zip格式的压缩文件 * @param inputFile 需压缩文件 * @param out 输出压缩文件 * @param base 结束标识 * @throws IOException */ @SuppressWarnings("unused") private synchronized void zip(File inputFile, ZipOutputStream out, String base) throws IOException { if (inputFile.isDirectory()) { File[] inputFiles = inputFile.listFiles(); out.putNextEntry(new ZipEntry(base + "/")); base = base.length() == 0 ? "" : base + "/"; for (int i = 0; i < inputFiles.length; i++) { zip(inputFiles[i], out, base + inputFiles[i].getName()); } } else { if (base.length() > 0) { out.putNextEntry(new ZipEntry(base)); } else { out.putNextEntry(new ZipEntry(inputFile.getName())); } FileInputStream in = new FileInputStream(inputFile); try { int c; byte[] by = new byte[BUFFEREDSIZE]; while ((c = in.read(by)) != -1) { out.write(by, 0, c); } } catch (IOException e) { throw e; } finally { in.close(); } } } /** * 解压rar格式的压缩文件到指定目录下 * @param rarFileName 压缩文件 * @param extPlace 解压目录 * @throws Exception */ public synchronized void unrar(String rarFileName, String extPlace) throws Exception{ try { (new File(extPlace)).mkdirs(); // 构建测解压缩类 Archive archive = new Archive(); archive.setEnabledLog(false); //不输出日志 // 设置rar文件 archive.setFile(rarFileName); archive.setExtractPath(extPlace); archive.extractAllFile(); } catch (Exception e) { // TODO: handle exception } }}
(下面附ant,unrar相关的jar包)
- java-unrar.zip (505.9 KB)
- 描述: (解压rar所需的jar包)
- 下载次数: 1860
- ant.jar (1.2 MB)
- 描述: 用于解压zip的jar包
- 下载次数: 1060
评论
2 楼
mbyd
2011-08-10
引用
[align=right][/align]
1 楼
wofeng
2010-11-07
Archive archive = new Archive(); archive.setEnabledLog(false); //不输出日志 // 设置rar文件 archive.setFile(rarFileName); archive.setExtractPath(extPlace); archive.extractAllFile();
Archive 没有这些方法吧。
-------------------------------
在linux下 用java-unrar 会乱码,能研究一下吗?
发表评论
-
what the fucking code
2012-04-12 14:08 1133public class MemInfo<T ex ... -
Jps介绍以及解决jps无法查看某个已经启动的java进程问题
2012-03-07 16:41 0Jps介绍以及解决jps无法查看某个已经启 ... -
java.lang.UnsupportedOperationException
2012-02-17 15:01 2119在使用Arrays.asList()后调用add,r ... -
设计模式
2011-10-09 11:05 91523种常用设计模式详细 ... -
JVM调优与监控
2011-09-17 10:57 1655随时记录,方便以后 ... -
A threadpool with limited cache
2011-09-14 10:13 1269As we all know ,we use the ... -
JVM监控工具介绍jstack, jconsole, jinfo, jmap, jdb, jsta
2011-09-10 13:03 1607Java 内存泄露监控工具(一) -- JV ... -
两个shell脚本,希望大家能够用上
2011-09-08 14:46 1090第一个 定时抓取服务器JVM相关信息 #! /bin/s ... -
责任链
2011-09-05 18:26 938其实我一直都不太喜欢去死记硬背那些神马设计模式,但不得不说,你 ... -
jstat的使用方法
2011-08-30 23:30 7653jstat的使用方法 http: ... -
JVM参数配置大全
2011-08-07 09:42 1210JVM参数配置大全[转] 2010-01-14 ... -
JAVA NoClassDefFoundError:
2011-07-24 05:31 49556NoClassDefFoundError: 我 ... -
单实例设计模式
2011-07-15 15:30 787今天讲一下单实例模式 A: public ... -
java关键字:Volatile,Transient,strictfp
2011-06-27 10:04 837Volatile修饰的成员变量 ... -
使用JDK 1.6自带的jvisualvm监控远程服务器中Java程序资源占用情况
2011-05-06 09:36 7688使用JDK 1.6自带的jvisualvm监控远程 ... -
java虚拟机参数设置
2011-05-05 20:07 1122java虚拟机参数设置 收藏 ... -
JAVA性能优化—Sun Hotspot JDK JVM参数设置
2011-05-05 18:31 1270JAVA性能优化—Sun Hotspot JDK JVM参 ... -
[转]JVM内存段分配及启动参数
2011-05-05 17:39 21761. JVM内存段分配及启动参数:J2EE服务器的内 ... -
Jconsole
2011-05-04 15:06 1368Jconsole是JDK自带的一个很好的jvm查看工具 ... -
[转]java.lang.OutOfMemoryError
2011-04-29 16:36 3604http://www.blogjava.net/ld ...
相关推荐
java 解压zip,rar文件 ,java 解压缩包,把压缩包的文件及文件夹的子文件全部压到指定目录
使用Java解压文件 支持RAR5,Zip,7z ,代码只实现了这个三种,但是要想支持更多的可以查看代码。 支持跨平台。 以前的通用java解压都是坑人的,无语了。
java上传文件 实时获取上传进度 上传RAR,ZIP 并指定解压路劲 上传之后会按照路劲进行解压
java实现的解压与压缩的类 其中有zip和rar类型的 还包括了所需的架包
Java解压ZIP和RAR格式文件_所需资源,本资源是博客中所需文件,故不要过多积分,以方便技术人员解决问题,而且方便下载。
使用该jar包可对zip,rar,7z进行压缩和解压操作 使用该jar包可对zip,rar,7z进行压缩和解压操作
java ZIP和RAR 压缩包 目录结构。 1.ant.jar 解决java自带zip不能读取中文压缩包的问题; 2.需要安装WINRAR软件,以便解压rar文件,然后获取对应目录; 3.实现在线预览压缩包目录结构的功能;
java解压zip或rar压缩文件源码和jar包
java rar解压 zip解压 工具
Java批量解压RAR或zip文件,压缩包内包含包括源码和jar包。 如发现其他人或组织有商用目的,必追究其法律责任。
java解压zip或rar压缩文件(源码和jar包)
NULL 博文链接:https://hogwartsrow.iteye.com/blog/1853355
完美 解 压 缩 zip , rar, 完美使用,可直 接 使 用.
java 压缩/解压 .zip/.rar/.tar 文件,大家一起讨论,学习 已实现zip压缩/解压,rar解压
java项目中常使用到的工具类:zip压缩解压缩、rar解压(有密码的文件)、gz解压、FTP上传与下载
本篇文章主要介绍了详解Java无需解压直接读取Zip文件和文件内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
Java解压RAR文件,压缩包内包含源码、Jar包、依赖包和示例程序,也可以从http://code.google.com/p/java-unrar/downloads/list这里下载支持密码的unrar项目(包括源码和jar包),其依赖包也很容易搜到。
在使用Java对ZIP压缩文件进行解压的方式中有两种,一种是使用apache提供的ant.jar工具包,但是如果ZIP文件中含有中文名称的文件,在解压后,文件名将出现乱码,另一种是使用Java自身JDK中java.util.zip包下工具类,...