java压缩文本内容

2023-11-08

java压缩文本内容工具类如下:

package com.my.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

    /**
     * 使用gzip进行压缩
     */
    public static String gzip(String primStr) {
        if (primStr == null || primStr.length() == 0) {
            return primStr;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(primStr.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return new sun.misc.BASE64Encoder().encode(out.toByteArray());
    }

    /**
     * <p>Description:使用gzip进行解压缩</p>
     *
     * @param compressedStr
     * @return
     */
    public static String gunzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        byte[] compressed = null;
        String decompressed = null;
        try {
            compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            in = new ByteArrayInputStream(compressed);
            ginzip = new GZIPInputStream(in);

            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }

        return decompressed;
    }

    /**
     * 使用zip进行压缩
     *
     * @param str 压缩前的文本
     * @return 返回压缩后的文本
     */
    public static final String zip(String str) {
        if (str == null)
            return null;
        byte[] compressed;
        ByteArrayOutputStream out = null;
        ZipOutputStream zout = null;
        String compressedStr = null;
        try {
            out = new ByteArrayOutputStream();
            zout = new ZipOutputStream(out);
            zout.putNextEntry(new ZipEntry("0"));
            zout.write(str.getBytes());
            zout.closeEntry();
            compressed = out.toByteArray();
            compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
        } catch (IOException e) {
            compressed = null;
        } finally {
            if (zout != null) {
                try {
                    zout.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return compressedStr;
    }

    /**
     * 使用zip进行解压缩
     *
     * @param compressed 压缩后的文本
     * @return 解压后的字符串
     */
    public static final String unzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        ZipInputStream zin = null;
        String decompressed = null;
        try {
            byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(compressed);
            zin = new ZipInputStream(in);
            zin.getNextEntry();
            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = zin.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            decompressed = null;
        } finally {
            if (zin != null) {
                try {
                    zin.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return decompressed;
    }
}

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java压缩文本内容 的相关文章

随机推荐

  • 慕课网实战项目《WebAPP书城整站开发》笔记六:字体面板中“背景”切换的交互开发

    推荐IMOOC实战课程 WebApp书城整站开发 效果图 任务分解描述 本效果需要实现的是图中点击字体按钮后弹出一个可以设置字体颜色和大小的面板 点击面板上的 大 和 小 可以设置字体的大小 已完成 点击面板上的颜色圆圈可以切换阅读器的背景
  • Altium Designer常见问题整理(持续更新)

    Altium Designer DRC检查报告无法定位PCB具体错误位置 在AD中使用设计规则检查DRC 出具的PCB报告点击错误信息无法定位到具体错误位置 使用AD内置打开和浏览器打开均无效 原因 原理图与PCB工程文件存放路径存在中文导
  • python3.8安装tensorflow_python安装TensorFlow吐血整理

    1 安装特定版本的TensorFlow 1 pip install tensorflow gpu 1 9 0 2 pip install tensorflow gpu 必须用命令 1 才能安装想要的新版本 命令 2 只会安装TensorFl
  • element upload上传单张图片,效果同多图

    效果如图
  • MySQL多表查询

    目录 1 创建class数据库 2 创建student和score表 3 向student表插入记录的INSERT语句 4 向score表插入记录的INSERT语句 5 查询student表的所有记录 6 查询student表的第2条到4条
  • 使用命令行打开vscode

    vscode 最新版的vscode不需要配置 打开命令行工具 cmdr git 进入项目目录 输入下面命令 code 创建test js文件 code test js 旧版本需要配置一下 手动打开vscode ctrl shift p 打开
  • 总结了大佬的学习方法 #CSDN博文精选# #学习方法# #高效系统化学习#

    大家好 我是小C 全名是CSDN高校俱乐部 我的职责之一是担任 文章过滤器 精选大咖干货 助力学习之路 你是否曾面对海量信息而无从下手 你是否曾苦恼学习效果不佳 事倍功半 你是否曾感叹知识零碎而无法发挥用途 针对这些问题 小C将开启一个全新
  • vue使用mock模拟后台接口返回数据

    一 项目引入mock依赖 npm install mockjs save 或 cnpm install mockjs save 二 准备数据文件和模拟接口的文件 1 src 文件夹下新建 mock 文件夹 2 在 mock 文件夹下新建 m
  • spring源码阅读一:spring初始化容器时扫描package的过程

    注 spring version 4 2 0 RELEASE 首先 spring 管理注解bean容器主要是 AnnotationConfigApplicationContext org springframework context an
  • 解析Qt的ui_*.h文件

    mainwindow h常见的几行 namespace Ui class MainWindow private Ui MainWindow ui mainwindow cpp常见的几行 include ui mainwindow h Mai
  • MQTT协议实现Eclipse Paho学习总结一

    http blog csdn net yangzl2008 article details 8861069 一 概述 遥测传输 MQTT 是轻量级基于代理的发布 订阅的消息传输协议 设计思想是开放 简单 轻量 易于实现 这些特点使它适用于受
  • C++继承,语法+案例,超详细!!

    类的继承 继承是代码重用的一种方法 通过继承创建的新类称为 派生类 或 子类 被继承的类称为 基类 或 父类 继承即子类无需重新编写父类成员代码的情况下继承父类所有的成员 子类只需书写新增成员的代码 语法 class 子类名 继承方式 父类
  • echo打印颜色

    Linux echo命令打印带有颜色的字 一 命令格式如下 echo e 033 字背景颜色 文字颜色m字符串 033 0m 例如 echo e 033 47 30m I love Android 033 0m 其中47的位置代表背景色 3
  • 仓库管理系统GreaterWMS的安装

    本文是应网友 ubuntu 和 Nathan 要求写的 因为看起来 Nathan 比较着急 就突击了一下 因为时间仓促 错误在所难免 敬请谅解 什么是 GreaterWMS GreaterWMS是完全开源的仓库管理系统 该库存管理系统是目前
  • 云平台学习笔记(三)-MobaXterm使用

    内网 外网的IP都可以 这个界面方面可视化管理
  • MATLAB数据关联性(相关性)分析

    分析某个因素与其它因素之间的关联强弱 举个例子X0 X1 X2 X3 X4 X5分别代表热效率 煤气流量 空气流量 热值 蒸汽流量 给水流量 这里分析X1 X2 X3 X4 X5与X0的关联度 定义如下 图片摘自知乎 X0 xlsread
  • qgis导入在线地图网址

    https webst01 is autonavi com appmaptile style 3D6 26x 3D 7Bx 7D 26y 3D 7By 7D 26z 3D 7Bz 7D zmax 18 zmin 0 type xyz url
  • TortoiseGit 入门指南14:比较差异

    版本控制系统最常见的应用是查看发生了哪些变化 Git 通过比较两个 文件 或者两个 提交 的 差异 differences 来实现这个功能 对于文本文件 TortoiseGit 提供了名为 TortoiseGitMerge 的内置工具来查看
  • 图像梯度(微分)及其MATLAB求解

    1 一阶梯度 MATLAB中的gradient 函数 实现的是中心梯度 2 二阶梯度 h fspecial laplacian 获得的模板是 h 0 1667 0 6667 0 1667 0 6667 3 3333 0 6667 0 166
  • java压缩文本内容

    java压缩文本内容工具类如下 package com my util import java io ByteArrayInputStream import java io ByteArrayOutputStream import java