分享一个Java使用GZIP压缩数据和文件的工具类

import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author lang.zhou
 * @date 2019/8/30
 */
public class GZipUtil {
    public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
    public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";
    private static final String SYSTEM_ENCODING = getSystemEncoding();

    private static String getSystemEncoding(){
        String s =System.getProperty("sun.jnu.encoding");
        if(s == null || s.length()==0){
            s="GBK";
        }
        return s;
    }

    public static byte[] compress(String str, String encoding) throws IOException {
        if (str == null || str.length() == 0) {
            return null;
        }
        byte[] b = str.getBytes(encoding);
        return compress(b);
    }
    public static byte[] compress(byte[] b) throws IOException {
        ByteArrayOutputStream out = null;
        GZIPOutputStream gzip = null;
        try {
            out = new ByteArrayOutputStream();
            gzip = new GZIPOutputStream(out);
            gzip.write(b);
            gzip.finish();
        } finally {
            IOUtils.closeQuietly(gzip);
            IOUtils.closeQuietly(out);
        }
        return out.toByteArray();
    }

    /**
     * 得到字符串压缩后的字节数组
     */
    public static byte[] compress(String str) throws IOException {
        return compress(str, SYSTEM_ENCODING);
    }
    /**
     * 将压缩后的字节数组转换为压缩前的字节数组
     * @param bytes     压缩后的字节数组
     * @return          压缩前的字节数组
     */
    public static byte[] unCompress(byte[] bytes) throws IOException {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        GZIPInputStream zip = null;
        try {
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(bytes);
            zip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = zip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
        } finally {
            IOUtils.closeQuietly(zip);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        return out.toByteArray();
    }
    /**
     * 将压缩后的字节数组转换为压缩前的字符串
     * @param bytes     压缩后的字节数组
     * @return          压缩前的字符串
     */
    public static String unCompressToString(byte[] bytes, String encoding) throws IOException {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = null;
        ByteArrayInputStream in = null;
        GZIPInputStream zip = null;
        try {
            out = new ByteArrayOutputStream();
            in = new ByteArrayInputStream(bytes);
            zip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = zip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            return out.toString(encoding);
        } finally {
            IOUtils.closeQuietly(zip);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }

    private static String unCompressToString(byte[] bytes) throws IOException {
        return unCompressToString(bytes, SYSTEM_ENCODING);
    }

    public static void main(String[] args) throws IOException {
        String s = "解压得到数据解压得到数据解压得到数据解压得到数据解压得到数据解压得到数据解压得到数据解压得到数据解压得到数据";
        System.out.println("压缩前:"+s.getBytes().length+"字节");
        byte[] b = compress(s);
        System.out.println("压缩后:"+b.length+"字节");
        System.out.println("解压得到数据:"+unCompressToString(b));

        System.out.println("------------");
        String d = encodeString(s);
        System.out.println(d);
        System.out.println(decodeString(d));

        GZipUtil.encodeFile("C:\\Users\\Administrator\\Desktop\\富国——资管月报模板2019年6月填报 (1).xlsx"
                ,"C:\\Users\\Administrator\\Desktop\\20191214.xlsx");
        GZipUtil.decodeFile("C:\\Users\\Administrator\\Desktop\\20191214.xlsx",
                "C:\\Users\\Administrator\\Desktop\\20191214_decode.xlsx"
                );

    }
    /*****************************************************************************/
    /********************************字符串压缩***********************************/
    /*****************************************************************************/
    /**
     * 使用gzip进行压缩
     */
    public static String encodeString(String str) throws IOException{
        return encodeString(str, SYSTEM_ENCODING);
    }
    public static String encodeString(String str,String charset) throws IOException {
        if (str.length() == 0) {
            return str;
        }
        byte[] b = compress(str,charset);
        return Base64Util.encode(b);
    }
    /**
     * 使用gzip进行解压缩
     */
    public static String decodeString(String str) throws IOException{
        return decodeString(str,SYSTEM_ENCODING);
    }

    public static String decodeString(String compressedStr,String charset) throws IOException {
        if (compressedStr == null) {
            return null;
        }
        byte[] b = Base64Util.decodeToByte(compressedStr);
        return unCompressToString(b,charset);
    }
    /*****************************************************************************/
    /**********************************文件压缩***********************************/
    /*****************************************************************************/
    /**
     * gzip压缩文件
     */
    public static void encodeFile(String src,String des) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;
        try{
            in = new FileInputStream(src);
            out = new FileOutputStream(des);
            byte[] data = new byte[in.available()];
            in.read(data);
            byte[] res = compress(data);
            out.write(res);
            out.flush();
        }finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }

    }


    /**
     * gzip解压缩文件
     */
    public static void decodeFile(String src,String des) throws IOException{
        FileInputStream in = null;
        FileOutputStream out = null;
        try{
            in = new FileInputStream(src);
            out = new FileOutputStream(des);
            byte[] data = new byte[in.available()];
            in.read(data);
            byte[] res = unCompress(data);
            out.write(res);
            out.flush();
        }finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
    }
    
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值