JAVA——GZIP压缩与解压缩
基本概念
GZIP編碼:GZIP最早由Jean-loup Gailly和Mark Adler創(chuàng)建,用于UNⅨ系統(tǒng)的文件壓縮。我們?cè)贚inux中經(jīng)常會(huì)用到后綴為.gz的文件,它們就是GZIP格式的。現(xiàn)今已經(jīng)成為Internet 上使用非常普遍的一種數(shù)據(jù)壓縮格式,或者說一種文件格式。
HTTP協(xié)議上的GZIP編碼是一種用來(lái)改進(jìn)WEB應(yīng)用程序性能的技術(shù)。大流量的WEB站點(diǎn)常常使用GZIP壓縮技術(shù)來(lái)讓用戶感受更快的速度。這一般是指WWW服務(wù)器中安裝的一個(gè)功能,當(dāng)有人來(lái)訪問這個(gè)服務(wù)器中的網(wǎng)站時(shí),服務(wù)器中的這個(gè)功能就將網(wǎng)頁(yè)內(nèi)容壓縮后傳輸?shù)絹?lái)訪的電腦瀏覽器中顯示出來(lái).一般對(duì)純文本內(nèi)容可壓縮到原大小的40%.這樣傳輸就快了,效果就是你點(diǎn)擊網(wǎng)址后會(huì)很快的顯示出來(lái).當(dāng)然這也會(huì)增加服務(wù)器的負(fù)載. 一般服務(wù)器中都安裝有這個(gè)功能模塊的。
解決方案
GZIP壓縮
public static byte[] compress(String str, String encoding) {if (str == null || str.length() == 0) {return null;}ByteArrayOutputStream out = new ByteArrayOutputStream();GZIPOutputStream gzip;try {gzip = new GZIPOutputStream(out);gzip.write(str.getBytes(encoding));gzip.close();} catch ( Exception e) {e.printStackTrace();}return out.toByteArray();}GZIP解壓
public static byte[] uncompress(byte[] bytes) {if (bytes == null || bytes.length == 0) {return null;}ByteArrayOutputStream out = new ByteArrayOutputStream();ByteArrayInputStream in = new ByteArrayInputStream(bytes);try {GZIPInputStream ungzip = new GZIPInputStream(in);byte[] buffer = new byte[256];int n;while ((n = ungzip.read(buffer)) >= 0) {out.write(buffer, 0, n);}} catch (Exception e) {e.printStackTrace();}return out.toByteArray();}工具類
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream;public class GZIPUtils {public static final String GZIP_ENCODE_UTF_8 = "UTF-8"; public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";public static byte[] compress(String str, String encoding) {if (str == null || str.length() == 0) {return null;}ByteArrayOutputStream out = new ByteArrayOutputStream();GZIPOutputStream gzip;try {gzip = new GZIPOutputStream(out);gzip.write(str.getBytes(encoding));gzip.close();} catch ( Exception e) {e.printStackTrace();}return out.toByteArray();}public static byte[] compress(String str) throws IOException { return compress(str, GZIP_ENCODE_UTF_8); }public static byte[] uncompress(byte[] bytes) {if (bytes == null || bytes.length == 0) {return null;}ByteArrayOutputStream out = new ByteArrayOutputStream();ByteArrayInputStream in = new ByteArrayInputStream(bytes);try {GZIPInputStream ungzip = new GZIPInputStream(in);byte[] buffer = new byte[256];int n;while ((n = ungzip.read(buffer)) >= 0) {out.write(buffer, 0, n);}} catch (Exception e) {e.printStackTrace();}return out.toByteArray();}public static String uncompressToString(byte[] bytes, String encoding) { if (bytes == null || bytes.length == 0) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayInputStream in = new ByteArrayInputStream(bytes); try {GZIPInputStream ungzip = new GZIPInputStream(in); byte[] buffer = new byte[256]; int n; while ((n = ungzip.read(buffer)) >= 0) { out.write(buffer, 0, n); } return out.toString(encoding);} catch (Exception e) {e.printStackTrace();}return null;}public static String uncompressToString(byte[] bytes) { return uncompressToString(bytes, GZIP_ENCODE_UTF_8); } public static void main(String[] args) throws IOException {String s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";System.out.println("字符串長(zhǎng)度:"+s.length());System.out.println("壓縮后::"+compress(s).length);System.out.println("解壓后:"+uncompress(compress(s)).length);System.out.println("解壓字符串后::"+uncompressToString(compress(s)).length());} }參考文章
java GZIP壓縮與解壓縮
總結(jié)
以上是生活随笔為你收集整理的JAVA——GZIP压缩与解压缩的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA——Unicode编码格式工具类
- 下一篇: 《编译原理》实验报告——TINY语言的词