美文网首页
Java字符串压缩

Java字符串压缩

作者: JackSpeed | 来源:发表于2021-01-27 16:12 被阅读0次

    java 压缩字符串

    如果源字符串长度小于64,压缩后的字符会比源字符串长。
    例如:
    str.length()=32
    compressedStr.length()=36

     /**
         * 压缩字符串
         * @param str 要压缩的字符串
         * @return 压缩后的字符串
         */
        public static String compress(String str) {
            if (str == null || str.length() == 0) {
                return str;
            }
            GZIPOutputStream gzipOutputStream=null;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
                gzipOutputStream.write(str.getBytes());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (gzipOutputStream != null) {
                    try {
                        gzipOutputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return new sun.misc.BASE64Encoder().encode(byteArrayOutputStream.toByteArray());
        }
    
        /**
         * 解压缩
         * @param compressedStr 压缩后的字符串
         * @return 解压后的字符串
         */
        public static String uncompress(String compressedStr) {
            if (compressedStr == null) {
                return null;
            }
            byte[] compressed;
            String decompressed=null;
            ByteArrayInputStream byteArrayInputStream = null;
            GZIPInputStream gzipInputStream = null;
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            try {
                compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
                byteArrayInputStream = new ByteArrayInputStream(compressed);
                gzipInputStream = new GZIPInputStream(byteArrayInputStream);
                byte[] buffer = new byte[1024];
                int offset;
                while ((offset = gzipInputStream.read(buffer)) != -1) {
                    byteArrayOutputStream.write(buffer, 0, offset);
                }
                decompressed = byteArrayOutputStream.toString();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (gzipInputStream != null) {
                    try {
                        gzipInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (byteArrayInputStream != null) {
                    try {
                        byteArrayInputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return decompressed;
        }
    

    相关文章

      网友评论

          本文标题:Java字符串压缩

          本文链接:https://www.haomeiwen.com/subject/gugxphtx.html