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类的更多相关文章

  1. Java对zip格式压缩和解压缩

    Java对zip格式压缩和解压缩 通过使用java的相关类可以实现对文件或文件夹的压缩,以及对压缩文件的解压. 1.1 ZIP和GZIP的区别 gzip是一种文件压缩工具(或该压缩工具产生的压缩文件格 ...

  2. Java 的zip压缩和解压缩

    Java 的zip压缩和解压缩 好久没有来这写东西了,今天中秋节,有个东西想拿出来分享,一来是工作中遇到的问题,一来是和csdn问候一下,下面就分享一个Java中的zip压缩技术,代码实现比较简单,代 ...

  3. java工具类——java将一串数据按照gzip方式压缩和解压缩

    我要整理在工作中用到的工具类分享出来,也方便自己以后查阅使用,这些工具类都是我自己实际工作中使用的 import java.io.ByteArrayInputStream; import java.i ...

  4. [Java 基础] 使用java.util.zip包压缩和解压缩文件

    reference :  http://www.open-open.com/lib/view/open1381641653833.html Java API中的import java.util.zip ...

  5. Java用ZIP格式压缩和解压缩文件

    转载:java jdk实例宝典 感觉讲的非常好就转载在这保存! java.util.zip包实现了Zip格式相关的类库,使用格式zip格式压缩和解压缩文件的时候,须要导入该包. 使用zipoutput ...

  6. Java ZIP压缩和解压缩文件(解决中文文件名乱码问题)

    Java ZIP压缩和解压缩文件(解决中文文件名乱码问题) 学习了:http://www.tuicool.com/articles/V7BBvy 引用原文: JDK中自带的ZipOutputStrea ...

  7. IO操作之使用zip包压缩和解压缩文件

    转自:http://www.cdtarena.com/java.html​​Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作. 我们可以使 ...

  8. 使用commons-compress操作zip文件(压缩和解压缩)

    http://www.cnblogs.com/luxh/archive/2012/06/28/2568758.html Apache Commons Compress是一个压缩.解压缩文件的类库. 可 ...

  9. Huffman 压缩和解压缩java实现

    附上完整的代码 http://download.csdn.net/download/u010485034/7847447 Huffman编码原理这里就不说了,是.这里来讲讲利用Huffman编码来进行 ...

随机推荐

  1. ES6学习笔记(四):异步操作

    Promise Promise三种状态 pending.resolved.rejected 使用语法 var promis = new Promise(function(resolve,reject) ...

  2. 【跨域】jsonp跨域实现方法

    封装原生jsonp: 以跨域调取豆瓣电影最热榜单为例: function $jsonp(url,data,callback){ var funcName = 'jsonp_cb' + Math.ran ...

  3. Struts2 分割字符串标签s:generator

    有些时候会从后台返回一个字符串,可以通过Strut2的标签s:generator进行分割. generator标签,该标签可以将指定字符串按指定分隔符分割成多个字串.生成的多个字串可以用iterato ...

  4. Emmagee—开源Android性能测试工具

    下载:https://github.com/NetEase/Emmagee/releases/download/V2.5/Emmagee.apk 1.Emmagee——Android性能测试工具 Em ...

  5. bzoj 4448 [Scoi2015]情报传递 (树链剖分+主席树)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4448 题面: Description 奈特公司是一个巨大的情报公司,它有着庞大的情报网络 ...

  6. scheme 教程 #lang racket

    scheme 教程 #lang racket 来源  https://blog.csdn.net/yemeishenme/article/details/51471037 原文: https://le ...

  7. 【刷题】洛谷 P4782 【模板】2-SAT 问题

    题目背景 2-SAT 问题 模板 题目描述 有n个布尔变量 \(x_1\)​~\(x_n\)​,另有m个需要满足的条件,每个条件的形式都是"\(x_i\)​为true/false或\(x_j ...

  8. 前端学习 -- Html&Css -- 相对定位 绝对定位 固定定位

    相对定位 - 定位指的就是将指定的元素摆放到页面的任意位置,通过定位可以任意的摆放元素. - 通过position属性来设置元素的定位. -可选值: static:默认值,元素没有开启定位: rela ...

  9. POJ 3436 ACM Computer Factory (网络流,最大流)

    POJ 3436 ACM Computer Factory (网络流,最大流) Description As you know, all the computers used for ACM cont ...

  10. 【CH4201】楼兰图腾

    题目大意:给定一个长度为 N 的序列,从序列中任意挑出三个数,求满足中间的数字值最小(最大)有多少种情况. 题解:建立在值域上的树状数组,从左到右扫描一遍序列,统计出每个点左边有多少个数大于(小于)该 ...