我要整理在工作中用到的工具類分享出來,也方便自己以後查閱使用,這些工具類都是我自己實際工作中使用的
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-1return 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 blocke.printStackTrace();}}}