• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
小折腾
博客园    首页    新随笔    联系   管理    订阅  订阅

compression and décompression

/**
     * Compress a string and add the header "zip:"
     *
     * @param strToCompress the string to compress.
     * @return the string compressed, NULL if an error occurs.
     */
    public static String compress(String strToCompress) {

        String out = null;
        ByteArrayOutputStream bos = null;
        Deflater compressor = null;
        try {
            byte[] input = strToCompress.getBytes("UTF-8");

            // Set the highest level of compression
            compressor = new Deflater();
            compressor.setLevel(Deflater.BEST_COMPRESSION);

            // Give the compressor the data to compress
            compressor.setInput(input);
            compressor.finish();

            // Create an expandable byte array to hold the compressed data.
            bos = new ByteArrayOutputStream(input.length);

            // Compress the data
            byte[] buf = new byte[1024];
            while (!compressor.finished()) {
                int count = compressor.deflate(buf);
                bos.write(buf, 0, count);
            }

            // Get the compressed data
            byte[] bytesCompressed = bos.toByteArray();
            out = new String(Base64.encodeBase64(bytesCompressed), "UTF-8");
            out = "zip:" + out;

        } catch (UnsupportedEncodingException e) {
            LOGGER.error("UnsupportedEncodingException in compress method. null is returned: " + e);
            return null;
        } finally {
            if (compressor != null) {
                compressor.end();
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    LOGGER.error("IOException in compress method. null is returned: " + e);
                    return null;
                }
            }
        }
        return out;
    }

 

 

/**
     * Uncompress a string if the input string has the header "zip:".
     *
     * @param strToUncompress the string to be uncompressed.
     * @return return the uncompressed string if the input string has the header "zip:", return the input string otherwise, return NULL if an error occurs.
     */
    public static String uncompress(String strToUncompress) {
        // Return the input string if the input string doesn't have the header "zip:"
        if (!strToUncompress.startsWith("zip:")) {
            return strToUncompress;
        }
        // Remove the header "zip:"
        strToUncompress = strToUncompress.substring(4);
        String out = null;
        ByteArrayOutputStream bos = null;
        Inflater decompressor = null;
        try {
            byte[] compressedData = Base64.decodeBase64(strToUncompress.getBytes("UTF-8"));

            // Create the decompressor and give it the data to compress
            decompressor = new Inflater();
            decompressor.setInput(compressedData);

            // Create an expandable byte array to hold the decompressed data
            bos = new ByteArrayOutputStream(compressedData.length);

            // Uncompress the data
            byte[] buf = new byte[1024];
            while (!decompressor.finished()) {
                    int count = decompressor.inflate(buf);
                    bos.write(buf, 0, count);
            }

            // Get the decompressed data
            out = new String(bos.toByteArray(), "UTF-8");

        } catch (UnsupportedEncodingException e) {
            LOGGER.error("UnsupportedEncodingException in compress method. null is returned: " + e);
            return null;
        } catch (DataFormatException e) {
            LOGGER.error("DataFormatException in compress method. null is returned: " + e);
            return null;
        } finally {
            if (decompressor != null) {
                decompressor.end();
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    LOGGER.error("IOException in compress method. null is returned: " + e);
                    return null;
                }
            }
        }
        return out;
  }

posted @ 2011-12-29 23:21  小折腾  阅读(206)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3