zoukankan      html  css  js  c++  java
  • 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.     }  
  • 相关阅读:
    独角戏
    开源引擎
    如何实现一个UI系统
    VC编程规范—程序员应该这样写代码
    夕阳下的熊猫香[转]
    在桌面上显示屏保
    在WinSock上使用IOCP
    结构体对齐的具体含义(#pragma pack)
    一个程序员的奋斗
    让汇编揭开死循环的神秘面纱
  • 原文地址:https://www.cnblogs.com/lexus/p/2376687.html
Copyright © 2011-2022 走看看