[英]Get specific file inputstream from TarArchiveInputStream
我有一个 tar 文件,其中包含许多文件。 我需要从 tar 文件中获取特定文件并从该文件中读取数据。
我正在使用下面的代码解压文件,我将使用其他一些 function 读取这个返回的输入 stream。
private InputStream unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
InputStream versionInputStream = null;
final InputStream is = new FileInputStream(inputFile);
final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
TarArchiveEntry entry = null;
while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
if (!entry.isDirectory() && entry.getName().equals("version.txt")) {
versionInputStream = new FileInputStream(entry.getFile());
}
}
return versionInputStream;
}
当我执行versionInputStream = new FileInputStream(entry.getFile());
我知道我们可以先将此文件保存在目录中,然后再读取文件,但我不想将此文件保存在目录中。
有什么方法可以在不将文件保存到某个目录的情况下读取此文件?
您阅读的存档条目没有文件。 TarArchiveEntry
的getFile
方法仅在使用File
-arg 构造函数创建条目时返回任何有用的内容,这仅在创建存档而不读取它时才有意义。
您正在寻找的 stream 是TarArchiveInputStream
本身,在您将其定位在您要阅读的条目之后,即
if (!entry.isDirectory() && entry.getName().equals("version.txt")) {
versionInputStream = debInputStream;
break;
}
注意break
。
尚未发布(还没有发布日期)Commons Compress 1.21 将包含一个新的TarFile
class ,它提供对从可搜索源(如File
)读取的档案的随机访问,并使您的任务更方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.