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. OpenStack运维(三):OpenStack存储节点和配置管理

    1.对象存储节点维护 1.1 重启存储节点 如果一个存储节点需要重启,直接重启即可. 1.2 关闭存储节点 如果一个存储节点需要关闭很长一段时间,可以考虑将该节点从存储环中移除. swift-ring ...

  2. sql经典试题

    1.一道SQL语句面试题,关于group by表内容:2005-05-09 胜2005-05-09 胜2005-05-09 负2005-05-09 负2005-05-10 胜2005-05-10 负2 ...

  3. ActiveMQ (二) 常用配置简介

    ActiveMQ的主要配置文件 ActiveMQ的一些常用的属性很多可以在对应的配置文件中进行配置的.比如访问web console的管理端的端口,用户名密码,连接MQ时的用户名和密码,持久化设置,是 ...

  4. QT中几个函数的使用方法

    一.把字符串转换成整形demo1:QString str = "FF";bool ok;int hex = str.toInt(&ok, 16); // hex == 25 ...

  5. 在表格中,th scope="row"和th scope="col"中的scope属性的用法及意义

    把表头和数据联系起来:scope,id,headers属性就我用到现在,很多表格要比上面提供的例子复杂的多.让例子复杂一点,我会移去"Company"表头,并且把第一列的数据移到表 ...

  6. C# 控件拖动

    https://zhidao.baidu.com/question/2116834779399609987.html [DllImport("user32.dll", EntryP ...

  7. Debian安装fail2ban来防止扫描

    vps的root密码不要设置的太简单,这样很容易被攻破,你可以安装如下软件来降低vps被攻破的机会. 输入如下命令: apt-get install fail2ban 提示如下表示安装完成: root ...

  8. StringMVC @RequestParam属性

    1.jsp: <a href="springmvc/testRequestParam?username=allen&age=sss">test RequsetP ...

  9. JS中date日期初始化的5种方法

    创建一个日期对象: 代码如下: var objDate=new Date([arguments list]); 参数形式有以下5种: 1)new Date("month dd,yyyy hh ...

  10. TensorFlow实现knn(k近邻)算法

    首先先介绍一下knn的基本原理: KNN是通过计算不同特征值之间的距离进行分类. 整体的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于 ...