0

I'm trying to use ZSTD algorithm to compress and decompress byte array. I read the ZSTD document that will give more details about implementation, but I can't still implement sufficient method to compress and decompress byte arrays. If anyone can explain to me what I have to do it would be appreciated.

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;

    }
pan ta
  • 3
  • 1

1 Answers1

-1

You should determine the size of the buffer and also determine the index value of the buffer.

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[MAX_VALUE_FOR_DECOMPRESSED_BUFFER];
        int decompressedSize = decompressor
                .decompress(compressed, 0, compressed.length, decompressedBuffer, 0, decompressedBuffer.length);
        return Arrays.copyOfRange(decompressedBuffer, 0, decompressedSize);

    }
sadegh
  • 14
  • 3