uncompress data stream with gzip - Java File Path IO

Java examples for File Path IO:GZIP

Description

uncompress 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.util.zip.GZIPInputStream;

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

    /**// w  ww .j  a  v a  2 s.  com
     * uncompress 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 ungzip(InputStream is, OutputStream os)
            throws IOException {
        GZIPInputStream gis = new GZIPInputStream(is);

        byte[] data = new byte[BYTE_BUFFER];
        int length = -1;
        while ((length = gis.read(data)) != -1) {
            os.write(data, 0, length);
        }

        gis.close();
    }

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

        ungzip(bis, bos);

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

        return result;
    }

    public static boolean ungzip(File gzipFile) {
        return ungzip(gzipFile, true);
    }

    public static boolean ungzip(File gzipFile, boolean deleteSourceFile) {
        String fileName = gzipFile.getName();
        if (fileName.endsWith(GZIP_FILE_SUFFIX)) {
            fileName = fileName.substring(0,
                    fileName.lastIndexOf(GZIP_FILE_SUFFIX));
        } else {
            fileName = "ungz-" + fileName;
        }
        File targetFile = new File(gzipFile.getParent(), fileName);
        return ungzip(gzipFile, targetFile, deleteSourceFile);
    }

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

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

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

        if (deleteSourceFile)
            return gzipFile.delete();

        return true;
    }
}

Related Tutorials