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;  
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;






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


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.     }  
posted on 2012-03-02 10:09  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2012/03/02/2376687.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值