Java decompression zip, rar file

Source: Internet
Author: User
Tags unzip rar

Unzip zip:

maven地址:<groupId>org.apache.ant</groupId>    <artifactId>ant</artifactId>    <version>1.9.7</version></dependency>
 Public Static void Unzipfile(String zipfilename) throws Exception {Try{File F =NewFile (Zipfilename); ZipFile ZipFile =NewZipFile (Zipfilename,"GBK");//windows to use GBK otherwise file name Chinese garbled    if((!f.exists ()) && (F.length () <=0)) {Throw NewException ("The file you want to unzip does not exist!");    } String strpath, Gbkpath, strtemp; File tempfile =NewFile (F.getparent ());    strpath = Tempfile.getabsolutepath (); Enumeration E = Zipfile.getentries (); while(E.hasmoreelements ())    {ZipEntry zipent = (zipentry) e.nextelement (); Gbkpath=zipent.getname ();if(Zipent.isdirectory ()) {strtemp = strpath +"/"+ Gbkpath; File dir =NewFile (strtemp); Dir.mkdirs ();Continue; }Else{//read/write FilesInputStream is= Zipfile.getinputstream (zipent); Bufferedinputstream bis =NewBufferedinputstream ( is);    Gbkpath=zipent.getname (); strtemp = strpath +"/"+ Gbkpath;//Build catalogueString strsubdir = Gbkpath; for(inti =0; I < strsubdir.length (); i++) {if(Strsubdir.substring (i, i +1). Equalsignorecase ("/") {String temp = strpath +"/"+ strsubdir.substring (0, i); File subdir =NewFile (temp);if(!subdir.exists ()) Subdir.mkdir (); }} FileOutputStream fos =NewFileOutputStream (strtemp); Bufferedoutputstream BOS =NewBufferedoutputstream (FOS);intC while((c = Bis.read ())! =-1) {Bos.write (byte) c);    } bos.close ();    Fos.close (); }    }    }Catch(Exception e) {E.printstacktrace ();ThrowE }    }

Unzip RAR:

<dependency>    <groupId>com.github.junrar</groupId>    <artifactId>junrar</artifactId>    <version>0.7</version></dependency>
/** * Extract RAR format compressed file to the specified directory * @param rarfilename Compressed file * @param extplace Extract directory * @thr OWS Exception * /     Public Static void Unrar(String rarfilename, String extplace)throwsexception{Try{File rar =NewFile (Rarfilename); File Destinationfolder =NewFile (Extplace); Extractarchive extractarchive =NewExtractarchive ();          Extractarchive.extractarchive (RAR, destinationfolder); }Catch(Exception e)        {E.printstacktrace (); }    }

Java decompression zip, rar file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.