Android GZip File Create gZip(File srcFile, File destFile)

Here you can find the source of gZip(File srcFile, File destFile)

Description

GZips a source File to a destination File.

License

Open Source License

Parameter

Parameter Description
srcFile the source file.
destFile the destination file.

Exception

Parameter Description
IOException error handling the files.

Declaration

public static void gZip(File srcFile, File destFile) throws IOException 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.zip.GZIPOutputStream;

public class Main {
    /**//from   w  ww  . ja v  a  2  s .c  o m
     * GZips a source File to a destination File.
     *
     * @param srcFile the source file.
     * @param destFile the destination file.
     * @throws IOException error handling the files.
     */
    public static void gZip(File srcFile, File destFile) throws IOException {

        FileOutputStream fos = null;
        GZIPOutputStream zos = null;
        InputStream fis = null;

        try {
            fos = new FileOutputStream(destFile);
            fis = new FileInputStream(srcFile);
            zos = new GZIPOutputStream(fos);

            copy(fis, zos);

        } finally {
            if (fis != null) {
                fis.close();
            }
            if (zos != null) {
                zos.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }

    /**
     * Copies from the {@code InputStream} into the {@code OutputStream}.
     *
     * @param inputStream the {@code GZIPInputStream}.
     * @param outputStream the {@code OutputStream}.
     * @throws IOException error handling files.
     */
    public static void copy(InputStream inputStream,
            OutputStream outputStream) throws IOException {

        byte[] buffer = new byte[8 * 1024];
        int count = 0;
        do {
            outputStream.write(buffer, 0, count);
            count = inputStream.read(buffer, 0, buffer.length);
        } while (count != -1);
    }
}