使用Java Tar Package读取*.tar 或*.tar.gz 文件

Java Tar Package com.ice.tar 实现了一个tar 文档输入输出io包。使用方式接近java 中自带的 java.util.zip 包,所以也该非常容易上手,如果使用国java的zip包的话。

而且配合java 的中的GZIPInputStream 使用,就很容易实现.tar.gz 文件的访问。
步骤:
1. 读取文件,生成GZIPInputStream 流
2. 把1中生成的GZIPInputStream流传给 Java Tar Package 中的TarInputStream 流
过程非常简单,代码如下
private void visitTARGZ(P parser, File targzFile) throws IOException {
FileInputStream fileIn = null;
BufferedInputStream bufIn = null;
GZIPInputStream gzipIn = null;
TarInputStream taris = null;
try {
fileIn = new FileInputStream(targzFile);
bufIn = new BufferedInputStream(fileIn);
gzipIn = new GZIPInputStream(bufIn); //first unzip the input file stream.
taris = new TarInputStream(gzipIn);

TarEntry entry = null;
while ((entry = taris.getNextEntry()) != null) {
if (entry.isDirectory())
continue;
configure(taris, entry.getFile()); //process every entry in this tar file.
}
} finally {
taris.close();
gzipIn.close();
bufIn.close();
fileIn.close();
}
}

TarInputStream 是FilterInputStream的子类,这样所用操作又回到Java中流的概念了。 TarInputStream 提供迭代访问tar归档中所有entry文件, 其方式与ZipInputStream风常相似。

TarOutputStream 当然用法也类似了。

更多参考其官方主页: http://www.trustice.com/index.shtml
下载地址:http://www.trustice.com/java/tar/

 Leave a Reply

(required)

(required)


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

使用腾讯微博登陆

Protected by WP Anti Spam
   
© 2011 Information Retrieval Blog Suffusion theme by Sayontan Sinha