ImportOrg.apache.commons.compress.archivers.tar.TarArchiveEntry;ImportOrg.apache.commons.compress.archivers.tar.TarArchiveInputStream;ImportOrg.apache.commons.compress.compressors.CompressorInputStream;ImportOrg.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;Importorg.apache.commons.io.FileUtils;Importorg.apache.commons.io.IOUtils;ImportOrg.apache.commons.io.LineIterator;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportJava.io.*;ImportJava.nio.charset.Charset; Public classIohelper { Public Static FinalLogger Logger = Loggerfactory.getlogger (iohelper.class); Public Static voidUncompresstargz (file tarfile, file dest)throwsIOException {BooleanMkdirs =Dest.mkdirs (); if(!mkdirs) {Logger.warn ("Unable to create directory ' {} '", Dest.getabsolutepath ()); return; } bufferedinputstream InputStream=NewBufferedinputstream (NewFileInputStream (tarfile)); Gzipcompressorinputstream Gcis=NewGzipcompressorinputstream (InputStream); Try(Tararchiveinputstream Tais =NewTararchiveinputstream (Gcis)) {Tararchiveentry entry; while((Entry = Tais.getnexttarentry ())! =NULL) {//create a file with the same name as the entryFile Desfile =NewFile (dest, Entry.getname ()); if(Entry.isdirectory ()) {BooleanMkdirs =Desfile.mkdirs (); if(!mkdirs) {Logger.warn ("Unable to create directory ' {} '", Desfile.getabsolutepath ()); } } Else { BooleanCreateNewFile =Desfile.createnewfile (); if(!createnewfile) {Logger.warn ("Unable to create file ' {} '", Desfile.getcanonicalpath ()); Continue; } Try(Bufferedoutputstream BOS =NewBufferedoutputstream (NewFileOutputStream (desfile));) {//ioutils.copy (Tais, BOS); byte[] Btoread =New byte[1024]; intLen; while(len = Tais.read (btoread))! =-1) {bos.write (Btoread,0, Len); } }}} logger.info ("Untar completed successfully!"); } } Public Static voidPrinttargzfile (File tarfile)throwsIOException {Bufferedinputstream bin=NewBufferedinputstream (Fileutils.openinputstream (tarfile)); Compressorinputstream CIS=NewGzipcompressorinputstream (BIN); Try(Tararchiveinputstream Tais =NewTararchiveinputstream (CIS)) {Tararchiveentry entry; while((Entry = Tais.getnexttarentry ())! =NULL) { if(Entry.isdirectory ()) {Logger.warn ("Dir:{}", Entry.getname ()); } Else { intSize = (int) entry.getsize (); byte[] content =New byte[size]; intReadcount = tais.read (content, 0, size); Logger.info ("Filename:{}", Entry.getname ()); Bytearrayinputstream Bytearrayinputstream=NewBytearrayinputstream (content, 0, Readcount); Lineiterator iterator= Ioutils.lineiterator (Bytearrayinputstream, Charset.forname ("Utf-8")); Try { while(Iterator.hasnext ()) {Logger.info ("Line:{}", Iterator.nextline ()); } } finally{lineiterator.closequietly (iterator); }}} logger.info ("===============finish==============="); } }}
Https://commons.apache.org/proper/commons-compress/examples.html
Http://stackoverflow.com/questions/7128171/how-to-compress-decompress-tar-gz-files-in-java
Https://commons.apache.org/proper/commons-io/description.html
How to Untar a TAR file using Apache Commons