关于java解压zip与rar的问题
来源:互联网 发布:能听金庸小说的软件 编辑:程序博客网 时间:2024/05/16 09:30
这两天,因为项目需要,涉及到文件夹的上传,想了很久,在网上也找了一些资料,都没有什么很好的办法,都是用插件之类的解决,后面就想到了上传压缩文件,说到压缩文件,自然就会遇到,文件的解压缩问题,大家都知道,比较常见的压缩文件有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
- }
- }}
- 关于java解压zip与rar的问题
- java解压zip与rar
- java解压rar.zip
- zip rar的解压
- java zip和rar压缩文件的解压
- java解压zip,rar的代码
- java解压rar以及zip
- Java解压ZIP、RAR文件
- Java 解压rar 解压缩zip
- java 解压zip、rar文件
- java之解压zip,rar
- JAVA 解压ZIP RAR文件
- ubuntu下解压zip rar中文字符乱码的问题
- Java解压zip和rar文件
- Java 解压zip和rar文件
- java解压zip或者rar压缩文件
- java解压zip、rar(多级文件)
- java.util.zip包无法正常解压rar软件压缩的rar文件
- C++的内存回收【转】
- 使用javascript读取Cookie值 - JQuery Cookie的用法
- W. :利用最小优先级队列实现对k个已序队列的合并排序。习题6.5-8
- vc 连接mdb数据库
- WebKit加载网页的流程(dlmu2001)
- 关于java解压zip与rar的问题
- windows下获取本机所有IP
- IE9 下载管理,轻松您的下载
- 有关 TDataLink 的说明文件
- 微软Chart控件导学--渲染图表
- IE9的 InPrivate 和 SmartScreen 功能大揭秘
- larbin中两个wait队列的功能
- eclipse中编程创建一个java项目
- 关于Linux的若干介绍