>  기사  >  Java  >  Java에서 GZIP 압축 및 압축 풀기 클래스를 사용하기 위한 예제 코드 공유

Java에서 GZIP 압축 및 압축 풀기 클래스를 사용하기 위한 예제 코드 공유

黄舟
黄舟원래의
2017-03-21 10:38:191518검색

이 글에서는 주로 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; 
  } 
}

위 내용은 Java에서 GZIP 압축 및 압축 풀기 클래스를 사용하기 위한 예제 코드 공유의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.