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