700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java解压zip与rar

java解压zip与rar

时间:2018-10-27 23:58:04

相关推荐

java解压zip与rar

这两天,因为项目需要,涉及到文件夹的上传,想了很久,在网上也找了一些资料,都没有什么很好的办法,都是用插件之类的解决,后面就想到了上传压缩 文件,说到压缩文件,自然就会遇到,文件的解压缩问题,大家都知道,比较常见的压缩文件有rar,zip,然而rar,zip的区别又在哪?说一 点,zip 压缩算法是免费开放的,任何人可以免费使用。但是 RAR 就不一样了,

这个压缩算法已经受到专利权的保护,如果要使用 RAR 算法必须向其专利所有人支付费用。所以在一般的开源网站,像apache,sourceforge等开源网站上的开源项目一般都用zip格式.本人所学语 言主要为java自然会想用java去解压,用java去解压zip比较容易,有apache提供的开源项目ant,我在网上找一下,找到了 sourceforge的开源项目unrar专用于压缩,解压rar.只可惜没有文档(让人即喜,又悲).

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

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

Java代码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 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代码import de.innosystec.unrar.Archive;

import de.innosystec.unrar.Archive;

Java代码/** ** *@version创建时间:Feb26,6:01:11PM *类说明:压缩、解压文件公用类 * */ public class Decompression{ private static final int BUFFEREDSIZE= 1024 ; /** *解 压zip格式的压缩文件到指定位置 *@paramzipFileName压 缩文件 *@paramextPlace解 压目录 *@throwsException */ @SuppressWarnings ( "unchecked" ) public synchronized void unzip(StringzipFileName,StringextPlace) throws Exception{ try { (new File(extPlace)).mkdirs(); Filef=new File(zipFileName); ZipFilezipFile=new ZipFile(zipFileName); if ((!f.exists())&&(f.length()<= 0 )){ throw new Exception( "要解压的文件不存在!" ); } StringstrPath,gbkPath,strtemp; FiletempFile=new File(extPlace); strPath=tempFile.getAbsolutePath(); java.util.Enumeratione=zipFile.getEntries(); while (e.hasMoreElements()){ org.apache.tools.zip.ZipEntryzipEnt=(ZipEntry)e.nextElement(); gbkPath=zipEnt.getName(); if (zipEnt.isDirectory()){ strtemp=strPath+File.separator+gbkPath; Filedir=new File(strtemp); dir.mkdirs(); continue ; }else { //读写文件 InputStreamis=zipFile.getInputStream(zipEnt); BufferedInputStreambis=new BufferedInputStream(is); gbkPath=zipEnt.getName(); strtemp=strPath+File.separator+gbkPath; //建目录 Stringstrsubdir=gbkPath; for ( int i= 0 ;i<strsubdir.length();i++){ if (strsubdir.substring(i,i+ 1 ).equalsIgnoreCase( "/" )){ Stringtemp=strPath+File.separator+strsubdir.substring(0 ,i); Filesubdir=new File(temp); if (!subdir.exists()) subdir.mkdir(); } } FileOutputStreamfos=new FileOutputStream(strtemp); BufferedOutputStreambos=new BufferedOutputStream(fos); int c; while ((c=bis.read())!=- 1 ){ bos.write((byte )c); } bos.close(); fos.close(); } } }catch (Exceptione){ e.printStackTrace(); throw e; } } /** *解 压zip格式的压缩文件到指定位置 *@paramzipFileName压 缩文件 *@paramextPlace解 压目录 *@throwsException */ @SuppressWarnings ( "unchecked" ) public synchronized void unzip(StringzipFileName,StringextPlace, boolean whether) throws Exception{ try { (new File(extPlace)).mkdirs(); Filef=new File(zipFileName); ZipFilezipFile=new ZipFile(zipFileName); if ((!f.exists())&&(f.length()<= 0 )){ throw new Exception( "要解压的文件不存在!" ); } StringstrPath,gbkPath,strtemp; FiletempFile=new File(extPlace); strPath=tempFile.getAbsolutePath(); java.util.Enumeratione=zipFile.getEntries(); while (e.hasMoreElements()){ org.apache.tools.zip.ZipEntryzipEnt=(ZipEntry)e.nextElement(); gbkPath=zipEnt.getName(); if (zipEnt.isDirectory()){ strtemp=strPath+File.separator+gbkPath; Filedir=new File(strtemp); dir.mkdirs(); continue ; }else { //读写文件 InputStreamis=zipFile.getInputStream(zipEnt); BufferedInputStreambis=new BufferedInputStream(is); gbkPath=zipEnt.getName(); strtemp=strPath+File.separator+gbkPath; //建目录 Stringstrsubdir=gbkPath; for ( int i= 0 ;i<strsubdir.length();i++){ if (strsubdir.substring(i,i+ 1 ).equalsIgnoreCase( "/" )){ Stringtemp=strPath+File.separator+strsubdir.substring(0 ,i); Filesubdir=new File(temp); if (!subdir.exists()) subdir.mkdir(); } } FileOutputStreamfos=new FileOutputStream(strtemp); BufferedOutputStreambos=new BufferedOutputStream(fos); int c; while ((c=bis.read())!=- 1 ){ bos.write((byte )c); } bos.close(); fos.close(); } } }catch (Exceptione){ e.printStackTrace(); throw e; } } /** *压 缩zip格式的压缩文件 *@paraminputFilename压 缩的文件或文件夹及详细路径 *@paramzipFilename输 出文件名称及详细路径 *@throwsIOException */ public synchronized void zip(StringinputFilename,StringzipFilename) throws IOException{ zip(new File(inputFilename),zipFilename); } /** *压 缩zip格式的压缩文件 *@paraminputFile需 压缩文件 *@paramzipFilename输 出文件及详细路径 *@throwsIOException */ public synchronized void zip(FileinputFile,StringzipFilename) throws IOException{ ZipOutputStreamout=new ZipOutputStream( new FileOutputStream(zipFilename)); try { zip(inputFile,out,"" ); }catch (IOExceptione){ throw e; }finally { out.close(); } } /** *压 缩zip格式的压缩文件 *@paraminputFile需 压缩文件 *@paramout输 出压缩文件 *@parambase结 束标识 *@throwsIOException */ @SuppressWarnings ( "unused" ) private synchronized void zip(FileinputFile,ZipOutputStreamout,Stringbase) 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())); } FileInputStreamin=new FileInputStream(inputFile); try { int c; byte []by= new byte [BUFFEREDSIZE]; while ((c=in.read(by))!=- 1 ){ out.write(by,0 ,c); } }catch (IOExceptione){ throw e; }finally { in.close(); } } } /** *解 压rar格式的压缩文件到指定目录下 *@paramrarFileName压 缩文件 *@paramextPlace解 压目录 *@throwsException */ public synchronized void unrar(StringrarFileName,StringextPlace) throws Exception{ try { (new File(extPlace)).mkdirs(); //构建测解压缩类 Archivearchive=new Archive(); archive.setEnabledLog(false ); //不输出 日志 //设置rar文件 archive.setFile(rarFileName); archive.setExtractPath(extPlace); archive.extractAllFile(); }catch (Exceptione){ //TODO:handleexception } }}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。