0

I have a key value storage which I have data stored. When I retrieve the value it is a JSON with a property "compressed_value", this value is a string and I don't know how to decompress or deserialize it.

I tried a lot of approaches, one of them try to cast the string to a byte[] using .getBytes() with different charsets, and using GZIPInputStream but I always get the error "java.util.zip.ZipException: Not in GZIP format".

The value es something like: "H4sIAAAAAAAA/wtOzC3ISVUoSa0oUSjJSCxRyFRITswrUUhJLU4tykzMyaxK1VFISczLVQQAkIb0eioAAAA=". When I paste it in a web like this one, it deserialize the value without problem.

Example code

public class Main {
    public static void main(String[] args) throws IOException {
    String compressedValue = "H4sIAAAAAAAA/wtOzC3ISVUoSa0oUSjJSCxRyFRITswrUUhJLU4tykzMyaxK1VFISczLVQQAkIb0eioAAAA=";

        try {
            final int BUFFER_SIZE = 32;
            ByteArrayInputStream is = new ByteArrayInputStream(compressedValue.getBytes());
            GZIPInputStream gis = new GZIPInputStream(is);
            byte[] data = new byte[BUFFER_SIZE];
            int bytesRead;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((bytesRead = gis.read(data)) != -1) {
                baos.write(data, 0, bytesRead);
            }
            gis.close();
            is.close();
            System.out.println(baos.toString("UTF-8"));
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • 2
    From the website you linked "This simple online text compression tool is compressing a plain text and decompressing compressed *base64 string* with gzip, bzip2 and deflate algorithms" (emphasis mine). Where is the part where you decode the base64 string? – Federico klez Culloca Jun 02 '21 at 19:50
  • @FedericoklezCulloca, I wasn't decoding the string. Thank you so much! – Clovis Ignacio Ferreira Jun 02 '21 at 19:58

0 Answers0