Java使用gzip對字符串進行壓縮和解壓縮
public static String uncompressString(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 { String a = compressString("China"); System.out.println(a); System.out.println(a.length()); String b = uncompressString(a); System.out.println(b); }</pre>
本文由用戶 wgd7 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!