Search code examples
javastreamgzip

Unable to read small compressed byte array from GZIPInputStream


I struggle with a problem and I can't solve it or find a useful answer to it.
I have a piece of code that compress an array of bytes :

try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) {
    final GZIPOutputStream gzip = new GZIPOutputStream(byteArrayOutputStream);
    gzip.write(response.getBytes());
    gzip.close();
    response = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
    log.error("Problem in compression", e);
}

It works pretty well, and the resulting compressed array of bytes is saved in a database.

I have another piece of code that will get the compressed array of bytes in the database and that will decompress it :

try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); 
    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(compressed)) {
    GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream, BUFFER_SIZE);
    byte[] data = new byte[BUFFER_SIZE];
    int bytesRead;

    while ((bytesRead = gzipInputStream.read(data)) != -1) {
        byteArrayOutputStream.write(data, 0, bytesRead);
    }

    gzipInputStream.close();

    return byteArrayOutputStream.toByteArray();
} catch (IOException e) {
    log.error("Problem in decompression", e);
}

The decompression works very well for "big" byte array (bigger than 32 bytes), but I have, for example a compressed array of 20 bytes and I cannot decompress it.
It never enters the while because the read always return -1 and so nothing is read.
I tried to change the buffer size, etc..., but nothing works.
What am I doing wrong ?

Thank you in advance !


Solution

  • This is essentially your code (though the zip input stream should be in the try-with-resources block), which you can run:

    import java.util.*;
    import java.util.zip.*;
    import java.io.*;
    
    public class Unzip {
        public static void main(String[] args) throws Exception {
            String s = "H4sIAAAAAAAAA3WRQW+CQBCF7/6K0dOSUNKeiYeaYMNJo+nFxjQrTHQTYMkwhNLG/95lESSIe9jDmzffm51lqkGsasZ3IllvSs5L3jOhTOE0qS4hwwomO4TjwwzM6athNoEbiiPaoCQineaERYGx48Cf5X4cwu2w+/yr8kfayCWmgl1Yfa7Xwe57Hx4Cx7f0xvd1hFiyvJGsMjAeW6PK2JaKHcrYn1mtuqgEQYheN4jReJ65YtHgzYPmS3h5697VpT9s1KtIMdoeF17de+pt5GsbPg6KEl2g+Y22SsglZU8CWPfLbxquEEmOLiDCTfATYc5KZ4DdnIk+e0ikSSy2pE8JpmYXEGP3Vca8cI3dcP4BN4CC8VgCAAA=";
            final int BUFFER_SIZE = 32;
            byte[] compressed = Base64.getDecoder().decode(s);
            try (ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(compressed)) {
                GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream, BUFFER_SIZE);
                byte[] data = new byte[BUFFER_SIZE];
                int bytesRead;
    
                while ((bytesRead = gzipInputStream.read(data)) != -1) {
                    byteArrayOutputStream.write(data, 0, bytesRead);
                }
    
                gzipInputStream.close();
    
                System.out.println(new String(byteArrayOutputStream.toByteArray()));
            } catch (IOException e) {
                e.printStackTrace();    
            }
        }
    }