Java实现GZIP压缩与解压缩

Gzip压缩

public static byte[] compress(String str, String charset) {
    if (str == null || str.length() == 0) {
        return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPOutputStream gzip;
    try {
        gzip = new GZIPOutputStream(out);
        gzip.write(str.getBytes(charset));
        gzip.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}

Gzip解压缩

public static byte[] uncompress(byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
        return null;
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
    try {
        GZIPInputStream unGzip = new GZIPInputStream(in);
        byte[] buffer = new byte[256];
        int n;
        while ((n = unGzip.read(buffer)) >= 0) {
            out.write(buffer, 0, n);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}

工具代码集合

public class GzipUtils {

    public static byte[] compress(String str, String charset) {
        if (str == null || str.length() == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(str.getBytes(charset));
            gzip.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static byte[] compress(String str) throws IOException {
        return compress(str, StandardCharsets.UTF_8.name());
    }

    public static byte[] uncompress(byte[] bytes) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream unGzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = unGzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static byte[] uncompress(String str, String charset) throws IOException {
        return uncompress(str.getBytes(charset));
    }

    public static byte[] uncompress(String str) throws IOException {
        return uncompress(str.getBytes(StandardCharsets.UTF_8.name()));
    }

    public static String uncompressToString(byte[] bytes, String charset) {
        if (bytes == null || bytes.length == 0) {
            return null;
        }
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = new ByteArrayInputStream(bytes);
        try {
            GZIPInputStream unGzip = new GZIPInputStream(in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = unGzip.read(buffer)) >= 0) {
                out.write(buffer, 0, n);
            }
            return out.toString(charset);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String uncompressToString(byte[] bytes) {
        return uncompressToString(bytes, StandardCharsets.UTF_8.name());
    }

    public static String uncompressToString(String str, String inputCharset, String outputCharset) throws IOException {
        return new String(uncompress(str, inputCharset), outputCharset);
    }

    public static String uncompressToString(String str) throws IOException {
        return new String(uncompress(str, StandardCharsets.ISO_8859_1.name()), StandardCharsets.UTF_8.name());
    }
}
上一篇 Java处理UFT-8编码文件出现\ufeff的解决方法
下一篇 Java实现将数字金额转为大写中文金额
目录
文章列表
1 Swift - 控制流及控制结构说明(if,switch,for,while)
Swift - 控制流及控制结构说明(if,switch,for,while)
2
K8s集群节点间通信no route to host
K8s集群节点间通信no route to host
3
Android SpannableString属性详解
Android SpannableString属性详解
4
腾讯云CVM部署OpenClaw全流程教程
腾讯云CVM部署OpenClaw全流程教程
5
设计模式(19)组合模式
设计模式(19)组合模式
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。