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当时解压的工具类的更多相关文章

  1. 字符串GZIP压缩解压

    c# /// <summary> /// 字符串压缩解压 /// </summary> public class Zipper { public static string C ...

  2. 对数据进行GZIP压缩和解压

    public class GzipUtils { /** * 对字符串进行gzip压缩 * @param data * @return * @throws IOException */ public ...

  3. GZip 压缩解压 工具类 [ GZipUtil ]

    片段 1 片段 2 pom.xml <dependency> <groupId>commons-codec</groupId> <artifactId> ...

  4. linux 压缩解压打包工具大集合

    压缩.解压缩及归档工具有很多,今天小编就整理几个大家较为常用的. compress gzip  bzip2 xz zip tar cpio 一.压缩.解压工具 用法 压缩 工具 压缩后 压缩包格式 解 ...

  5. linux命令:压缩解压打包工具大集合

    目录 (1)zip 压缩.解压缩及归档工具有很多,今天小编就整理几个大家较为常用的. compress gzip  bzip2 xz zip tar cpio 一.压缩.解压工具 用法 压缩 工具 压 ...

  6. PHP压缩与解压Zip(PHPZip类)

    <?php     class PHPZip     {         private $ctrl_dir     = array();         private $datasec    ...

  7. Android Studio 插件开发详解二:工具类

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自[赵彦军的博客] 在插件开发过程中,我们按照开发一个正式的项 ...

  8. 【转】Java压缩和解压文件工具类ZipUtil

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  9. 数据持久化之SP的优化—送工具类

    第一点:sp存储的是键值对 getSharedPreferences 第一个參数是你保存文件的名字,第个是保存的模式一般能够默觉得0 先看普通 使用SP 存储String类型字符串吧 SharedPr ...

随机推荐

  1. ArcGIS API for JavaScript 4.2学习笔记[8] 2D与3D视图同步

    同一份数据不同视图查看可能用的比较少,因为3D视图放大很多后就和2D地图差不多了,畸变很小,用于超大范围的地图显示时有用,很多时候都是在平面地图上进行分析.查询.操作.教学需要可能会对这个有要求? 本 ...

  2. 开启tomcat的apr模式,并利用redis做tomcat7的session的共享。

    更新系统组件 yum -y install readline* xmlto kernel-devel yum* screen vim* psmisc wget lrzsz pcre-devel lib ...

  3. websocket教程(一) 非常有趣的理解websocket

    一.websocket与http WebSocket是HTML5出的东西(协议),也就是说HTTP协议没有变化,或者说没关系,但HTTP是不支持持久连接的(长连接,循环连接的不算) 首先HTTP有 1 ...

  4. Struts2-整理笔记(五)拦截器、拦截器配置

    拦截器(Interceptor) 拦截器是Struts2最强大的特性之一,它是一种可以让用户在Action执行之前和Result执行之后进行一些功能处理的机制. 拦截器的优点 简化了Action的实现 ...

  5. Eclipse配置tomcat程序发布到哪里去了?

    今天帮同事调一个问题,明明可以main函数执行的,他非要固执的使用tomcat执行,依他.但是发布到tomcat之后我想去看看发布后的目录,所以就打开了tomcat中的webapps目录,可是并没有发 ...

  6. POI 导出导入工具类介绍

    介绍: Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI (POI ...

  7. js 类数组arguments详解

    arguments并不是一个真正的数组,而是一个"类似数组(array-like)"的对象: 就像下面的这段输出,就是典型的类数组对象: [, , callee: ƒ, Symbo ...

  8. Android studio打开项目一直卡住

    修改/gradle/wrapper/gradle-wrapper.properties文件中的最后一行distributionUrl=:(可找一个可用项目的复制过来)

  9. css边框内圆角

    一.使用两个元素实现 html <div class="parent"> <div class="inset-radius">时代峰峻胜 ...

  10. iOS 动画篇 之 Core Animation (一)

    iOS中实现动画有两种方式,一种是自己不断的通过drawRect:方法来绘制,另外一种就是使用核心动画(Core Animation). 导语: 核心动画提供高帧速率和流畅的动画,而不会增加CPU的负 ...