我要整理在工作中用到的工具类分享出来,也方便自己以后查阅使用,这些工具类都是我自己实际工作中使用的

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();
}
}
}

java工具类——java将一串数据按照gzip方式压缩和解压缩的更多相关文章

  1. redis集群使用Java工具类(Java jedis集群工具类)

    package com.xiaomi.weather.vote.webservices.util.redisCache; import com.google.common.base.Strings; ...

  2. java工具类--数据库操作封装类

    java对数据库操作简单处理,如下代码即可,封装了 增删改查及获取连接.关闭连接. 代码如下: package com.test; import java.sql.Connection; import ...

  3. Java工具类——通过配置XML验证Map

    Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...

  4. 排名前 16 的 Java 工具类

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  5. JavaSE-基础语法(二)-系统类(java.lang.*)和工具类(java.util.*)

    系统类(java.lang.*)和工具类(java.util.*) 一.系统类(java.lang.*) 这个包下包含java语言的核心类,如String.Math.System和Thread类等,使 ...

  6. 排名前16的Java工具类

    原文:https://www.jianshu.com/p/9e937d178203 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法 ...

  7. 干货:排名前16的Java工具类

    在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码. 一. ...

  8. 常用高效 Java 工具类总结

    一.前言 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选取的5万个开源项目源码 ...

  9. 几种高效的Java工具类推荐

    本文将介绍了十二种常用的.高效的Java工具类 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类. 在开发中,使用这些工具类,不仅可以提高编码效率,还 ...

随机推荐

  1. 分别用命令行、NetBeans IDE 8.2实现firstcup 项目部署

    准备工作要搞好,对吧!(如下:) firstcup项目代码文件下载链接:click me~ NetBeans IDE 8.2下载链接:点我~(ps:建议下载此版本,再安装过程中,要选择安装GlassF ...

  2. html5 知识点简单总结03

    table表格 ----基本结构 table默认无边框(border) <table border="数值"> <tr> <th>表头</ ...

  3. Python基础:九、运算符

    一.赋值运算 简单的赋值运算符号:= c = a + b #将a+b的元算结果赋值给c 加法赋值运算符:+= c += a #等效于 c = c + a 减法赋值运算符:-= c -= a #等效于 ...

  4. 16. Antimalware (反病毒 3个)

    Malwarebytes反恶意软件是为Windows的恶意软件扫描程序. 作者声称使用各种技术来查找其他恶意软件扫描仪检测不到的恶意软件. 有一个有限选项的免费试用版和支持能够运行扫描计划的完整版本, ...

  5. 2019CVTE技术支持软件编程

    题目:找出长度为M的数组S中求和为1的任意三个数组合个数,举例:S=[-2, 0, 1, 2, -1, 3],结果为:3个([-2, 0, 3], [-2, 1, 2], [0, 2, -1]). 思 ...

  6. frost_vex_01

    int inc = 0; //整数inc等于0 while(inc < 6){ //inc在小于6的范围内递增 if(rand(@ptnum + inc + ch("seed" ...

  7. git 命令提交文件

    方法/步骤   打开要添加的文件的位置,右键,点击下面强调的内容,进入命令页面 先执行命git pull,这是提交的基本操作, git status,查看现在当前的文件状态 没有看到你现在要添加的文件 ...

  8. stolon cloud native postgresql 高可用方案

    stolon方案与patroni 类似,是一个新的pg ha 方案 包含的组件 keeper:它管理一个PostgreSQL实例,汇聚到由领导者sentinel计算的clusterview. sent ...

  9. tomcat配置ssl证书

    1.server.xml  打开已经注释掉的代码 然后添加证书地址 <Connector SSLEnabled="true" clientAuth="want&qu ...

  10. Linux基础入门-环境变量与文件查找

    一.环境变量: Shell中的变量也有不同的数据类型,不用专门指定类型名,可以参与运算,有作用域限制. declare tmp 创建一个变量 可以不用专门声明,可以即用即创建 tmp=shiyanlo ...