用gzip解压缩数据流
//package com.nowjava; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; /** 来 自 NowJava.com **/ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.GZIPInputStream; public class Main { /* 来自 *时代Java公众号 - nowjava.com*/ private static final int BYTE_BUFFER = 1024; private static final String GZIP_FILE_SUFFIX = ".gz"; /** * uncompress data stream * @author Crow * @date 2015?6?19? * @version v0.1 * @param is input stream of bytes * @param os output stream of bytes * @throws IOException */ public static void ungzip(InputStream is, OutputStream os) throws IOException { GZIPInputStream gis = new GZIPInputStream(is); byte[] data = new byte[BYTE_BUFFER]; int length = -1; while ((length = gis.read(data)) != -1) { os.write(data, 0, length); } gis.close(); } /** * uncompress bytes * @author Crow * @date 2015?6?19? * @version v0.1 * @param content the bytes to be uncompressed * @return * @throws IOException */ public static byte[] ungzip(byte[] content) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(content); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ungzip(bis, bos); bos.flush(); byte[] result = bos.toByteArray(); bos.close(); bis.close(); return result; } public static boolean ungzip(File gzipFile) { return ungzip(gzipFile, true); } public static boolean ungzip(File gzipFile, boolean deleteSourceFile) { String fileName = gzipFile.getName(); if (fileName.endsWith(GZIP_FILE_SUFFIX)) { fileName = fileName.substring(0, fileName.lastIndexOf(GZIP_FILE_SUFFIX)); } else { fileName = "ungz-" + fileName; } File targetFile = new File(gzipFile.getParent(), fileName); return ungzip(gzipFile, targetFile, deleteSourceFile); } public static boolean ungzip(File gzipFile, File targetFile, boolean deleteSourceFile) { BufferedInputStream bis = null; try { bis = new BufferedInputStream(new FileInputStream(gzipFile)); } catch (FileNotFoundException e) { System.out.println("??????gz????????????????"); e.printStackTrace(); return false; } BufferedOutputStream bos = null; try { bos = new BufferedOutputStream(new FileOutputStream(targetFile)); } catch (FileNotFoundException e) { e.printStackTrace(); try { if (bis != null) bis.close(); } catch (IOException e1) { e1.printStackTrace(); } return false; } GZIPInputStream gis = null; try { gis = new GZIPInputStream(bis); byte[] data = new byte[BYTE_BUFFER]; int length = -1; while ((length = gis.re