java采用zip方式实现String的压缩和解压缩CompressStringUtil类
CompressStringUtil类:
不多说,直接贴代码:
/**
* 压缩
*
* @param paramString
* @return
*/
public static final byte[] compress(String paramString) throws Exception {
if (paramString == null)
return null;
ByteArrayOutputStream byteArrayOutputStream = null;
ZipOutputStream zipOutputStream = null;
byte[] arrayOfByte;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
zipOutputStream.putNextEntry(new ZipEntry("0"));
zipOutputStream.write(paramString.getBytes("GBK"));//这里采用gbk方式压缩,如果采用编译器默认的utf-8,这里就直接getByte();
zipOutputStream.closeEntry();
arrayOfByte = byteArrayOutputStream.toByteArray();
} catch (IOException e) {
arrayOfByte = null;
throw new Exception("压缩字符串数据出错", e);
} finally {
if (zipOutputStream != null)
try {
zipOutputStream.close();
} catch (IOException e) {
LOGGER.debug("关闭zipOutputStream出错", e);
}
if (byteArrayOutputStream != null)
try {
byteArrayOutputStream.close();
} catch (IOException e) {
LOGGER.error("关闭byteArrayOutputStream出错", e);
}
}
return arrayOfByte;
}
/**
* 解压缩
*
* @param compressed
* @return
*/
public static String decompress(byte[] compressed) throws Exception {
if (compressed == null)
return null;
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
ZipInputStream zin = null;
String decompressed;
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(compressed);
zin = new ZipInputStream(in);
zin.getNextEntry();
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = zin.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
decompressed = out.toString("GBK");//相应的这里也要采用gbk方式解压缩,如果采用编译器默认的utf-8,这里就直接toString()就ok了
} catch (IOException e) {
decompressed = null;
throw new Exception("解压缩字符串数据出错", e);
} finally {
if (zin != null) {
try {
zin.close();
} catch (IOException e) {
LOGGER.debug("关闭ZipInputStream出错", e);
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
LOGGER.error("关闭ByteArrayInputStream出错", e);
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
LOGGER.debug("关闭ByteArrayOutputStream出错", e);
}
}
}
return decompressed;
}
测试:
public static void main(String[] args) throws Exception {
byte[] aa = compress("czy爱wt");
System.out.println("压缩后字段" + aa);
System.out.println("解压缩后字段" + decompress(aa));
}
为了做个比较,下面贴出gzip方式压缩和解压缩的demo(网上扣的,不做保证能运行)
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream; // 将一个字符串按照zip方式压缩和解压缩
public class ZipUtil { // 压缩
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
return out.toString("ISO-8859-1");
} // 解压缩
public static String uncompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(str
.getBytes("ISO-8859-1"));
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// toString()使用平台默认编码,也可以显式的指定如toString("GBK")
return out.toString();
} // 测试方法
public static void main(String[] args) throws IOException {
System.out.println(ZipUtil.uncompress(ZipUtil.compress("中国China")));
} }
over。。。
java采用zip方式实现String的压缩和解压缩CompressStringUtil类的更多相关文章
- Java对zip格式压缩和解压缩
Java对zip格式压缩和解压缩 通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压. 1.1 ZIP和GZIP的区别 gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格 ...
- Java 的zip压缩和解压缩
Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...
- java工具类——java将一串数据按照gzip方式压缩和解压缩
我要整理在工作中用到的工具类分享出来,也方便自己以后查阅使用,这些工具类都是我自己实际工作中使用的 import java.io.ByteArrayInputStream; import java.i ...
- [Java 基础] 使用java.util.zip包压缩和解压缩文件
reference : http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...
- Java用ZIP格式压缩和解压缩文件
转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...
- Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)
Java ZIP压缩和解压缩文件(解决中文文件名乱码问题) 学习了:http://www.tuicool.com/articles/V7BBvy 引用原文: JDK中自带的ZipOutputStrea ...
- IO操作之使用zip包压缩和解压缩文件
转自:http://www.cdtarena.com/java.htmlJava API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使 ...
- 使用commons-compress操作zip文件(压缩和解压缩)
http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...
- Huffman 压缩和解压缩java实现
附上完整的代码 http://download.csdn.net/download/u010485034/7847447 Huffman编码原理这里就不说了,是.这里来讲讲利用Huffman编码来进行 ...
随机推荐
- 小程序开发 绑定自定义数据data- 及JS获取
1wxml<!-- 茶系显示隐藏函数sectionSelect --><view class="img-fur" data-id="{{item.id} ...
- 2013成都网赛1003 hdu 4730 We Love MOE Girls
题意:有一个字符串,若以"desu"结尾,则将末尾的"desu"替换为"nanodesu",否则在字符串末尾加上"nanodesu ...
- JavaFile类和递归
八.File类和递归 8.1 概述 java.io.File 类时文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和产出等操作. 8.2 构造方法 public File(String pa ...
- Tomcat如何开启SSL配置(https)
一.创建证书 证书用于客户端与服务端安全认证.我们可以使用JDK自带的keytool工具来生成证书.真正在产品环境中使用肯定要去证书提供商去购买,证书认证一般都是由VeriSign认证,官方地址:ht ...
- 【poj2187】最远点对(勉强凑数)
题目简述 输入n个点,及其坐标,n<=50000,所有坐标都是不超过10000的整数组成,没有重点. 问最远点对间的距离的平方是多少 题解 这是一道旋转卡壳的裸题 我们要求这个多边形的直径,这可 ...
- javaWeb接口开发
链接:http://blog.csdn.net/zxw136511485/article/details/51437115
- 人生效率手册:如何卓有成效地过好每一天--By张萌姐姐--读书笔记
读书笔记:<人生效率手册>:如何卓有成效地过好每一天--By张萌姐姐... 整本书看完的感受: 这本书主要讲的是生活中我们需要给自己一个目标,然后通过自己的努力去实现这个目标,书中说的很多 ...
- Hadoop生态圈-Flume的主流Sinks源配置
Hadoop生态圈-Flume的主流Sinks源配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 本篇博客只是配置的是Flume主流的Sinks,想要了解更详细的配置信息请参考官 ...
- Solr记录-solr检索和查询数据
Solr检索数据 在本章中,我们将讨论如何使用Java Client API检索数据.假设有一个名为sample.csv的.csv文档,其中包含以下内容. 001,9848022337,Hyderab ...
- Codeforces Round #540 Tanya and Candies 预处理
http://codeforces.com/contest/1118/problem/B 题目大意,给你一个序列,删去一个数值之后,要求剩下序列奇数和偶数的和相同,问有多少种删法. 思路:预处理奇数和 ...