集册 Java实例教程 通过gzip算法压缩数据字节

通过gzip算法压缩数据字节

欢马劈雪     最近更新时间:2020-01-02 10:19:05

510
通过gzip算法压缩数据字节


//package com.nowjava;
// from nowjava - 时代Java

import java.io.ByteArrayOutputStream;

import java.io.IOException;


import java.util.zip.GZIPOutputStream;


public class Main {

    /**

     * Compress data bytes by gzip algorithm

     * 

     * @param data

     * @return compressed data

     * @throws IOException

     */

    public static byte[] gzip(byte[] data) throws IOException {

        if (data == null) {

            return data;/*来自 时代Java - nowjava.com*/

        }


        ByteArrayOutputStream out = new ByteArrayOutputStream();

        GZIPOutputStream gos = null;

        try {

            gos = new GZIPOutputStream(out);

            gos.write(data);

        } 
展开阅读全文