GzipOutputStream及GzipInputStream的用法 - 搜索-gcgmh - ITeye技术网站
- ByteArrayOutputStream arrayOutputStream =new ByteArrayOutputStream();
- GZIPOutputStream gop = new GZIPOutputStream(arrayOutputStream);
- byte[] buffer = new byte[1024];
- int len = 0;
- while ((len = inputStream.read(buffer)) != -1) {
- gop.write(buffer, 0, len);
- }
- gop.finish(); //这个在写入arrayOutputStream时一定要有,否则不能完全写入
- gop.close;
ByteArrayOutputStream arrayOutputStream =new ByteArrayOutputStream(); GZIPOutputStream gop = new GZIPOutputStream(arrayOutputStream); byte[] buffer = new byte[1024]; int len = 0; while ((len = inputStream.read(buffer)) != -1) { gop.write(buffer, 0, len); } gop.finish(); //这个在写入arrayOutputStream时一定要有,否则不能完全写入 gop.close;
----------------------------------------
- Header encoding = method.getResponseHeader("Content-Encoding");
- if (encoding != null) {
- if (encoding.getValue().equals("gzip")) {
- bytes = GZipUtil.unzip(bytes);
- }
- }
- public static byte[] unzip(InputStream in) throws IOException {
- // Open the compressed stream
- GZIPInputStream gin = new GZIPInputStream(in);
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- // Transfer bytes from the compressed stream to the output stream
- byte[] buf = new byte[size];
- int len;
- while ((len = gin.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- // Close the file and stream
- gin.close();
- out.close();
- return out.toByteArray();
- }