尝试解压缩 gzip 编码的 http 响应。将以下内容传递给解压缩器:
gd.decompressGZIP(response.split("\r\n\r\n")[1].getBytes());
此代码拆分响应并将后一部分(gzip 编码内容)发送到 decompressGZIP 函数。“response”变量是一个包含完整 htpp 响应的字符串。解压器代码如下。
String decompressGZIP(byte[] gzip) throws IOException {
java.io.ByteArrayInputStream bytein = new java.io.ByteArrayInputStream(gzip);
java.util.zip.GZIPInputStream gzin = new java.util.zip.GZIPInputStream(bytein);
java.io.ByteArrayOutputStream byteout = new java.io.ByteArrayOutputStream();
int res = 0;
byte buf[] = new byte[1024];
while (res >= 0) {
res = gzin.read(buf, 0, buf.length);
if (res > 0) {
byteout.write(buf, 0, res);
}
}
return (new String(byteout.toByteArray()));
}
但是,我收到如下异常:
Sep 03, 2013 9:19:04 PM ownproxy.ProxyThread2 run
SEVERE: null
java.util.zip.ZipException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:164)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:78)
at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:90)
at ownproxy.GZIPDecompress.decompressGZIP(GZIPDecompress.java:18)
at ownproxy.ProxyThread2.run(InterceptionProxy2.java:84)
数据似乎不是 gzip 格式。谁能告诉我我做错了什么?