欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java GZIP压缩与解压缩代码实例

程序员文章站 2023-11-07 13:50:28
这篇文章主要介绍了java gzip压缩与解压缩代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 1.gzip压缩...

这篇文章主要介绍了java gzip压缩与解压缩代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1.gzip压缩

public static byte[] compress(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 ( exception e) {
      e.printstacktrace();
    }
    return out.tobytearray();
  }

2.gzip解压缩

public static byte[] uncompress(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
      return null;
    }
    bytearrayoutputstream out = new bytearrayoutputstream();
    bytearrayinputstream in = new bytearrayinputstream(bytes);
    try {
      gzipinputstream ungzip = new gzipinputstream(in);
      byte[] buffer = new byte[256];
      int n;
      while ((n = ungzip.read(buffer)) >= 0) {
        out.write(buffer, 0, n);
      }
    } catch (exception e) {
      e.printstacktrace();
    }
    return out.tobytearray();
  }

3.工具代码集合

import java.io.bytearrayinputstream;
import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.util.zip.gzipinputstream;
import java.util.zip.gzipoutputstream;

public class gziputils {
  public static final string gzip_encode_utf_8 = "utf-8"; 
  public static final string gzip_encode_iso_8859_1 = "iso-8859-1";

  
  public static byte[] compress(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 ( exception e) {
      e.printstacktrace();
    }
    return out.tobytearray();
  }
  
  public static byte[] compress(string str) throws ioexception { 
    return compress(str, gzip_encode_utf_8); 
  }
  
  public static byte[] uncompress(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
      return null;
    }
    bytearrayoutputstream out = new bytearrayoutputstream();
    bytearrayinputstream in = new bytearrayinputstream(bytes);
    try {
      gzipinputstream ungzip = new gzipinputstream(in);
      byte[] buffer = new byte[256];
      int n;
      while ((n = ungzip.read(buffer)) >= 0) {
        out.write(buffer, 0, n);
      }
    } catch (exception e) {
      e.printstacktrace();
    }
    return out.tobytearray();
  }
  
  public static string uncompresstostring(byte[] bytes, string encoding) { 
    if (bytes == null || bytes.length == 0) { 
      return null; 
    } 
    bytearrayoutputstream out = new bytearrayoutputstream(); 
    bytearrayinputstream in = new bytearrayinputstream(bytes); 
    try {
      gzipinputstream ungzip = new gzipinputstream(in); 
      byte[] buffer = new byte[256]; 
      int n; 
      while ((n = ungzip.read(buffer)) >= 0) { 
        out.write(buffer, 0, n); 
      } 
      return out.tostring(encoding);
    } catch (exception e) {
      e.printstacktrace();
    }
    return null;
  }
  
  public static string uncompresstostring(byte[] bytes) { 
    return uncompresstostring(bytes, gzip_encode_utf_8); 
  } 
  
  public static void main(string[] args) throws ioexception {
    string s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    system.out.println("字符串长度:"+s.length());
    system.out.println("压缩后::"+compress(s).length);
    system.out.println("解压后:"+uncompress(compress(s)).length);
    system.out.println("解压字符串后::"+uncompresstostring(compress(s)).length());
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。