【问题标题】:Decompressing a buffer from a SOAP response从 SOAP 响应中解压缩缓冲区
【发布时间】:2021-05-14 08:02:21
【问题描述】:

这可能是一个愚蠢的问题,但我真的无法弄清楚。 我在 SoapUI 中发出一个 SOAP 请求,该请求为我检索某个文件的 GZIP 压缩缓冲区。 我的问题是我无法解压缩获得的缓冲区(我对 java 没有经验)。到目前为止,我获得的唯一结果是一些随机的 10-11 个字符串( [B@6d03e736 )或诸如“不是 GZIP 格式)之类的错误

在缓冲器看起来像这样: “1f8b0800000000000000a58e4d0ac2400c85f78277e811f2e665329975bbae500f2022dd2978ff95715ae82cdcf9415efec823c6710247582d5965c32c65aab0f5fc0a5204c415855e7c190ef61b34710bcdc7486d2bab8a7a4910d022d5e107d211ed345f2f37a103da2ddb1f619ab8acefe7fdb1beb6394998c7dfbde3dcac3acf3f399f3eeae152012e010000” P>

我查看了许多类似的线程,并且只遇到了一些示例,其中有人获得了一个随机字符串,该字符串被压缩然后解压缩(主要是通过 GZIPInputStream/GZIPOutputStream)。

String stringBuffer = "buffer_from_above";

byte[] buffer = stringBuffer.getBytes(StandardCharsets.ISO_8859_1); // also tried UTF-8

System.out.println(decompress(buffer));

public static String decompress(final byte[] compressed) throws IOException {
    final StringBuilder outStr = new StringBuilder();
    if ((compressed == null) || (compressed.length == 0)) {
        return "";
    }
    if (isCompressed(compressed)) {
        final GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(compressed));
        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(gis, "UTF-8"));

        String line;
        while ((line = bufferedReader.readLine()) != null) {
            outStr.append(line);
        }
    } else {
        outStr.append(compressed);
    }
    return outStr.toString();
}

如果有人能够就此事给我任何提示或建议,我将不胜感激。 感谢您花费的时间,祝您有美好的一天!

从这个线程窃取的解压函数:compression and decompression of string data in java

【问题讨论】:

    标签: java compression gzip encode


    【解决方案1】:

    如果这确实是字符串,那么您需要将十六进制转换为二进制,然后再将其提供给 gzip 解码器。见Convert a string representation of a hex dump to a byte array using Java?

    【讨论】:

    • 你是最棒的!非常感谢,第一次尝试就完美了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2023-03-06
    • 2016-08-02
    • 2015-05-23
    • 1970-01-01
    • 2016-03-13
    相关资源
    最近更新 更多