0

I have a String which is first zipped (not gzip) and then base64 encoded. I want to have the plain text again. Following code

private void decode_decompress(String string) {
    byte[] decodedBytes = Base64.decodeBase64(string.getBytes());
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Inflater decompresser = new Inflater(true);
    InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(stream, decompresser);
    try {
        inflaterOutputStream.write(decodedBytes);
        inflaterOutputStream.close();
        byte[] output2 = stream.toByteArray();          
        logger.info("Data: {}", output2.toString());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

throws an exception:

java.util.zip.ZipException: invalid stored block lengths
        at java.util.zip.InflaterOutputStream.write(InflaterOutputStream.java:273)
        at java.io.FilterOutputStream.write(FilterOutputStream.java:97)
        at de.martinm.tools.EBICS.DSTools.decode_decompress(DSTools.java:87)
        at de.martinm.tools.EBICS.DSTools.process(DSTools.java:77)
        at de.martinm.tools.EBICS.DSTools.main(DSTools.java:100)

I guess, I'm mixing up again input/output inflate/deflate

Here is the data which is compressed and base64 encoded:

eJx1U2tzqjAQ/SsO96NTw1PBQTr4qlgtyqvVL3cihEehiSYg1l9/rd46dZx+y549Ocme3dUfDx9FY48oywjucUKL5xoIhyTKcNLjfG/8oHINVkIcwYJg1OMw4R4N3c0SDMuKokW1eUafNo0QHcISNk5qmPW4tCy3XQDqum6hTRayFqEJcHle4C6MbnRLqqUzQ+R5HvAaOHEiliV/vtlnjR5XUdw90S5hd8Lz8jfhwLJf9ATwNp+5YYo+4EOGvyoJ0ekWy7rsDM5ICMtz7b/+uXH/Ljgf/7JvG1oHFnF3tlg4JoZ+OQewqJChR6zruOZNPCdRVVTMMOebJcxHZRJ1kqeDJJqfR6IQJDdngt1cBt5ncYKnO8d99Tp9gYoweT2O40BUatURhWKZvVHV7E8102XHXTDN5ZI1vZyX6KKeSm+SmK9VlQZ5nZeKvd8X7aPUmRztxdp8rtaZom1kJlsRqsK95RSS7RJ7AYOQbg6S2vZXrjWA6S5vqzlWYCG/z947YgXjcOasFuF8/JKs34nngCGYIVBukJd9jLHftuQSmfV6LJFg2CQrU5Ze4qJYpR1/b5qD2MaOvSv27Z1PV4GA+p1U1IDFWLJaifGEKmGKxZ3lq5Ox0EHb1G++JvGIpaSayxYd9J2kfO7nhXiw4XYYD3fyJsbC8kmDVv2iJZqqaAtnn/d08MPkL8NHh+1plHFpmEtzcM5ekXN00yBw075rg4PLxhgmz7d1cAf/gG5GAdISI2oNjVHfGried5K/QrrPfqYUHfwH7sSu62b8A39iR+Y=

xRed
  • 1,895
  • 6
  • 20
  • 36
Martin Müller
  • 122
  • 3
  • 17
  • 1
    The compression is probably wrong; show its code. Here I would do for cross-platform clarity (code checkers): `string.getBytes(StandardCharsets.US_ASCII)` and `new String(output2, StandardCharsets.UTF_8)` or what used when compressing. – Joop Eggen Nov 13 '17 at 14:18
  • Note that instead of `Base64.decodeBase64(string.getBytes());` you could just co `Base64.decodeBase64(string);` which internally uses the ISO-8859-1 charset. Also note the JavaDoc on `Inflater(boolean)`: "When using the 'nowrap' option it is also necessary to provide an extra "dummy" byte as input." - thus you might have to prepend a byte to `decodedBytes`. However, since you stated "not gzip" you'd probably want to disable GZIP compatible decompression and hence pass `false` or use the no-argument constructor. – Thomas Nov 13 '17 at 14:20
  • The compression must have been pure zip compress, as it is requested by the standard for which the data is ment to be – Martin Müller Nov 13 '17 at 14:23
  • @MartinMüller As Thomas already mentioned, if you want to use pure Zip compression you should call the Inflater constructor with false. – Andreas Brunnet Nov 13 '17 at 14:46
  • Ok, by changing true to false the exception does not occur anymore. But my output is: `decode_decompress - Data: [B@1fc2b765` I except XML as data – Martin Müller Nov 13 '17 at 14:51
  • Yes, that is normal. Arrays do not implement their own toString method, so they inherit the default toString from java.lang.Object. See https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array. – VGR Nov 13 '17 at 14:59

1 Answers1

3

This solved the issue:

private void decode_decompress(String string) {
    byte[] decodedBytes = Base64.decodeBase64(string);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Inflater decompresser = new Inflater(false);
    InflaterOutputStream inflaterOutputStream = new InflaterOutputStream(stream, decompresser);
    try {
        inflaterOutputStream.write(decodedBytes);
        inflaterOutputStream.close();
        String data = stream.toString();
        logger.info("Data: {}", data);
    } catch (IOException e) {
        logger.error(string, e);
    }
}
Martin Müller
  • 122
  • 3
  • 17