Java解压gzip数据流

响应类型为:
content-encoding: gzip

gzip数据流解压

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static String streamToString2(InputStream in) throws IOException {
//定义一个内存输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//将流转换成字符串
GZIPInputStream gis = new GZIPInputStream(in);
int len1 = -1;
byte[] b1 = new byte[1024];
while ((len1 = gis.read(b1)) != -1) {
byteArrayOutputStream.write(b1, 0, len1);
}
byteArrayOutputStream.close();
//转化为数组
// byte[] bytes = byteArrayOutputStream.toByteArray();
return byteArrayOutputStream.toString();
}

获取数据流

1
2
3
4
5
6
7
8
9
byte[] bytes = null;
DataInputStream dataInputStream = new DataInputStream(connection.getInputStream());
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
bytes =output.toByteArray();
谢谢,爱你么么哒