采用jdk自带的gzip和zip方法进行压缩。并用apache common 的base64进行压缩字符串的转码。base64的转码需要导入apache的codec包,不要使用sun的base64转码包。
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.apache.commons.codec.binary.Base64; /** * author:mujunfeng * desc:字符串压缩/解压工具,提供gzip和zip两种方式,压缩后的字符串使用base64转码 * */ public class ZipUtils { /** * * 使用gzip进行压缩 */ public static String gzip(String primStr) { if (primStr == null || primStr.length() == 0) { return primStr; } ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = null; try { gzip = new GZIPOutputStream(out); gzip.write(primStr.getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if (gzip != null) { try { gzip.close(); } catch (IOException e) { e.printStackTrace(); } } } return new String(new Base64().encode(out.toByteArray())); } /** * 使用gzip进行解压缩 * @param compressedStr * @return 解压后的字符串 */ public static String gunzip(String compressedStr) { if (compressedStr == null) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = null; GZIPInputStream ginzip = null; byte[] compressed = null; String decompressed = null; try { compressed = new Base64().decode(compressedStr.getBytes()); in = new ByteArrayInputStream(compressed); ginzip = new GZIPInputStream(in); byte[] buffer = new byte[1024]; int offset = -1; while ((offset = ginzip.read(buffer)) != -1) { out.write(buffer, 0, offset); } decompressed = out.toString(); } catch (IOException e) { e.printStackTrace(); } finally { if (ginzip != null) { try { ginzip.close(); } catch (IOException e) { } } if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } return decompressed; } /** * 使用zip进行压缩 * * @param str * 压缩前的文本 * @return 返回压缩后的文本 */ public static final String zip(String str) { if (str == null) return null; byte[] compressed; ByteArrayOutputStream out = null; ZipOutputStream zout = null; String compressedStr = null; try { out = new ByteArrayOutputStream(); zout = new ZipOutputStream(out); zout.putNextEntry(new ZipEntry("0")); zout.write(str.getBytes()); zout.closeEntry(); compressed = out.toByteArray(); compressedStr = new String(new Base64().encode(compressed)); } catch (IOException e) { compressed = null; } finally { if (zout != null) { try { zout.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } return compressedStr; } /** * 使用zip进行解压缩 * * @param compressed * 压缩后的文本 * @return 解压后的字符串 */ public static final String unzip(String compressedStr) { if (compressedStr == null) { return null; } ByteArrayOutputStream out = null; ByteArrayInputStream in = null; ZipInputStream zin = null; String decompressed = null; try { byte[] compressed = new Base64().decode(compressedStr.getBytes()); 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(); } catch (IOException e) { decompressed = null; } finally { if (zin != null) { try { zin.close(); } catch (IOException e) { } } if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } return decompressed; } }
相关推荐
Java 字符串压缩和文件压缩代码,详细说明参考博客地址: https://blog.csdn.net/zhangmingziliao/article/details/77414760
java压缩字符串
运行程序之后输入任意的字符串,将字符串转化成二进制数字字符串,然后利用LZ78算法实现对二进制字符串压缩解压,最后再恢复原来的字符串
主要介绍了java字符串压缩解压示例,先压缩,再加密,再压缩,数据越大,压缩比例越高,需要的朋友可以参考下
字符串的压缩和解压,java语言编写,zip实现,代码编写
NULL 博文链接:https://mingren135.iteye.com/blog/2410538
Java实现压缩与解压缩ZIP import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream;...
关于经典算法--压缩字符串(将字符串内连续重复出现的字符进行压缩),个人的想法
quicklz cocos2dx 压缩 字符串压缩 效率又与zip
在C++编译环境下的字符串压缩,关于字符的压缩问题。
CompresszZipFile 中带是 zip解压 ZipFileUtil 是文件的压缩 和rar的解压
JAva方面操作字符串和文本的相关代码集,面向Java初学者,高手请绕行,这些实例都是挺简单的,可帮助初学者熟悉这方面的基础操作。
两种方法 对字符串进行压缩和解压缩,inflater和deflater。
字符串采用UTF-8编码获得byte数组,保证两端通用,如果应用对编码有要求,两端同时改为其他编码方式也可以 从Java和C#的代码看,两者代码上有细微差别,但是思路方面两者基本是一样的 另外一个备忘,Java里边,...
主要介绍了java实现压缩字符串和java字符串过滤,需要的朋友可以参考下
字符串-Java解题分析-学习资料.zip 是一...无论是初学者还是有一定经验的开发者,都可以从中获取有关Java字符串处理的实用知识和技能。 场景目标: 技能提升:帮助开发者提升解决字符串相关问题的能力,提高编程水平。
【免费题库】华为OD机试 - 一种字符串压缩表示的解压(Java & JS & Python & C & C++).html付费专栏内容,免费下载,多种语言解法
java端:返回类型非字符接口调用StringUtils#base64AndCompressJson进行编码压缩 ,返回类型为字符接口调用StringUtils#base64Andcompress js 端:引入压缩包中的js文件 ,调用deBase64AndUncompress进行解压解码
把xml文件转成一行字符串
java实现霍夫曼(huffman)树的压缩和解压缩,支持对文档的压缩和解压缩