集册 Java实例教程 GZIP压缩数据。

GZIP压缩数据。

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

567
GZIP压缩数据。

/*****************************************************************************

 * Copyright (c) 2007 Jet Propulsion Laboratory,

 * California Institute of Technology.  All rights reserved

 *****************************************************************************/

//package com.nowjava;


import java.io.ByteArrayOutputStream;
/**
 * 时代Java - nowjava.com 提 供 
**/

import java.io.File;

import java.io.FileInputStream;


import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.IOException;

import java.io.OutputStream;


import java.util.zip.GZIPOutputStream;
//nowjava.com - 时  代  Java

public class Main {

    private static final int _BUFFER_SIZE = 1024;


    /**

     * Compresses data.

     * 

     * @param data Data to be compressed.

     * @return Compressed data.

     * @throws IOException If there is an error.

     */

    public static byte[] compress(byte[] dataSource) throws IOException {

        ByteArrayOutputStream baos = null;

        GZIPOutputStream gos = null;


        int bytesToWrite = 0;

        int currentPosition = 0;

        try {

            // this initial size does not have to be accurate, however, providing

            // a value close to the final size would make it faster since less

            // byte allocation happens.

            baos = new ByteArrayOutputStream(dataSource.length);

            gos = new GZIPOutputStream(baos);


            while (currentPosition < dataSource.length) {

                bytesToWrite = _BUFFER_SIZE;

                if (bytesToWrite > (dataSource.length - currentPosition)) {

                    bytesToWrite = (dataSource.length - currentPosition);

                }


                gos.write(dataSource, currentPosition, bytesToWrite);

                currentPosition += bytesToWrite;

            }

        } catch (Exception exception) {

            throw new IOException("Failed to compress data.");

        } finally {

            try {

                if (gos != null) {

                    gos.finish();

                    gos.close();

                }

                if (baos != null) {

                    baos.close();

                }

            } catch (Exception exception) {

            }

        }

        return baos.toByteArray();

    }


    public static void compress(InputStream inputStream,

            OutputStream outputStream) throws IOException {

        byte[] buffer = new byte[_BUFFER_SIZE];

        GZIPOutputStream gos = null;


        try {

            gos = new GZIPOutputStream(outputStream);


            int bytesRead = 0;

            while ((bytesRead = inputStream.read(buffer, 0, buffer.length)) != -1) {

                gos.write(buffer, 0, bytesRead);

            }

        } catch (IOException exception) {

            throw exception;

        } finally {

            if (gos != null) {

                gos.finish();

                gos.close();

            }

        }

    }


    public static void compress(File in
展开阅读全文