tgz Decompression of Java files

Source: Internet
Author: User

 Packagecom.alibaba.intl.batch.dependency;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportOrg.apache.commons.compress.archivers.tar.TarArchiveEntry;ImportOrg.apache.commons.compress.archivers.tar.TarArchiveInputStream;ImportOrg.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;Importorg.apache.commons.compress.utils.IOUtils;Importorg.apache.commons.lang3.StringUtils;/*** Unzip Tool class *@authorHollis **/ Public classPackdecompressor { Public Static intBuffer_size = 2048;  Public Static voidMain (string[] args)throwsException {untargz ("/home/hollis/downloads/a.tgz", "/home/hollis/downloads/a"); }     Public Static voidUntargz (String file,string DestDir)throwsexception{File tarfile=Newfile (file);   Untargz (Tarfile, DestDir); }     Public Static voidUntargz (File tarfile,string DestDir)throwsexception{if(Stringutils.isblank (DestDir)) {DestDir=tarfile.getparent (); } DestDir= Destdir.endswith (file.separator)? Destdir:destdir +File.separator; Untar (NewGzipcompressorinputstream (NewFileInputStream (Tarfile)), DestDir); }      Private Static voidUntar (InputStream InputStream, String DestDir)throwsException {tararchiveinputstream Tarin=NewTararchiveinputstream (InputStream, buffer_size); Tararchiveentry entry=NULL; Try {            while((Entry = Tarin.getnexttarentry ())! =NULL) {               if(Entry.isdirectory ()) {//is the directoryCreateDirectory (DestDir, Entry.getname ());//Create an empty directory}Else{//is a fileFile tmpfile =NewFile (DestDir + file.separator +entry.getname ()); CreateDirectory (Tmpfile.getparent ()+ File.separator,NULL);//Create an Output directoryOutputStream out =NULL; Try{ out=NewFileOutputStream (tmpfile); intLength = 0; byte[] B =New byte[2048];  while(length = Tarin.read (b))! =-1) {Out.write (b,0, length); }                   } finally{ioutils.closequietly (out); }               }           }       } Catch(Exception e) {e.printstacktrace (); Throwe; } finally{ioutils.closequietly (Tarin); }   }    Public Static voidcreatedirectory (String outputdir,string subdir) {File file=NewFile (OutputDir); if(! (SubDir = =NULL|| Subdir.trim (). Equals (""))) {//Sub-directory is not emptyFile =NewFile (OutputDir + file.separator +subdir); }         if(!file.exists ())         {File.mkdirs (); }     }  }

tgz Decompression of Java files

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.