4

我正在尝试解压缩大量 40 MB 以上的文件,因为我使用 ByteBuffers 和 Channels 并行下载它们。使用 Channels 比使用 Streams 获得了更好的吞吐量,我们需要这是一个非常高吞吐量的系统,因为我们每天需要处理 40 TB 的文件,而这部分过程目前是瓶颈。这些文件使用zstd-jni压缩。Zstd-jni 有用于解压缩字节缓冲区的 api,但是当我使用它们时出现错误。如何使用 zstd-jni 一次解压缩一个字节缓冲区?

我在他们的测试中找到了这些示例,但除非我遗漏了什么,否则使用 ByteBuffers 的示例似乎假设整个输入文件适合一个 ByteBuffer: https ://github.com/luben/zstd-jni/blob/master/src/测试/scala/Zstd.scala

下面是我用于压缩和解压缩文件的代码。压缩代码运行良好,但解压缩代码随后失败,错误为 -70。

public static long compressFile(String inFile, String outFolder, ByteBuffer inBuffer, ByteBuffer compressedBuffer, int compressionLevel) throws IOException {
    File file = new File(inFile);
    File outFile = new File(outFolder, file.getName() + ".zs");
    long numBytes = 0l;

    try (RandomAccessFile inRaFile = new RandomAccessFile(file, "r");
        RandomAccessFile outRaFile = new RandomAccessFile(outFile, "rw");
                FileChannel inChannel = inRaFile.getChannel();
                FileChannel outChannel = outRaFile.getChannel()) {
        inBuffer.clear();
        while(inChannel.read(inBuffer) > 0) {
            inBuffer.flip();
            compressedBuffer.clear();

            long compressedSize = Zstd.compressDirectByteBuffer(compressedBuffer, 0, compressedBuffer.capacity(), inBuffer, 0, inBuffer.limit(), compressionLevel);
            numBytes+=compressedSize;
            compressedBuffer.position((int)compressedSize);
            compressedBuffer.flip();
            outChannel.write(compressedBuffer);
            inBuffer.clear(); 
        }
    }

    return numBytes;
}

public static long decompressFile(String originalFilePath, String inFolder, ByteBuffer inBuffer, ByteBuffer decompressedBuffer) throws IOException {
    File outFile = new File(originalFilePath);
    File inFile = new File(inFolder, outFile.getName() + ".zs");
    outFile = new File(inFolder, outFile.getName());

    long numBytes = 0l;

    try (RandomAccessFile inRaFile = new RandomAccessFile(inFile, "r");
        RandomAccessFile outRaFile = new RandomAccessFile(outFile, "rw");
                FileChannel inChannel = inRaFile.getChannel();
                FileChannel outChannel = outRaFile.getChannel()) {

        inBuffer.clear();

        while(inChannel.read(inBuffer) > 0) {
            inBuffer.flip();
            decompressedBuffer.clear();
            long compressedSize = Zstd.decompressDirectByteBuffer(decompressedBuffer, 0, decompressedBuffer.capacity(), inBuffer, 0, inBuffer.limit());
            System.out.println(Zstd.isError(compressedSize) + " " + compressedSize);
            numBytes+=compressedSize;
            decompressedBuffer.position((int)compressedSize);
            decompressedBuffer.flip();
            outChannel.write(decompressedBuffer);
            inBuffer.clear(); 
        }
    }

    return numBytes;
}
4

1 回答 1

3

是的,您在示例中使用的静态方法假定整个压缩文件适合一个 ByteBuffer。据我了解您的要求,您需要使用 ByteBuffers 进行流式解压。ZstdDirectBufferDecompressingStream 已经提供了这个:

https://static.javadoc.io/com.github.luben/zstd-jni/1.3.7-1/com/github/luben/zstd/ZstdDirectBufferDecompressingStream.html

这是一个如何使用它的示例(来自测试):

https://github.com/luben/zstd-jni/blob/master/src/test/scala/Zstd.scala#L261-L302

但是您还必须对其进行子类化并覆盖“refill”方法。

编辑:这是我刚刚添加的一个新测试,其结构与您的问题完全相同 - 在通道之间移动数据:

https://github.com/luben/zstd-jni/blob/master/src/test/scala/Zstd.scala#L540-L586

于 2018-10-23T17:37:37.903 回答