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