如何在内存中压缩公共压缩?

时间:2015-06-18 00:17:41

标签: bzip2 bytearrayoutputstream bytearrayinputstream apache-commons-compress

我正在尝试使用commons compress在内存中bzip2数据。我正在尝试这个:

private static final int bufferSize = 8192;

public void compress(
    ByteArrayInputStream byteArrayInputStream,
    CompressorOutputStream compressorOutputStream) throws IOException {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final byte[] buffer = new byte[bufferSize];
    int n = 0;
    while (-1 != (n = byteArrayInputStream.read(buffer)))
        compressorOutputStream.write(buffer, 0, n);
}

public byte[] compressBZIP2(byte[] inputBytes) throws Exception {
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(inputBytes);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BZip2CompressorOutputStream bZip2CompressorOutputStream = new BZip2CompressorOutputStream(byteArrayOutputStream);
    compress(byteArrayInputStream, bZip2CompressorOutputStream);
    return byteArrayOutputStream.toByteArray();
}

但这不起作用,

byte[] bzipCompressed = resultCompressor.compressBZIP2(contentBytes);

结果总是有3个字节,这就是全部。我究竟做错了什么?

3 个答案:

答案 0 :(得分:1)

您永远不会关闭BZip2CompressorOutputStream,这意味着最终(可能只有)数据块永远不会被写入包装流。

答案 1 :(得分:0)

对于那些有兴趣使用Apache Commons Compress,但寻找内存中bunzip2的人,这是一个经过测试的实现:

import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;

(..)

private static final int bufferSize = 8192;

private void uncompress(CompressorInputStream compressorInputStream, 
        ByteArrayOutputStream byteArrayOutputStream) throws IOException {
    final byte[] buffer = new byte[bufferSize];
    int n = 0;
    while (-1 != (n = compressorInputStream.read(buffer))) {
        byteArrayOutputStream.write(buffer, 0, n);
    }
    compressorInputStream.close();
    byteArrayOutputStream.close();
}

public ByteArrayOutputStream bunzip2(FileInputStream inputStream) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    BZip2CompressorInputStream bZip2CompressorInputStream = new BZip2CompressorInputStream(inputStream);
    uncompress(bZip2CompressorInputStream, byteArrayOutputStream);
    return byteArrayOutputStream;
}

希望这有助于某人!

答案 2 :(得分:0)

我在使用“ apache commons compress”时遇到了同样的问题,bzip2只写了3个字节。我最终将ByteArrayOutputStream替换为FileOutputStream

java 12上的示例:

public static String compress(String data) throws IOException {
        File bzip2TempFile = new File("/tmp/compressed.bzip2");
        var outputStream = new FileOutputStream(bzip2TempFile);

        CompressorOutputStream burrowZip2OutputStream = 
                new BZip2CompressorOutputStream(outputStream, 9);
        burrowZip2OutputStream.write(data.getBytes());
        burrowZip2OutputStream.close();

        try (var is = new FileInputStream(bzip2TempFile)) {
            var result = new String(Base64.getEncoder().encode(is.readAllBytes()));
            bzip2TempFile.deleteOnExit();
            return result;
        }
}

测试:

input: pirem
bzip2: BZh91AY&SYZF???"P 0???P??H?

H?@
base64 encoded: QlpoOTFBWSZTWRhaRoIAAAGBgAIiUAAgADDNAMGgUOLuSKcKEgMLSNBA