分享一个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);
        }
    }
    
}

 

你可能感兴趣的:(java,Java,GZIP)