我正在尝试使用 ZSTD 算法来压缩和解压缩字节数组。 我阅读了 ZSTD 文档,该文档将提供有关实现的更多详细信息,但我仍然无法实现足够的方法来压缩和解压缩字节数组。 如果有人能向我解释我必须做什么,将不胜感激。
public static byte[] compressZstd(byte[] input) throws IOException {
var compressor = new ZstdCompressor();
byte[] compressedBuffer = new byte[1024];
compressor.compress(input, 0, input.length, compressedBuffer, 0, 1024);
return compressedBuffer;
}
public static byte[] decompressZstd(byte[] input) throws IOException {
var decompressor = new ZstdDecompressor();
byte[] decompressedBuffer = new byte[1024];
decompressor.decompress(input, 0, input.length, decompressedBuffer, 0, 1024);
return decompressedBuffer;
}
你应该确定缓冲区的大小,还要确定缓冲区的索引值。
public static byte[] compressZstd(byte[] input) throws IOException {
var compressor = new ZstdCompressor();
int maxCompressLength = compressor.maxCompressedLength(input.length);
byte[] compressedBuffer = new byte[maxCompressLength];
int compressedSize = compressor.compress(input, 0, input.length, compressedBuffer, 0, compressedBuffer.length);
return Arrays.copyOfRange(compressedBuffer, 0, compressedSize);
}
public static byte[] decompressZstd(byte[] compressed) throws IOException {
var decompressor = new ZstdDecompressor();
byte[] decompressedBuffer = new byte[Integer.MAX_VALUE];
int decompressedSize = decompressor
.decompress(compressed, 0, compressed.length, decompressedBuffer, 0, decompressedBuffer.length);
return Arrays.copyOfRange(decompressedBuffer, 0, decompressedSize);
}