集册 Java实例教程 使用GZIP压缩输入字节数组。

使用GZIP压缩输入字节数组。

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

844
使用GZIP压缩输入字节数组。
/*
n o w  j a v a  . c o m
*/


//package com.nowjava;


import java.io.ByteArrayOutputStream;

import java.io.IOException;


import java.util.zip.GZIPOutputStream;

import sun.misc.BASE64Decoder;


public class Main {

    /**

     * Compresses the input byte array using GZIP.

     *

     * @param binaryInput Array of bytes that should be compressed.

     * @return Compressed bytes

     * @throws IOException

     */

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

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        GZIPOutputStream gzipOut = new GZIPOutputStream(baos);
        /*
        N o w J a v a . c o m - 时  代  Java 提供
        */


        gzipOut.write(binaryInput);

        gzipOut.finish();

        gzipOut.close();


        return baos.toByteArray();

    }


    /**

     * Compresses the input BASE64 encoded byte array using GZIP.

     *

     * @param base64Input BASE64 encoded bytes that should be compressed

     * @return Compressed bytes

     * @exception IOException

     */

    public static byte[] gzip(String base64Input) 
展开阅读全文