Reading gzip files inside gzip file using Java -
using java have read text files inside gz file in .tar.gz
gz_ltm_logs.tar.gz filename. has files ltm.1.gz, ltm.2.gz inside , these files have text files in them.
i wanted using java.util.zip.* if impossible can @ other libraries. thought able using java.util.zip. doesn't seem straightforward
here's code give idea. method try extract given tar.gz file outputfolder.
public static void extract(file input, file outputfolder) throws ioexception { byte[] buffer = new byte[1024]; gzipinputstream gzipfile = new gzipinputstream(new fileinputstream(input)); byteoutputstream tarstream = new byteoutputstream(); int gziplengthread; while ((gziplengthread = gzipfile.read(buffer)) > 0){ tarstream.write(buffer, 0, gziplengthread); } gzipfile.close(); org.apache.tools.tar.tarinputstream tarfile = null; // files inside tar outputstream out = null; try { tarfile = new org.apache.tools.tar.tarinputstream(tarstream.newinputstream()); tarstream.close(); tarentry entry = null; while ((entry = tarfile.getnextentry()) != null) { string outfilename = entry.getname(); if (entry.isdirectory()) { file directory = new file(outputfolder, outfilename); directory.mkdirs(); } else { file outputfile = new file(outputfolder, outfilename); file outputdirectory = outputfile.getparentfile(); if (!outputdirectory.exists()) { outputdirectory.mkdirs(); } out = new fileoutputstream(outputfile); // transfer bytes tarfile output file int innerlen; while ((innerlen = tarfile.read(buffer)) > 0) { out.write(buffer, 0, innerlen); } out.close(); } } } { if (tarfile != null) { tarfile.close(); } if (out != null) { out.close(); } } }
Comments
Post a Comment