将数据按照gzip当时解压的工具类
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* @author tanml
* 将一串数据按照gzip方式压缩和解压缩
*/
public class GZipUtils {
// 压缩
public static byte[] compress(byte[] data) throws IOException {
if (data == null || data.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(data);
gzip.close();
return out.toByteArray();//out.toString("ISO-8859-1");
}
public static byte[] compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return null;
}
return compress(str.getBytes("utf-8"));
}
// 解压缩
public static byte[] uncompress(byte[] data) throws IOException {
if (data == null || data.length == 0) {
return data;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(data);
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
gunzip.close();
in.close();
return out.toByteArray();
}
public static String uncompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
byte[] data = uncompress(str.getBytes("utf-8")); // ISO-8859-1
return new String(data);
}
/**
* @Title: unZip
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param unZipfile
* @param @param destFile 指定读取文件,需要从压缩文件中读取文件内容的文件名
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public static String unZip(String unZipfile, String destFile) {// unZipfileName需要解压的zip文件名
InputStream inputStream;
String inData = null;
try {
// 生成一个zip的文件
File f = new File(unZipfile);
ZipFile zipFile = new ZipFile(f);
// 遍历zipFile中所有的实体,并把他们解压出来
ZipEntry entry = zipFile.getEntry(destFile);
if (!entry.isDirectory()) {
// 获取出该压缩实体的输入流
inputStream = zipFile.getInputStream(entry);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] bys = new byte[4096];
for (int p = -1; (p = inputStream.read(bys)) != -1;) {
out.write(bys, 0, p);
}
inData = out.toString();
out.close();
inputStream.close();
}
zipFile.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return inData;
}
public static void main(String[] args){
String json = "{\"androidSdk\":22,\"androidVer\":\"5.1\",\"cpTime\":1612071603,\"cupABIs\":[\"armeabi-v7a\",\"armeabi\"],\"customId\":\"QT99999\",\"elfFlag\":false,\"id\":\"4a1b644858d83a98\",\"imsi\":\"460015984967892\",\"system\":true,\"systemUser\":true,\"test\":true,\"model\":\"Micromax R610\",\"netType\":0,\"oldVersion\":\"0\",\"pkg\":\"com.adups.fota.sysoper\",\"poll_time\":30,\"time\":1481634113876,\"timeZone\":\"Asia\\/Shanghai\",\"versions\":[{\"type\":\"gatherApks\",\"version\":1},{\"type\":\"kernel\",\"version\":9},{\"type\":\"shell\",\"version\":10},{\"type\":\"silent\",\"version\":4},{\"type\":\"jarUpdate\",\"version\":1},{\"type\":\"serverIps\",\"version\":1}]}";
json="ksjdflkjsdflskjdflsdfkjsdf";
try {
byte[] buf = GZipUtils.compress(json);
File fin = new File("D:/temp/test4.txt");
FileChannel fcout = new RandomAccessFile(fin, "rws").getChannel();
ByteBuffer wBuffer = ByteBuffer.allocateDirect(buf.length);
fcout.write(wBuffer.wrap(buf), fcout.size());
if (fcout != null) {
fcout.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
将数据按照gzip当时解压的工具类的更多相关文章
- 字符串GZIP压缩解压
c# /// <summary> /// 字符串压缩解压 /// </summary> public class Zipper { public static string C ...
- 对数据进行GZIP压缩和解压
public class GzipUtils { /** * 对字符串进行gzip压缩 * @param data * @return * @throws IOException */ public ...
- GZip 压缩解压 工具类 [ GZipUtil ]
片段 1 片段 2 pom.xml <dependency> <groupId>commons-codec</groupId> <artifactId> ...
- linux 压缩解压打包工具大集合
压缩.解压缩及归档工具有很多,今天小编就整理几个大家较为常用的. compress gzip bzip2 xz zip tar cpio 一.压缩.解压工具 用法 压缩 工具 压缩后 压缩包格式 解 ...
- linux命令:压缩解压打包工具大集合
目录 (1)zip 压缩.解压缩及归档工具有很多,今天小编就整理几个大家较为常用的. compress gzip bzip2 xz zip tar cpio 一.压缩.解压工具 用法 压缩 工具 压 ...
- PHP压缩与解压Zip(PHPZip类)
<?php class PHPZip { private $ctrl_dir = array(); private $datasec ...
- Android Studio 插件开发详解二:工具类
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自[赵彦军的博客] 在插件开发过程中,我们按照开发一个正式的项 ...
- 【转】Java压缩和解压文件工具类ZipUtil
特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...
- 数据持久化之SP的优化—送工具类
第一点:sp存储的是键值对 getSharedPreferences 第一个參数是你保存文件的名字,第个是保存的模式一般能够默觉得0 先看普通 使用SP 存储String类型字符串吧 SharedPr ...
随机推荐
- NOI2009 植物大战僵尸
啊一道好题感觉写得挺爽的啊这题这种有一点懵逼然后学了一点东西之后很明朗的感觉真是好!预处理参考 :http://www.cppblog.com/MatoNo1/archive/2014/11/01/1 ...
- bzoj 4199 [NOI2015]寿司晚宴
Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴. 在晚宴上,主办方为大家提供了 n−1 种不同 ...
- bzoj 3143: [Hnoi2013]游走
Description 一个无向连通图,顶点从1编号到N,边从1编号到M. 小Z在该图上进行随机游走,初始时小Z在1号顶点,每一步小Z以相等的概率随机选 择当前顶点的某条边,沿着这条边走到下一个顶点, ...
- 3.sass的数据类型与函数
数据类型 在sass里有数字.字符串.列表.颜色等类型 在cmd里 输入 sass -i 就会进入到交互模式,输入的计算可以马上得到结果 type-of()可以用来得到数据类型,如: type-of( ...
- The Hungarian Abhorrence Principle
原文:http://www.butunclebob.com/ArticleS.UncleBob.TheHungarianAbhorrencePrinciple The Hungarian Abh ...
- Handwritten Parsers & Lexers in Go (Gopher Academy Blog)
Handwritten Parsers & Lexers in Go (原文地址 https://blog.gopheracademy.com/advent-2014/parsers-lex ...
- Tomcat在windows系统中的防火墙设置
在Win7下安装Tomcat后,其他机器无法访问到Tomcat服务,需要修改防火墙设置. 控制面板->window防火墙->允许程序通过Windows防火墙通信 将Tomcat目录下\bi ...
- K:java中properties文件的读写
Properties类与.properties文件: Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集的类,不过Properties有特殊 ...
- css 背景色渐变兼容写法
最近在项目中,有很多地方都用到了线性渐变,比如:表单提交按钮的背景,数据展示的标题背景等等,按照以前的做法是切 1px 图片然后 repeat-x.下面我将介绍如何用 css 来完成该效果. css3 ...
- Python简单小程序练习
1.九九乘法表 #!/usr/bin/python for i in range(1,10): for j in range(i): j += 1 print ("%d * %d = %-2 ...