GZIP Compresses data. - Java File Path IO

Java examples for File Path IO:GZIP

Description

GZIP Compresses data.

Demo Code

/*****************************************************************************
 * Copyright (c) 2007 Jet Propulsion Laboratory,
 * California Institute of Technology.  All rights reserved
 *****************************************************************************/
//package com.java2s;

import java.io.ByteArrayOutputStream;
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;

public class Main {
    private static final int _BUFFER_SIZE = 1024;

    /**//  ww  w  . j  av a  2  s.  c o m
     * 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 inputFile, File outputFile)
            throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(inputFile);
            fos = new FileOutputStream(outputFile, false);
            compress(fis, fos);
        } catch (IOException exception) {
            throw exception;
        } finally {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }
}

Related Tutorials