"Go" Java unzip. Tar.z and. zip files

Source: Internet
Author: User
Tags uncompress

Extract. ZIP file

 PackageApp.qdupr.Method;ImportJava.io.File;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.util.Enumeration;ImportOrg.apache.tools.zip.ZipEntry;ImportOrg.apache.tools.zip.ZipFile;/*** Zip file extraction *@authorAdministrator **/ Public classzipuncompress {Private Static intBufSize = 8096;//size of Byte   Public Static voidMain (string[] args)throwsException {String tmpfile= "C:\\users\\administrator\\desktop\\20151101.rar"; Zipuncompress.unzip (Tmpfile, tmpfile.substring (0, Tmpfile.lastindexof (".")))); // -----------------  System.out.println ("Usage:"); System.out.println ("Unzip: Java zipfileutils-unzip filename.zip");      System.out.println (); }    Public Staticstring UnZip (String unzipfilename, String destfilename) {File unzipfile=NewFile (unzipfilename); if(destFileName = =NULL|| Destfilename.trim (). Length () = = 0) {destFileName=unzipfile.getparent ();          } File DestFile; ZipFile ZipFile=NULL; Try{ZipFile=NewZipFile (Unzipfile, "GBK");  for(Enumeration entries =zipfile.getentries (); entries.hasmoreelements ();) {ZipEntry entry=(ZipEntry) entries.nextelement (); DestFile=NewFile (destFileName, Entry.getname ()); Unzipfile (DestFile, ZipFile, entry); //Perform decompression            }          } Catch(Exception e) {returnE.getmessage (); }          finally          {              Try              {                  assertZipFile! =NULL;              Zipfile.close (); } Catch(Exception e) {e.printstacktrace (); }          }          return NULL; }   Private Static voidUnzipfile (File destfile, ZipFile ZipFile, zipentry entry)throwsIOException {InputStream inputstream;          FileOutputStream fileout; if(Entry.isdirectory ())//is a directory, the creation of the{destfile.mkdirs (); }          Else //is a file        {              //if the parent directory of the specified file does not exist, it is created. File parent =Destfile.getparentfile (); if(Parent! =NULL&&!parent.exists ())              {Parent.mkdirs (); } InputStream=Zipfile.getinputstream (entry); Fileout=NewFileOutputStream (destfile); byte[] buf =New byte[BufSize]; intreadedbytes;  while((Readedbytes = Inputstream.read (buf)) > 0) {fileout.write (buf,0, readedbytes);                } fileout.close ();          Inputstream.close (); }      } }     
Unzip. TAR. Z File
Required Jar Pack commons-compress-1.7 and above
 PackageApp.qdupr.Method;ImportJava.io.BufferedInputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportOrg.apache.commons.compress.archivers.tar.TarArchiveEntry;ImportOrg.apache.commons.compress.archivers.tar.TarArchiveInputStream;ImportOrg.apache.commons.compress.compressors.z.zcompressorinputstream;/*** Automatic decompression. Z-formatted files *@authorAdministrator **/  Public classzcompress {/*** Test method *@paramargs*/ Public Static voidMain (string[] args) {zcompress zip=Newzcompress (); File dir=NewFile ("E:\\test"); File[] Subs=Dir.listfiles ();  for(File sub:subs) {String file=sub.getparent () +file.separator+Sub.getname ();      zip.uncompress (file);   System.out.println (file); }}  /*** Get all Files under folder *@paramfile*/ Public Static voidfindallfile (String file) {zcompress zip=Newzcompress (); File dir=Newfile (file); File[] Subs=Dir.listfiles ();  for(File sub:subs) {String NewFile=sub.getparent () +file.separator+Sub.getname ();   Zip.uncompress (NewFile); }}/** * . Z File extraction *@paramfile*/ Public  voiduncompress (String file) {//File File =new file ("e:\\bea20151101. Z ");zcompress.decompresstzfile (file);}/*** Convert String type to file type *@paramfile *@return */ Public Staticfile Decompresstzfile (String file) {returnDecompresszfile (Newfile);/*** Perform decompression *@paramfile *@return */Private Staticfile decompresszfile (file file) {intBufferSize = 2048; FileOutputStream out=NULL; Zcompressorinputstream ZIn=NULL;Try{fileinputstream fin=Newfileinputstream (file); Bufferedinputstream in=NewBufferedinputstream (Fin);//file storage path and filename after decompressionString name = File.getname (). substring (0, File.getname (). IndexOf (".")); File OutFile=NewFile ("e:\\file\\" +name); out=NewFileOutputStream (outFile); ZIn=NewZcompressorinputstream (in);Final byte[] buffer =New byte[buffersize];intn = 0; while( -1! = (n =zin.read (buffer))) {out.write (buffer,0, n);}returnOutFile;} Catch(Exception e) {e.printstacktrace ();return NULL;} finally {Try{out.close (); Zin.close ();}Catch(IOException e) {e.printstacktrace ();}}} /** * . Tar format File decompression *@paramfile*/Private Static voiddecompresstarfile (file file) {intBufferSize = 2048; String BasePath= File.getparent () +File.separator; Tararchiveinputstream is=NULL;Try{ is=NewTararchiveinputstream (Newfileinputstream (file)); while(true) {Tararchiveentry entry=is.getnexttarentry ();if(Entry = =NULL) { Break;}if(Entry.isdirectory ()) {//It doesn't seem to work here, it's a little different from zipentry.NewFile (BasePath +entry.getname ()). Mkdirs ();} Else{fileoutputstream OS=NULL;Try{File F=NewFile (BasePath +entry.getname ());if(!F.getparentfile (). exists ()) {F.getparentfile (). Mkdirs ();}if(!f.exists ()) {F.createnewfile ();} OS=NewFileOutputStream (f);byte[] bs =New byte[buffersize];intLen =-1; while(len = Is.read (BS))! =-1) {os.write (BS,0, Len);} Os.flush ();} Catch(Exception e) {e.printstacktrace ();}finally{os.close ();}}}} Catch(Exception e) {e.printstacktrace ();}finally {Try{is.close (); File.delete ();}Catch(IOException e) {e.printstacktrace ();}} } }

"Go" Java unzip. Tar.z and. zip files

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.