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

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

最新文章

  1. 创建SSH Key连接github或gitlab
  2. TextView中的部分文字响应点击事件
  3. opencv直线检测在c#、Android和ios下的实现方法
  4. BZOJ1109 : [POI2007]堆积木Klo
  5. 夺命雷公狗---Thinkphp----16之首页的完成及全站的完成
  6. hdu 3501 Calculation 2 (欧拉函数)
  7. hdu4374One hundred layer (DP+单调队列)
  8. 逻辑运算符&& 用法解释
  9. C++线性序列容器<vector>简单总结
  10. 阿里云CentOS部署小笔记
  11. SpringBoot2.0 项目中使用事务
  12. 【转】JAVA反射与注解
  13. 从零开始学 Web 之 jQuery(一)jQuery的概念,页面加载事件
  14. 一个死循环导致的栈溢出实例:StackOverFlowError
  15. Android UI组件之自定义控件实现IP地址控件
  16. 【学习笔记】初识FreeMarker简单使用
  17. JAVA初学者(一)
  18. 发生在阿里云 SLB 4 层的一次故障记录
  19. vue-cli 搭建项目
  20. Google androd性能优化经典

热门文章

  1. org.jsoup.Jsoup找不到jar包问题解决思路
  2. 什么是C/S模式与B/S模式,两者区别与优缺点
  3. C语言简单计算一元二次方程
  4. Jump跳板机的搭建和部署
  5. 学习笔记TF067:TensorFlow Serving、Flod、计算加速,机器学习评测体系,公开数据集
  6. 学习笔记TF054:TFLearn、Keras
  7. vue-----样式绑定 事件处理
  8. Django Admin初识
  9. keepalived.conf配置说明
  10. adb和机顶盒一些常识