字串的壓縮和解壓縮
資料傳輸時,有時需要將資料壓縮和解壓縮,本例使用GZIPOutputStream/GZIPInputStream實現。
1、使用ISO-8859-1作為中介編碼,可以保證準確還原資料
2、字元編碼確定時,可以在uncompress方法最後一句中顯式指定編碼
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
// 將一個字串按照zip方式壓縮和解壓縮
public class ZipUtil {
// 壓縮
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
return out.toString("ISO-8859-1");
}
// 解壓縮
public static String uncompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(str
.getBytes("ISO-8859-1"));
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// toString()使用平臺預設編碼,也可以顯式的指定如toString("GBK")
return out.toString();
}
// 測試方法
public static void main(String[] args) throws IOException {
System.out.println(ZipUtil.uncompress(ZipUtil.compress("中國China")));
}
}
1、使用ISO-8859-1作為中介編碼,可以保證準確還原資料
2、字元編碼確定時,可以在uncompress方法最後一句中顯式指定編碼
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
// 將一個字串按照zip方式壓縮和解壓縮
public class ZipUtil {
// 壓縮
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
return out.toString("ISO-8859-1");
}
// 解壓縮
public static String uncompress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(str
.getBytes("ISO-8859-1"));
GZIPInputStream gunzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = gunzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
// toString()使用平臺預設編碼,也可以顯式的指定如toString("GBK")
return out.toString();
}
// 測試方法
public static void main(String[] args) throws IOException {
System.out.println(ZipUtil.uncompress(ZipUtil.compress("中國China")));
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12921506/viewspace-259944/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- JAVA壓縮和解壓縮Java
- zip壓縮和解壓縮
- 簡單的zip壓縮和解壓縮
- linux壓縮和解壓縮命令Linux
- linux壓縮和解壓縮命令整理Linux
- linux壓縮和解壓縮命令大全Linux
- Python實現壓縮和解壓縮Python
- Linux中檔案的壓縮和解壓縮Linux
- Linux tar壓縮和解壓縮等命令Linux
- linux下的檔案的壓縮和解壓縮Linux
- 常用的壓縮和解壓命令案例
- pigz更快的壓縮和解壓工具
- 【Linux基礎】壓縮和解壓Linux
- linux檔案壓縮和解壓命令Linux
- linux壓縮和解壓命令總結Linux
- zip壓縮檔案處理方案(Zip4j壓縮和解壓)
- 基本字串壓縮字串
- pclzip壓縮檔案和解壓的多種示例
- 強大且易於使用的壓縮和解壓縮軟體:Keka for MacMac
- Nginx網路壓縮 CSS壓縮 圖片壓縮 JSON壓縮NginxCSSJSON
- C# 壓縮和解壓檔案(SharpZipLib)C#
- 字串壓縮(一)之ZSTD字串
- linux壓縮解壓縮Linux
- JS壓縮方法及批量壓縮JS
- aix 下壓縮與解壓縮AI
- tar 分卷壓縮&解壓縮命令
- AIX 上壓縮與解壓縮AI
- aix 檔案的壓縮與解壓縮AI
- 8-字串的壓縮儲存字串
- 壓縮字串《演算法很美》字串演算法
- 『學了就忘』Linux基礎命令 — 32、壓縮和解壓縮相關命令Linux
- PAT-B 1078 字串壓縮與解壓【字串】字串
- 使用org.apache.tools.zip實現zip壓縮和解壓Apache
- linux下壓縮解壓縮命令Linux
- Linux壓縮及解壓縮命令Linux
- linux 高效壓縮工具之xz的壓縮解壓使用Linux
- Linux下常用壓縮格式的壓縮與解壓方法Linux
- ppt怎麼壓縮,ppt壓縮的技巧分享