当我们客户端与服务端进行数据传输时需要走流量,为了节省流量我们常常需要写一个压缩类对数据进行压缩。
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压缩解压文件源码
基于java的开发源码-用GZIP压缩解压文件.zip 基于java的开发源码-用GZIP压缩解压文件.zip 基于java的开发源码-用GZIP压缩解压文件.zip 基于java的开发源码-用GZIP压缩解压文件.zip 基于java的开发源码-用GZIP压缩...
使用C语言编写的GZIP压缩及解压源代码
该案例通过zlib实现gzip的压缩解压功能,包括解压gzip字符串、图片两个案例;提供了zlib1211的源码包和对应编译后生成的文件; 相应说明参考笔者博客:Qt笔记8--zlib实现gzip解压,链接...
Java用GZIP压缩解压文件
一个Java随书实例:用GZIP压缩解压文件,从源文件得到文件输入流,得到目标文件输出流,得到压缩输出流,设定读入缓冲区尺寸,弹出文件选择器,并判断是否点击了打开按钮,判断事件来自于哪个按钮,用于选择解压和...
Java用GZIP压缩解压文件.7z
易语言gzip解压缩模块源码,gzip解压缩模块,PeLoader_LoadLibrary,PeLoader_FreeLibrary,PeLoader_GetProcAddress,PeLoader_GetEntryPoint,Call,初始化,GZIP压缩,GZIP解压
Delphi gzip 压缩 解压 zlib 源码(128版,123版),123版有例子
利用JAVASCRIPT即你想那个GZIP压缩与解压缩 最近流行的网络游戏(FLASH)数据传输都是用GZIP进行压缩与解压缩的,在客户端FLASH对与服务器交互的数据进行解压缩,
VC6 gzip 压缩解压 测试代码 使用gzip.dll windows2003自带的gzip库,用于网络数据传输的压缩解压,兼容目前所有的网络服务器
易语言GZIP解压缩源码,GZIP解压缩,GZIP压缩,GZIP解压,InitDecompression,InitCompression,CreateDecompression,CreateCompression,ResetDecompression,ResetCompression,DestroyDecompression,DeInitCompression,...
gzip 压缩 解压
Java用GZIP压缩解压文件.zip
此动态库是为了解决c# 编写的WebService使用Gzip压缩后,其他语言调用解压和压缩困难的问题。 webservice传递xml 非常占用带宽,如果压缩后传输就大大减少带宽的使用。
基于pako的gzip 压缩和解压缩,解决中英文乱码问题。可运行demo
基于Java的用GZIP压缩解压文件.zip
基于java的用GZIP压缩解压文件.zip
易语言GZIP数据解压缩源码,GZIP压缩,GZIP解压,InitDecompression,InitCompression,CreateDecompression,CreateCompression,ResetDecompression,ResetCompression,DestroyDecompression,DeInitCompression,...