java中GZIP压缩解压类使用实例

 更新时间:2017年03月20日 14:30:18   作者:刘望舒  
这篇文章主要介绍了java中GZIP压缩解压类使用实例的相关资料,需要的朋友可以参考下

java中GZIP压缩解压类使用实例

   当我们客户端与服务端进行数据传输时需要走流量,为了节省流量我们常常需要写一个压缩类对数据进行压缩。

实例代码:

import java.io.ByteArrayInputStream; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.util.zip.GZIPInputStream; 
import java.util.zip.GZIPOutputStream; 
 
/** 
 * GZIP压缩解压类 
 */ 
public class MessageGZIP { 
   
  private static String encode = "utf-8";//"ISO-8859-1" 
   
  public String getEncode() { 
    return encode; 
  } 
 
  /* 
   * 设置 编码,默认编码:UTF-8 
   */ 
  public void setEncode(String encode) { 
    MessageGZIP.encode = encode; 
  } 
 
  /* 
   * 字符串压缩为字节数组 
   */ 
  public static byte[] compressToByte(String str){ 
    if (str == null || str.length() == 0) { 
      return null; 
    } 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    GZIPOutputStream gzip; 
    try { 
      gzip = new GZIPOutputStream(out); 
      gzip.write(str.getBytes(encode)); 
      gzip.close(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
    return out.toByteArray(); 
  } 
 
  /* 
   * 字符串压缩为字节数组 
   */ 
  public static byte[] compressToByte(String str,String encoding){ 
    if (str == null || str.length() == 0) { 
      return null; 
    } 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    GZIPOutputStream gzip; 
    try { 
      gzip = new GZIPOutputStream(out); 
      gzip.write(str.getBytes(encoding)); 
      gzip.close(); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
    return out.toByteArray(); 
  } 
 
  /* 
   * 字节数组解压缩后返回字符串 
   */ 
  public static String uncompressToString(byte[] b) { 
    if (b == null || b.length == 0) { 
      return null; 
    } 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    ByteArrayInputStream in = new ByteArrayInputStream(b); 
 
    try { 
      GZIPInputStream gunzip = new GZIPInputStream(in); 
      byte[] buffer = new byte[256]; 
      int n; 
      while ((n = gunzip.read(buffer)) >= 0) { 
        out.write(buffer, 0, n); 
      } 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
    return out.toString(); 
  } 
 
  /* 
   * 字节数组解压缩后返回字符串 
   */ 
  public static String uncompressToString(byte[] b, String encoding) { 
    if (b == null || b.length == 0) { 
      return null; 
    } 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    ByteArrayInputStream in = new ByteArrayInputStream(b); 
 
    try { 
      GZIPInputStream gunzip = new GZIPInputStream(in); 
      byte[] buffer = new byte[256]; 
      int n; 
      while ((n = gunzip.read(buffer)) >= 0) { 
        out.write(buffer, 0, n); 
      } 
      return out.toString(encoding); 
    } catch (IOException e) { 
      e.printStackTrace(); 
    } 
    return null; 
  } 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

相关文章

  • MyBatis自定义映射resultMap的实现

    MyBatis自定义映射resultMap的实现

    本文主要介绍了MyBatis自定义映射resultMap的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • 利用线程实现动态显示系统时间

    利用线程实现动态显示系统时间

    编写Applet小程序,通过在HTML文档中接收参数,显示当前的系统时间,需要的朋友可以参考下
    2015-10-10
  • Exception in thread main java.lang.NoClassDefFoundError错误解决方法

    Exception in thread main java.lang.NoClassDefFoundError错误解决方

    这篇文章主要介绍了Exception in thread main java.lang.NoClassDefFoundError错误解决方法,需要的朋友可以参考下
    2016-08-08
  • response.setContentType()参数以及作用详解

    response.setContentType()参数以及作用详解

    这篇文章主要介绍了response.setContentType()参数以及作用详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Spring中BeanFactory和ApplicationContext的作用和区别(推荐)

    Spring中BeanFactory和ApplicationContext的作用和区别(推荐)

    这篇文章主要介绍了Spring中BeanFactory和ApplicationContext的作用和区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • 高并发环境下安全修改同一行数据库数据的策略分享

    高并发环境下安全修改同一行数据库数据的策略分享

    随着互联网技术的发展,越来越多的应用需要在高并发环境中运行,数据库的并发控制成为了业务的关键,本文将介绍如何在高并发情况下,安全地修改数据库中的同一行数据,需要的可以参考一下
    2023-06-06
  • 在springboot中注入FilterRegistrationBean不生效的原因

    在springboot中注入FilterRegistrationBean不生效的原因

    这篇文章主要介绍了在springboot中注入FilterRegistrationBean不生效的原因及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Spring Boot整合Redis的完整步骤

    Spring Boot整合Redis的完整步骤

    这篇文章主要给大家介绍了关于Spring Boot整合Redis的完整步骤,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-05-05
  • Java从零开始创建功能丰富的PDF文档

    Java从零开始创建功能丰富的PDF文档

    随着数字化办公的普及,PDF格式因其跨平台兼容性和安全性而被广泛应用于各种场景中,本文将详细介绍如何使用Java来实现PDF文档的一键生成,有需要的可以了解下
    2025-01-01
  • RocketMQ消息丢失场景以及解决方法

    RocketMQ消息丢失场景以及解决方法

    这篇文章主要给大家介绍了关于RocketMQ消息丢失场景以及解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09

最新评论