Ami*_*ani 3 java compression apache gzip tar
我正在使用Apache Compress库来读取.tar.gz文件,如下所示:
final TarArchiveInputStream tarIn = initializeTarArchiveStream(this.archiveFile);
try {
TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
while (tarEntry != null) {
byte[] btoRead = new byte[1024];
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); //<- I don't want this!
int len = 0;
while ((len = tarIn.read(btoRead)) != -1) {
bout.write(btoRead, 0, len);
}
bout.close();
tarEntry = tarIn.getNextTarEntry();
}
tarIn.close();
}
catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
有可能不将它提取到一个单独的文件中,并以某种方式在内存中读取它吗?也许变成一个巨大的字符串或什么?
您可以使用a替换文件流ByteArrayOutputStream.
即取而代之:
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath)); //<- I don't want this!
Run Code Online (Sandbox Code Playgroud)
有了这个:
ByteArrayOutputStream bout = new ByteArrayOutputStream();
Run Code Online (Sandbox Code Playgroud)
然后关闭后bout,bout.toByteArray()用来获取字节.
是否有可能不将其提取到单独的文件中,并以某种方式将其读取到内存中?也许变成一个巨大的字符串什么的?
是的,当然。
只需将打开文件并写入文件的内部循环中的代码替换为写入ByteArrayOutputStream...或一系列此类流的代码即可。
从 TAR 读取的数据(就像这样)的自然表示将是字节/字节数组。如果字节是正确编码的字符,并且您知道正确的编码,那么您可以将它们转换为字符串。否则,最好将数据保留为字节。(如果您尝试将非文本数据转换为字符串,或者使用错误的字符集/编码进行转换,则可能会不可逆转地破坏它。)
显然,您需要自己思考其中一些问题,但基本思想应该可行......只要您有足够的堆空间。