GzipOutputStream及GzipInputStream的用法

GzipOutputStream及GzipInputStream的用法 - 搜索-gcgmh - ITeye技术网站

Java代码  收藏代码
  1. ByteArrayOutputStream arrayOutputStream =new ByteArrayOutputStream();  
  2. GZIPOutputStream gop = new GZIPOutputStream(arrayOutputStream);  
  3. byte[] buffer = new byte[1024];  
  4. int len = 0;  
  5. while ((len = inputStream.read(buffer)) != -1) {  
  6.     gop.write(buffer, 0, len);  
  7. }  
  8. gop.finish(); //这个在写入arrayOutputStream时一定要有,否则不能完全写入  
  9. gop.close;  






----------------------------------------


Java代码  收藏代码
  1. Header encoding = method.getResponseHeader("Content-Encoding");  
  2.         if (encoding != null) {  
  3.             if (encoding.getValue().equals("gzip")) {  
  4.                 bytes = GZipUtil.unzip(bytes);  
  5.             }  
  6.         }  
  7.   
  8.   
  9. public static byte[] unzip(InputStream in) throws IOException {  
  10.         // Open the compressed stream  
  11.         GZIPInputStream gin = new GZIPInputStream(in);  
  12.   
  13.         ByteArrayOutputStream out = new ByteArrayOutputStream();  
  14.   
  15.         // Transfer bytes from the compressed stream to the output stream  
  16.         byte[] buf = new byte[size];  
  17.         int len;  
  18.         while ((len = gin.read(buf)) > 0) {  
  19.             out.write(buf, 0, len);  
  20.         }  
  21.   
  22.         // Close the file and stream  
  23.         gin.close();  
  24.         out.close();  
  25.         return out.toByteArray();  
  26.     }  
原文地址:https://www.cnblogs.com/lexus/p/2376687.html