JAVA——GZIP压缩与解压缩
生活随笔
收集整理的這篇文章主要介紹了
JAVA——GZIP压缩与解压缩
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
基本概念
GZIP編碼:GZIP最早由Jean-loup Gailly和Mark Adler創建,用于UNⅨ系統的文件壓縮。我們在Linux中經常會用到后綴為.gz的文件,它們就是GZIP格式的。現今已經成為Internet 上使用非常普遍的一種數據壓縮格式,或者說一種文件格式。
HTTP協議上的GZIP編碼是一種用來改進WEB應用程序性能的技術。大流量的WEB站點常常使用GZIP壓縮技術來讓用戶感受更快的速度。這一般是指WWW服務器中安裝的一個功能,當有人來訪問這個服務器中的網站時,服務器中的這個功能就將網頁內容壓縮后傳輸到來訪的電腦瀏覽器中顯示出來.一般對純文本內容可壓縮到原大小的40%.這樣傳輸就快了,效果就是你點擊網址后會很快的顯示出來.當然這也會增加服務器的負載. 一般服務器中都安裝有這個功能模塊的。
解決方案
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("字符串長度:"+s.length());System.out.println("壓縮后::"+compress(s).length);System.out.println("解壓后:"+uncompress(compress(s)).length);System.out.println("解壓字符串后::"+uncompressToString(compress(s)).length());} }參考文章
java GZIP壓縮與解壓縮
總結
以上是生活随笔為你收集整理的JAVA——GZIP压缩与解压缩的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA——Unicode编码格式工具类
- 下一篇: 《编译原理》实验报告——TINY语言的词