我有一个相当大的BZ2文件,里面有几个文本文件。我是否可以使用Java来解压缩BZ2文件中的某些文件,并动态地解压缩/解析数据?假设一个300mb的BZ2文件包含1 GB的文本。理想情况下,我希望我的java程序读取1mb的BZ2文件,动态地解压缩它,对它执行操作,并继续读取BZ2文件以获取更多数据。这有可能吗?
谢谢
发布于 2013-06-10 21:21:32
apache的commons-compress库相当不错。这是他们的示例页面:http://commons.apache.org/proper/commons-compress/examples.html
下面是最新的maven代码片段:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.10</version>
</dependency>下面是我的util方法:
public static BufferedReader getBufferedReaderForCompressedFile(String fileIn) throws FileNotFoundException, CompressorException {
FileInputStream fin = new FileInputStream(fileIn);
BufferedInputStream bis = new BufferedInputStream(fin);
CompressorInputStream input = new CompressorStreamFactory().createCompressorInputStream(bis);
BufferedReader br2 = new BufferedReader(new InputStreamReader(input));
return br2;
}发布于 2011-01-29 10:48:44
Ant项目包含一个bzip2库。它有一个org.apache.tools.bzip2.CBZip2InputStream类。您可以使用这个类动态地解压缩bzip2文件--它只是扩展了标准的Java InputStream类。
发布于 2021-12-19 10:51:39
您可以从Apache commons-compress中使用org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream
InputStream inputStream = new BZip2CompressorInputStream(new FileInputStream(xmlBz2File), true) // true should be used for big files, as I understand而不是org.apache.commons.compress.utils.IOUtils
int pos = 0;
int step = 1024 * 32;
byte[] buffer = new byte[step];
int actualLength = 1;
while (actualLength > 0) {
actualLength = IOUtils.readFully(inputStream, buffer, pos, step);
pos += actualLength;
String str = new String(buffer, 0, actualLength, StandardCharsets.UTF_8);
// something what you want to do
}但这可能很难应对背压(消费者可能比生产者更快,反之亦然)。所以我尝试在BZip2CompressorInputStream中使用Akka Streams。
https://stackoverflow.com/questions/4834721
复制相似问题