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. bootstrap响应式布局列子

    <!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="utf-8&q ...

  2. [转帖]Vim编辑器使用方法详解

    Vim编辑器使用方法详解 程序员小新人学习 2018-12-16 12:26:23 转载于https://www.cnblogs.com/libaoliang/articles/6961676.htm ...

  3. 【JavaService】使用Java编写部署windows服务

    如果你玩windows系统,你对服务这个东西并不会陌生,服务可以帮我们做很多事情,在不影响用户正常工作的情况下,可以完成很多我们需要的需求. 众所周知,微软的visio studio内置的Servic ...

  4. quartz.net 3.x版本如何通过xml文件进行Job配置

    在2.x版本中,我们可以简单的在quartz.config文件中通过以下Xml配置方式来注册相应的Job以及触发器 quartz.plugin.xml.type = Quartz.Plugin.Xml ...

  5. mongoDB学习--建库、删库、插入、更新

    在讲之前我们说一下mongoDB的一些基本概念,我们对比关系型数据库能更直观的理解 SQL术语/概念 MongoDB术语/概念 说明 database database 数据库 table colle ...

  6. 【设计模式】—— 解释器模式Interpret

    前言:[模式总览]——————————by xingoo 模式意图 自定义某种语言后,给定一种文法标准,定义解释器,进行解析. 做过搜索的朋友们可能更了解一些,平时我们搜索所需要的词库,通常就需要用这 ...

  7. 【洛谷P1101】单词方阵

    题目大意:给一 \(n \times n\) 的字母方阵,内可能蕴含多个 \("yizhong"\) 单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着 8 个方向的任一方向, ...

  8. 收藏:IPicture总结

    1.IPicture接口对象的创建方法1:直接通过文件创建LPCSTR szFileUrl; IPicture *pIPicture; OleLoadPicturePath(CComBSTR(szFi ...

  9. scrapy 简单防封

    设置爬取间隔 setting.py from random import random DOWNLOAD_DELAY = random()* ps:此次的爬取间隔,在读取seeting文件确定,并非每 ...

  10. C根据排序字符串

    #include<stdio.h> #include<string.h> #include <stdlib.h> #define STR_LEN_MAX 100 c ...