美文网首页
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字符串压缩

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

  • day18-使用赫夫曼算法实现文件压缩

    使用赫夫曼算法实现压缩 步骤: 压缩一个字符串 "i like like like java do you lik...

  • java-字符串压缩

    题目: 将字符串 aaabcdda (可以从控制台接收)编程实现将其转换为 3a1b1c2d1a。 算法思想: 遍...

  • 1394-字符串压缩

    字符串压缩 题目 字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabc...

  • Java字符串压缩和解压缩源码

    在做工程过程中,把做工程过程较好的代码片段做个收藏,下边资料是关于Java字符串压缩和解压缩的代码,希望能对大家有...

  • 字符串压缩算法

    题目描述:用Java实现一个字符串压缩算法 Input : "aaabbacc"Output:"3a2b1a2c"

  • LeetCode | 面试题 01.06. 字符串压缩【Pyth

    LeetCode 面试题 01.06. 字符串压缩【Easy】【Python】【双指针】 问题 力扣 字符串压缩。...

  • java对字符串进行压缩

    话不多说直接上代码 测试结果 补充 该程序使用的zip压缩算法,具体参考文章[https://www.ngui.c...

  • 2020-03-16 刷题1(字符串)

    01.06 字符串压缩 标签:字符串,内存题目其实很简单,用模拟法模拟字符串的压缩过程即可。但是我提交了三次,因为...

  • LeetCode 面试题 01.06. 字符串压缩

    题目 字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaa...

网友评论

      本文标题:Java字符串压缩

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