{"id":325,"date":"2023-02-25T06:20:22","date_gmt":"2023-02-24T22:20:22","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=325"},"modified":"2023-04-30T14:53:35","modified_gmt":"2023-04-30T06:53:35","slug":"java-implement-gzip-compression-and-decompression","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/25\/java-implement-gzip-compression-and-decompression\/","title":{"rendered":"Java\u5b9e\u73b0GZIP\u538b\u7f29\u4e0e\u89e3\u538b\u7f29"},"content":{"rendered":"<h3>Gzip\u538b\u7f29<\/h3>\n<pre><code class=\"language-java\">public static byte[] compress(String str, String charset) {\n    if (str == null || str.length() == 0) {\n        return null;\n    }\n    ByteArrayOutputStream out = new ByteArrayOutputStream();\n    GZIPOutputStream gzip;\n    try {\n        gzip = new GZIPOutputStream(out);\n        gzip.write(str.getBytes(charset));\n        gzip.close();\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n    return out.toByteArray();\n}<\/code><\/pre>\n<p><!-- more --><\/p>\n<h3>Gzip\u89e3\u538b\u7f29<\/h3>\n<pre><code class=\"language-java\">public static byte[] uncompress(byte[] bytes) {\n    if (bytes == null || bytes.length == 0) {\n        return null;\n    }\n    ByteArrayOutputStream out = new ByteArrayOutputStream();\n    ByteArrayInputStream in = new ByteArrayInputStream(bytes);\n    try {\n        GZIPInputStream unGzip = new GZIPInputStream(in);\n        byte[] buffer = new byte[256];\n        int n;\n        while ((n = unGzip.read(buffer)) &gt;= 0) {\n            out.write(buffer, 0, n);\n        }\n    } catch (Exception e) {\n        e.printStackTrace();\n    }\n    return out.toByteArray();\n}<\/code><\/pre>\n<h3>\u5de5\u5177\u4ee3\u7801\u96c6\u5408<\/h3>\n<pre><code class=\"language-java\">public class GzipUtils {\n\n    public static byte[] compress(String str, String charset) {\n        if (str == null || str.length() == 0) {\n            return null;\n        }\n        ByteArrayOutputStream out = new ByteArrayOutputStream();\n        GZIPOutputStream gzip;\n        try {\n            gzip = new GZIPOutputStream(out);\n            gzip.write(str.getBytes(charset));\n            gzip.close();\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n        return out.toByteArray();\n    }\n\n    public static byte[] compress(String str) throws IOException {\n        return compress(str, StandardCharsets.UTF_8.name());\n    }\n\n    public static byte[] uncompress(byte[] bytes) {\n        if (bytes == null || bytes.length == 0) {\n            return null;\n        }\n        ByteArrayOutputStream out = new ByteArrayOutputStream();\n        ByteArrayInputStream in = new ByteArrayInputStream(bytes);\n        try {\n            GZIPInputStream unGzip = new GZIPInputStream(in);\n            byte[] buffer = new byte[256];\n            int n;\n            while ((n = unGzip.read(buffer)) &gt;= 0) {\n                out.write(buffer, 0, n);\n            }\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n        return out.toByteArray();\n    }\n\n    public static byte[] uncompress(String str, String charset) throws IOException {\n        return uncompress(str.getBytes(charset));\n    }\n\n    public static byte[] uncompress(String str) throws IOException {\n        return uncompress(str.getBytes(StandardCharsets.UTF_8.name()));\n    }\n\n    public static String uncompressToString(byte[] bytes, String charset) {\n        if (bytes == null || bytes.length == 0) {\n            return null;\n        }\n        ByteArrayOutputStream out = new ByteArrayOutputStream();\n        ByteArrayInputStream in = new ByteArrayInputStream(bytes);\n        try {\n            GZIPInputStream unGzip = new GZIPInputStream(in);\n            byte[] buffer = new byte[256];\n            int n;\n            while ((n = unGzip.read(buffer)) &gt;= 0) {\n                out.write(buffer, 0, n);\n            }\n            return out.toString(charset);\n        } catch (Exception e) {\n            e.printStackTrace();\n        }\n        return null;\n    }\n\n    public static String uncompressToString(byte[] bytes) {\n        return uncompressToString(bytes, StandardCharsets.UTF_8.name());\n    }\n\n    public static String uncompressToString(String str, String inputCharset, String outputCharset) throws IOException {\n        return new String(uncompress(str, inputCharset), outputCharset);\n    }\n\n    public static String uncompressToString(String str) throws IOException {\n        return new String(uncompress(str, StandardCharsets.ISO_8859_1.name()), StandardCharsets.UTF_8.name());\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Gzip\u538b\u7f29 public static byte[] compress(String str, String [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[107],"class_list":["post-325","post","type-post","status-publish","format-standard","hentry","category-java-basic","tag-gzip"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/325","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=325"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/325\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=325"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=325"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=325"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}