Base64解码并解压缩字符串

时间:2017-11-13 14:13:35

标签: java base64 unzip

我有一个首先压缩的字符串(不是gzip),然后是base64编码的。 我想再次使用纯文本。以下代码

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();
    }
}

抛出异常:

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)

我想,我正在混合输入/输出膨胀/放气

以下是压缩和base64编码的数据:

  

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

1 个答案:

答案 0 :(得分:1)

这解决了这个问题:

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);
    }
}