compression - How to print the content of a tar.gz file with Java? -
i have implement application permits printing content of files within tar.gz file.
for example:
if have 3 files in folder called testx:
a.txt contains words "god save queen"
b.txt contains words "ubi maior, minor cessat"
c.txt.gz file compressed gzip contain file c.txt words "hello america!!"
so compress testx, obtain compressed tar file: testx.tar.gz.
so java application print in console:
"god save queen"
"ubi maior, minor cessat"
"hello america!!"
i have implemented zip version , works well, keeping tar library apache ant http://commons.apache.org/compress/, noticed not easy zip java utils.
could me?
i have started looking on net understand how accomplish aim, have following code:
gzipinputstream gzipinputstream=null; gzipinputstream = new gzipinputstream( new fileinputstream(filename)); tarinputstream = new tarinputstream(gzipinputstream); tarentry entryx = null; while((entryx = is.getnextentry()) != null) { if (entryx.isdirectory()) continue; else { system.out.println(entryx.getname()); if ( entryx.getname().endswith("txt.gz")){ is.copyentrycontents(out); // out outputstream!! } } }
so in line is.copyentrycontents(out)
, possible save on file stream passing outputstream, don't want it! in zip version after keeping first entry, zipentry, can extract stream compressed root folder, testx.tar.gz, , create new zipinputstream , play obtain content.
is possible tar.gz file?
thanks.
surfing net, have encountered interesting idea @ : http://hype-free.blogspot.com/2009/10/using-tarinputstream-from-java.html.
after converting ours tarentry stream, can adopt same idea used zip files like:
inputstream tmpin = new streamingtarentry(is, entryx.getsize()); // use bufferedreader 1 line @ time bufferedreader gzipreader = new bufferedreader( new inputstreamreader( new gzipinputstream( inputzip ))); while (gzipreader.ready()) { system.out.println(gzipreader.readline()); } gzipreader.close();
so code print content of file testx.tar.gz ^_^
Comments
Post a Comment