java中GZIP壓縮解壓類使用實(shí)例
java中GZIP壓縮解壓類使用實(shí)例
當(dāng)我們客戶端與服務(wù)端進(jìn)行數(shù)據(jù)傳輸時(shí)需要走流量,為了節(jié)省流量我們常常需要寫一個(gè)壓縮類對(duì)數(shù)據(jù)進(jìn)行壓縮。
實(shí)例代碼:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* GZIP壓縮解壓類
*/
public class MessageGZIP {
private static String encode = "utf-8";//"ISO-8859-1"
public String getEncode() {
return encode;
}
/*
* 設(shè)置 編碼,默認(rèn)編碼:UTF-8
*/
public void setEncode(String encode) {
MessageGZIP.encode = encode;
}
/*
* 字符串壓縮為字節(jié)數(shù)組
*/
public static byte[] compressToByte(String str){
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(encode));
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
/*
* 字符串壓縮為字節(jié)數(shù)組
*/
public static byte[] compressToByte(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 (IOException e) {
e.printStackTrace();
}
return out.toByteArray();
}
/*
* 字節(jié)數(shù)組解壓縮后返回字符串
*/
public static String uncompressToString(byte[] b) {
if (b == null || b.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(b);
try {
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (IOException e) {
e.printStackTrace();
}
return out.toString();
}
/*
* 字節(jié)數(shù)組解壓縮后返回字符串
*/
public static String uncompressToString(byte[] b, String encoding) {
if (b == null || b.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(b);
try {
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
return out.toString(encoding);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
MyBatis自定義映射resultMap的實(shí)現(xiàn)
本文主要介紹了MyBatis自定義映射resultMap的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
利用線程實(shí)現(xiàn)動(dòng)態(tài)顯示系統(tǒng)時(shí)間
編寫Applet小程序,通過在HTML文檔中接收參數(shù),顯示當(dāng)前的系統(tǒng)時(shí)間,需要的朋友可以參考下2015-10-10
Exception in thread main java.lang.NoClassDefFoundError錯(cuò)誤解決方
這篇文章主要介紹了Exception in thread main java.lang.NoClassDefFoundError錯(cuò)誤解決方法,需要的朋友可以參考下2016-08-08
response.setContentType()參數(shù)以及作用詳解
這篇文章主要介紹了response.setContentType()參數(shù)以及作用詳解,本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Spring中BeanFactory和ApplicationContext的作用和區(qū)別(推薦)
這篇文章主要介紹了Spring中BeanFactory和ApplicationContext的作用和區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
高并發(fā)環(huán)境下安全修改同一行數(shù)據(jù)庫(kù)數(shù)據(jù)的策略分享
隨著互聯(lián)網(wǎng)技術(shù)的發(fā)展,越來越多的應(yīng)用需要在高并發(fā)環(huán)境中運(yùn)行,數(shù)據(jù)庫(kù)的并發(fā)控制成為了業(yè)務(wù)的關(guān)鍵,本文將介紹如何在高并發(fā)情況下,安全地修改數(shù)據(jù)庫(kù)中的同一行數(shù)據(jù),需要的可以參考一下2023-06-06
在springboot中注入FilterRegistrationBean不生效的原因
這篇文章主要介紹了在springboot中注入FilterRegistrationBean不生效的原因及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08

