compress data stream with gzip - Java File Path IO

Java examples for File Path IO:GZIP

Description

compress data stream with gzip

Demo Code


//package com.java2s;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;

import java.util.zip.GZIPOutputStream;

public class Main {
    private static final int BYTE_BUFFER = 1024;
    private static final String GZIP_FILE_SUFFIX = ".gz";

    /**/*from w w w  . j ava 2s . c o m*/
     * compress data stream
     * @author Crow
     * @date 2015?6?19?
     * @version v0.1
     * @param is   input stream of bytes
     * @param os   output stream of bytes
     * @throws IOException 
     */
    public static void gzip(InputStream is, OutputStream os)
            throws IOException {
        GZIPOutputStream gos = new GZIPOutputStream(os);

        byte[] data = new byte[BYTE_BUFFER];
        int length = -1;
        while ((length = is.read(data)) != -1) {
            gos.write(data, 0, length);
        }
        gos.finish();
        gos.flush();
        gos.close();
    }

    /**
     * compress bytes
     * @author Crow
     * @date 2015?6?19?
     * @version v0.1
     * @param content   the bytes to be compressed
     * @return
     * @throws IOException 
     */
    public static byte[] gzip(byte[] content) throws IOException {
        ByteArrayInputStream bis = new ByteArrayInputStream(content);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        gzip(bis, bos);

        bos.flush();
        byte[] result = bos.toByteArray();
        bos.close();
        bis.close();

        return result;
    }

    /**
     * compress string, default UTF-8 encoding
     * @author Crow
     * @date 2015?6?19?
     * @version v0.1
     * @param content   the string to be compressed
     * @return
     * @throws IOException
     */
    public static byte[] gzip(String content) throws IOException {
        return gzip(content, Charset.forName("UTF-8"));
    }

    /**
     * compress string, need to provide encoding
     * @author Crow
     * @date 2015?6?19?
     * @version v0.1
     * @param content   the string to be compressed
     * @param cs      encoding
     * @return
     * @throws IOException 
     */
    public static byte[] gzip(String content, Charset cs)
            throws IOException {
        ByteArrayInputStream bis = new ByteArrayInputStream(
                content.getBytes(cs));
        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        gzip(bis, bos);

        bos.flush();
        byte[] result = bos.toByteArray();
        bos.close();
        bis.close();

        return result;
    }

    /**
     * compress file, after generates a .gz file in the same directory, the original file will be deleted 
     * @author Crow
     * @date 2015?6?19?
     * @version v0.1
     * @param sourceFile   the file to be compressed
     * @return
     */
    public static boolean gzip(File sourceFile) {
        return gzip(sourceFile, true);
    }

    /**
     * compress file, generates a .gz file in the same directory
     * @author Crow
     * @date 2015?6?19?
     * @version v0.1
     * @param sourceFile      the file to be compressed
     * @param deleteSourceFile   if true, the original file will be deleted 
     * @return
     */
    public static boolean gzip(File sourceFile, boolean deleteSourceFile) {
        File gzipFile = new File(sourceFile.getParent(),
                sourceFile.getName() + GZIP_FILE_SUFFIX);
        return gzip(sourceFile, gzipFile, deleteSourceFile);
    }

    public static boolean gzip(File sourceFile, File gzipFile,
            boolean deleteSourceFile) {
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(sourceFile));
        } catch (FileNotFoundException e) {
            System.out.println("?????????????????????????");
            e.printStackTrace();
            return false;
        }

        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(gzipFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            try {
                if (bis != null)
                    bis.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            return false;
        }

        GZIPOutputStream gos = null;
        try {
            gos = new GZIPOutputStream(bos);
            byte[] data = new byte[BYTE_BUFFER];
            int length = -1;
            while ((length = bis.read(data)) != -1) {
                gos.write(data, 0, length);
            }
            gos.flush();
            gos.finish();
        } catch (IOException e) {
            deleteSourceFile = false;// IOException, undelete file.
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (gos != null)
                    gos.close();
                if (bos != null)
                    bos.close();
                if (bis != null)
                    bis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (deleteSourceFile)
            return sourceFile.delete();

        return true;
    }
}

Related Tutorials