如何打印带有Java的tar.gz文件的内容? [英] How to print the content of a tar.gz file with Java?
问题描述
我必须实现允许打印tar.gz文件中所有文件内容的应用程序。
I have to implement an application that permits printing the content of all files within a tar.gz file.
例如:
if我在一个名为testx的文件夹中有三个文件:
A.txt包含God Save the Queen字样
B.txt包含Ubi maior,minor cessat
C.txt.gz是一个使用gzip压缩的文件,其中包含带有Hello America !!字样的文件c.txt。
For Example:
if I have three files like this in a folder called testx:
A.txt contains the words "God Save The queen"
B.txt contains the words "Ubi maior, minor cessat"
C.txt.gz is a file compressed with gzip that contain the file c.txt with the words "Hello America!!"
所以我压缩testx,获得压缩的tar文件:testx.tar.gz。
So I compress testx, obtain the compressed tar file: testx.tar.gz.
所以我的Java应用程序我想在控制台打印:
God Save the Queen
Ubi maior,minor cessat
Hello America !!
So with my Java application I would like to print in the console:
"God Save The queen"
"Ubi maior, minor cessat"
"Hello America!!"
我实现了ZIP版本,它工作得很好,但保持tar库从apache蚂蚁 http://commons.apache .org / compress / ,我注意到它不容易像ZIP java utils。
I have implemented the ZIP version and it works well, but keeping tar library from apache ant http://commons.apache.org/compress/, I noticed that it is not easy like ZIP java utils.
有人可以帮助我吗?
我已经开始在网络上了解如何完成我的目标,所以我有以下代码:
I have started looking on the net to understand how to accomplish my aim, so I have the following code:
GZIPInputStream gzipInputStream=null;
gzipInputStream = new GZIPInputStream( new FileInputStream(fileName));
TarInputStream is = 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 is a OutputStream!!
}
}
}
c $ c> is.copyEntryContents(out),可以保存一个文件流传递OutputStream,但我不想要它!在保存第一个条目ZipEntry之后的zip版本中,我们可以从压缩的根文件夹testx.tar.gz中提取流,然后创建一个新的ZipInputStream并使用它来获取内容。
So in the line is.copyEntryContents(out)
, it is possible to save on a file the stream passing an OutputStream, but I don't want it! In the zip version after keeping the first entry, ZipEntry, we can extract the stream from the compressed root folder, testx.tar.gz, and then create a new ZipInputStream and play with it to obtain the content.
可以使用tar.gz文件吗?
Is it possible to do this with the tar.gz file?
谢谢。
推荐答案
在网上冲浪,我遇到了一个有趣的想法: http://hype-free.blogspot.com/2009/10/using-tarinputstream-from-java.html 。
surfing the net, i have encountered an interesting idea at : http://hype-free.blogspot.com/2009/10/using-tarinputstream-from-java.html.
在将我们的TarEntry转换为Stream之后,我们可以采用与Zip文件相同的想法:
After converting ours TarEntry to Stream, we can adopt the same idea used with Zip Files like:
InputStream tmpIn = new StreamingTarEntry(is, entryx.getSize());
// use BufferedReader to get one line at a time
BufferedReader gzipReader = new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
inputZip )));
while (gzipReader.ready()) { System.out.println(gzipReader.readLine()); }
gzipReader.close();
使用此代码,您可以打印文件的内容testx.tar.gz ^ _ ^
SO with this code you could print the content of the file testx.tar.gz ^_^
这篇关于如何打印带有Java的tar.gz文件的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!