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. 基于 Laravel 的 文件管理

    以 laravel 5.5 为例,框架集成了文件系统和云存储功能 可以实现文件夹列表.创建.重命名.删除,文件列表.上传.重命名.删除等操作 一.先进行配置 在 config 文件夹下有 filesy ...

  2. 【BOM】浏览器对象模型

    1.navigator :保存浏览器配置信息的对象 常用 navigator.plugins: 显示浏览器中所有插件信息的集合 navigator.cookieEnabled: 判断是否开启cooki ...

  3. 洛谷P2396 yyy loves Maths VII

    P2396 yyy loves Maths VII 题目背景 yyy对某些数字有着情有独钟的喜爱,他叫他们为幸运数字;然而他作死太多,所以把自己讨厌的数字成为"厄运数字" 题目描述 ...

  4. shell之三大文本处理工具grep、sed及awk

    grep.sed和awk都是文本处理工具,虽然都是文本处理工具单却都有各自的优缺点,一种文本处理命令是不能被另一个完全替换的,否则也不会出现三个文本处理命令了.只不过,相比较而言,sed和awk功能更 ...

  5. NOI前各种Idea总结以及各种文本乱堆

    转载请注明原文地址:https://www.cnblogs.com/LadyLex/p/9227267.html 不过这篇的确没什么*用了转转吧 2018-6-24 关于一类延迟标记(来自UR14 思 ...

  6. 【刷题】BZOJ 1487 [HNOI2009]无归岛

    Description Neverland是个神奇的地方,它由一些岛屿环形排列组成,每个岛上都生活着之中与众不同的物种.但是这些物种都有一个共同的生活习性:对于同一个岛上的任意两个生物,他们有且仅有一 ...

  7. 洛谷U19464 山村游历(Wander)(LCT)

    洛谷题目传送门 LCT维护子树信息常见套路详见我的总结 闲话 题目摘自WC模拟试题(by Philipsweng),原题目名Wander,"山村游历"是自己搞出来的中文名. 数据自 ...

  8. less深度作用域/deep/

    <style lang="less" scoped> .text-box { /deep/ input { width: 166px; text-align: cent ...

  9. 如何设置Ultraedit自动换行

    有时候这会非常麻烦, 要让Ultraedit自动换行请按发下方法: 1. 点击菜单栏的"高级→配置",找到"编辑器→自动换行/制表符设置". 2. 然后,把&q ...

  10. bzoj千题计划211:bzoj1996: [Hnoi2010]chorus 合唱队

    http://www.lydsy.com/JudgeOnline/problem.php?id=1996 f[i][j][0/1] 表示已经排出队形中的[i,j],最后一个插入的人在[i,j]的i或j ...