gzip对字符串的压缩和解压
- package org.jc.plugins.gzip;
- 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;
- 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 sun.misc.BASE64Encoder().encode(out.toByteArray());
- }
- /**
- *
- * <p>
- * Description:使用gzip进行解压缩
- * </p>
- *
- * @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 sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
- 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 sun.misc.BASE64Encoder().encodeBuffer(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 sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
- 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;
- }
- }
gzip对字符串的压缩和解压的更多相关文章
- 使用zlib实现gzip格式数据的压缩和解压
注意代码中的注释部分,这里设置是专门针对gzip的,缺少了就不行了,gzip压缩格式和其他格式的区别就在这里. Bytef 就是 unsigned char,uLong就是 unsigned long ...
- 对数据进行GZIP压缩和解压
public class GzipUtils { /** * 对字符串进行gzip压缩 * @param data * @return * @throws IOException */ public ...
- C#实现通过Gzip来对数据进行压缩和解压
C#实现通过Gzip来对数据进行压缩和解压 internal static byte[] Compress(byte[] data) { using (var compressedStream = n ...
- VB6进行GZIP解压&C#进行GZIP压缩和解压
VB进行GZIP解压的,DLL是系统的,如果没有 [点击下载] Option Explicit 'GZIP API '----------------------------------------- ...
- 使用pako.js实现gzip的压缩和解压
poko.js可至Github下载:https://github.com/nodeca/pako 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
- Linux 时间日期类、搜索查找类、 压缩和解压类指令
l 时间日期类 date指令-显示当前日期 基本语法 1) date (功能描述:显示当前时间) 2) date +%Y (功能描述:显示当前年份) 3) date +%m (功能描述:显示当前月份) ...
- linux常用命令:4文件压缩和解压命令
文件压缩和解压命令 压缩命令:gzip.tar[-czf].zip.bzip2 解压缩命令:gunzip.tar[-xzf].unzip.bunzip2 1. 命令名称:gzip 命令英文原意:GNU ...
- .net文件压缩和解压及中文文件夹名称乱码问题
/**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...
- Linux下的压缩和解压
1. gzip, bzip2 能否直接压缩目录呢?不可以 2. 请快速写出,使用gzip和bzip2压缩和解压一个文件的命令.压缩:gzip 1.txt bzip2 1.txt解压:gzip -d 1 ...
随机推荐
- 剑指offer——python【第4题】重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- java基础 第八章课后习题
1.什么是二重循环?在内层循环中使用continue和break语句,程序如何跳转? 答:二重循环就是一个循环结构体内又包含另一个完整的循环结构. continue语句跳转时是跳过了内层循环中的剩余语 ...
- 简单数据库开发之dao层开发
数据库 dao层是用来与底层数据库连接的一系列代码,它因上层service层调用而调用底层数据库,因为一般的数据库不会只存在一到几张表格,所以必须定义出dao层的接口协议,方便各种表格的操作. dao ...
- docsis cm 上线过程(bigwhite)
扫描与同步下行(SYNC消息) 获取上行参数(UCD消息.MAP消息) 通过测距完成时间偏移等的调整(RNG消息) 设备类型鉴定(可选,DCI消息) 建立IP通道(DHCP) 同步系统时间(TOD ...
- RobotFramework环境配置:默认以管理员权限运行cmd
设置cmd以管理员权限运行 目的:创建或删除文件等命令时,需要管理员权限运行cmd(linux以root用户登录). 例如,创建日志目录. 方法一: 1.激活administrator用户 2 ...
- 上传及更新代码到github(以及如何在vscode上提交自己的代码)
上传本地代码 第一步:去github上创建自己的Repository,创建页面如下图所示: 红框为新建的仓库的https地址 第二步: echo "# Test" >> ...
- 关于ico图标
ico图标可以作为网页标签上显示的小logo,比如: 要获取一个网站的ico图标,只需要在url后输入/favicon.ico即可,比如 https://www.baidu.com/favicon ...
- Exp5 MSF基础应用 20164320 王浩
1. 实践目标 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.1一个主动攻击实践,如ms08_067; (1分) 1.2 一个针对浏览器的攻击, ...
- allegro17.2 错误记录
1.网表导入PCB时出现如下错误::error with pin number 'P11'in device ' lfbga176'::Unable to find pin name in adfnc ...
- python类与对象-如何让对象支持上下文管理
如何让对象支持上下文管理 问题举例 一个telnet客户端的类TelnetClient, 调用实例的connect(),login(),interact方法 启动客户端与服务器交互,交互完毕后需要调用 ...