对数据进行GZIP压缩或解压缩
/**
* 对data进行GZIP解压缩
* @param data
* @return
* @throws Exception
*/
public static String unCompress(byte[] data) throws Exception {
if (null == data && data.length <= 0) {
return null;
}
String reString = "";
try {
//创建一个新的byte数组输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
//创建一个byte数组输入流
ByteArrayInputStream in = new ByteArrayInputStream(data);
//创建gzip输入流
GZIPInputStream gzip = new GZIPInputStream(in);
byte[] buf = new byte[1024];
int len = 0;
while ((len = gzip.read(buf)) >= 0) {
out.write(buf, 0, len);
}
// 使用指定的 charsetName,通过解码字节将缓冲区内容转换为字符串
reString = out.toString("UTF-8");
out.close();
in.close();
gzip.close();
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return reString;
} /**
* 对字符串进行gzip压缩
* @param data
* @return
* @throws IOException
*/
public static String compress(String data) throws Exception {
if (null == data || data.length() <= 0) {
return data;
}
//创建一个新的byte数组输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
//使用默认缓冲区大小创建新的输出流
GZIPOutputStream gzip = new GZIPOutputStream(out);
//将b.length个字节写入此输出流
gzip.write(data.getBytes());
gzip.flush();
gzip.close(); //使用指定的charsetName,通过解码字节将缓冲区内容转换为字符串
return out.toString("ISO-8859-1");
}
对数据进行GZIP压缩或解压缩的更多相关文章
- GZIP压缩、解压缩工具类
GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...
- 对数据进行GZIP压缩和解压
public class GzipUtils { /** * 对字符串进行gzip压缩 * @param data * @return * @throws IOException */ public ...
- GZip 压缩及解压缩
/// <summary> /// GZipHelper /// </summary> public class GZipHelper { /// <summary> ...
- GZip压缩与解压缩
GZIP的压缩与解压缩代码: public static class CompressionHelper { /// <summary> /// Compress the byte[] / ...
- java GZIP压缩与解压缩
1.GZIP压缩 public static byte[] compress(String str, String encoding) { if (str == null || str.length( ...
- 将PHP程序中返回的JSON格式数据用gzip压缩输出
//phpinfo(); 搜索下 zlib 是否开启 //此示例开启压缩 Content-Length:124 Size: 404B //未开启gzip压缩 Content-Length:675 Si ...
- 压缩与解压缩 gzip bzip2 tar 命令
gzip压缩与解压缩 命令 gzip -v 解压缩 gzip-d 操作如下. 压缩 .可以看到源文件有5171大小,压缩后,变成了1998大小. 解压缩 .解压缩之后可以看到,原来的man_db ...
- Okhttp3请求网络开启Gzip压缩
前沿 首先OkHttp3是支持Gzip解压缩的,不过我们要明白,它是支持我们在发起请求的时候自动加入header,Accept-Encoding: gzip,而我们的服务器返回的时候header中有C ...
- Java Web 减少网络 IO、静态资源磁盘 IO 有效的办法--响应使用 GZIP( 压缩http请求与响应gzip压缩)
(转载http://blog.csdn.net/hylclxy/article/details/7779662) 出于节约流量考虑, 客户端在向服务端发送request的时候对post数据进行gzip ...
随机推荐
- TeXstudio+TexLive交叉引用
LaTeX 交叉引用系统简介 https://www.cnblogs.com/wenbosheng/p/9537774.html 一般来说需要两次运行排版命令才能生成引用编号.背后的原理是这样的,第一 ...
- Angular4.0 项目报错:Unexpected value xxxComponent' declared by the module 'xxxxModule'. Please add a @Pipe...
最近刚刚开始学习angular 4.0,在网上找了一个小项目教程学习,然而学习的过程有点艰辛,,各种报错,我明明就是按照博主的步骤老老实实走的呀!!话不多说,上bug- .- Uncaught Er ...
- Behavior Question - Most challenging project.
介绍项目,challenging的地方 The most challenging project I have ever done was an online collaborative coding ...
- scala操作HBase2.0
在前面: scala:2.12 hbase:2.0.2 开发工具:IDEA 准备工作: 1.将生产上的hbase中的conf/hbase-site.xml文件拷贝到idea中的src/resource ...
- Linux - 7种运行级别
目录:etc/rc.d/init.d 1. linux开机过程 2. 运行级别(0-6) 存储位置 etc/inittab,开机加载,也可以用命令init [数字]切换. # 0 - 停机(默认时为0 ...
- 64位win7连接ACCESS报错“Microsoft.Jet.OLEDB.4.0”问题
本人开发了桌面软件,关于事务提醒,自己一直在用,很喜欢,但是在64位win7上无法运行,网上说多种方法,都觉得麻烦.后来自己在vs2012下编译成x86版本,结果可以运行在64位win7下了. 原来如 ...
- Java toBinaryString()函数探究及Math.abs(-2147483648)=-2147483648原理探究
toBinaryString()函数 public class Customer { public static void main(String[] args) { int m=-8; System ...
- oracle 导出某用户下的表
exp test/test@orcl owner=test file=E:/all.dmp
- jq demo 点击弹窗,居中,可滚动,可拖动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- LAMP架构(二)
第十八次课 LAMP架构(二) 目录 一.Apache默认虚拟主机 二.Apache用户认证 三.域名跳转 四.Apache访问日志 五.访问日志不记录静态文件 六.访问日志切割 七.静态元素过期时间 ...