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()); } }
java GZIP压缩与解压缩的更多相关文章
- GZIP压缩、解压缩工具类
GZIP压缩.解压缩工具类: public class GZIPUtiles { public static String compress(String str) throws IOExceptio ...
- GZip 压缩及解压缩
/// <summary> /// GZipHelper /// </summary> public class GZipHelper { /// <summary> ...
- 对数据进行GZIP压缩或解压缩
/** * 对data进行GZIP解压缩 * @param data * @return * @throws Exception */ public static String unCompress( ...
- GZip压缩与解压缩
GZIP的压缩与解压缩代码: public static class CompressionHelper { /// <summary> /// Compress the byte[] / ...
- java GZIP压缩和解压
最近碰到了一个按GZIP解压指定的输入流数据,备份下 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream ...
- java gzip压缩与解压
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...
- Java实现压缩与解压缩
import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...
- JAVA中压缩与解压缩
以压缩Zip文件为例.主要是通过ZipOutputStream类实现.解压缩主要使用ZipFile类和ZipInputStream以及ZipEntry类. package main; import j ...
- java 版本压缩、解压缩zip
import java.io.*; import java.util.*; import java.util.zip.ZipOutputStream; import java.util.zip.Zip ...
随机推荐
- hdu2767(图的强连通)
//题意:问需要添加几条边使得这张图成为每个点都等价(强连通图) 我们先把图中的强连通分量缩点 可能他本身就是满足条件,那么直接输出0 经过缩点后,就可以把强连通分量看成一个个独立的点,在这张图上搞一 ...
- 调试的时候禁止chrome缓存图片
https://blog.csdn.net/yiifaa/article/details/54290047 https://blog.csdn.net/xinghuo0007/article/deta ...
- 有哪些值得学习的spring boot开源项目?
1. awesome-spring-boot 首先给大家介绍的就是Spring Boot 中文索引,这是一个专门收集 Spring Boot 相关资料的开源项目,也有对应的导航页面. 产品主页 htt ...
- hdu 2818 Building Block 种类并查集
在进行并的时候不能瞎jb并,比如(x, y)就必须把x并给y ,即fa[x] = y #include <iostream> #include <string> #includ ...
- [转]Adaboost 算法的原理与推导
看了很多篇解释关于Adaboost的博文,觉得这篇写得很好,因此转载来自己的博客中,以便学习和查阅. 原文地址:<Adaboost 算法的原理与推导>,主要内容可分为三块,Adaboost ...
- 520 Detect Capital 检测大写字母
给定一个单词,你需要判断单词的大写使用是否正确.我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如&q ...
- RHEL6.5----LVS(NAT)
主机名 IP 所需软件 master 192.168.30.130(Nat) 192.168.17.130(VMnet4) ipvsadm node-1 192.168.17.131 http ...
- Keepalived+Nginx实现Nginx的高可用
集群规划 主机名 IP VIP Nginx:port KeepAlived主备 KA_NG_01 192.168.30.130 192.168.30.120 8088 MASTER KA_NG_02 ...
- 机器学习概念之特征选择(Feature selection)之RFormula算法介绍
不多说,直接上干货! RFormula算法介绍: RFormula通过R模型公式来选择列.支持R操作中的部分操作,包括‘~’, ‘.’, ‘:’, ‘+’以及‘-‘,基本操作如下: 1. ~分隔目标和 ...
- 动手实现 React-redux(六):React-redux 总结
到这里大家已经掌握了 React-redux 的基本用法和概念,并且自己动手实现了一个 React-redux,我们回顾一下这几节都干了什么事情. React.js 除了状态提升以外并没有更好的办法帮 ...