Example usage for java.util.zip GZIPInputStream read

List of usage examples for java.util.zip GZIPInputStream read

Introduction

In this page you can find the example usage for java.util.zip GZIPInputStream read.

Prototype

public int read(byte[] buf, int off, int len) throws IOException 

Source Link

Document

Reads uncompressed data into an array of bytes.

Usage

From source file:Main.java

public static byte[] decompress(byte[] bytes) throws IOException {

    GZIPInputStream gzip = null;
    ByteArrayOutputStream baos = null;
    try {/*from www  . j av  a2  s .c  o  m*/
        baos = new ByteArrayOutputStream();

        gzip = new GZIPInputStream(new ByteArrayInputStream(bytes));

        int len = 0;
        byte data[] = new byte[BUFFER_SIZE];

        while ((len = gzip.read(data, 0, BUFFER_SIZE)) != -1) {
            baos.write(data, 0, len);
        }

        gzip.close();
        baos.flush();

        return baos.toByteArray();

    } finally {
        if (gzip != null) {
            gzip.close();
        }
        if (baos != null) {
            baos.close();
        }
    }
}

From source file:og.android.tether.system.WebserviceTask.java

public static boolean downloadBluetoothModule(String downloadFileUrl, String destinationFilename) {
    if (android.os.Environment.getExternalStorageState()
            .equals(android.os.Environment.MEDIA_MOUNTED) == false) {
        return false;
    }/*ww w . j  ava  2s .  com*/
    File bluetoothDir = new File(BLUETOOTH_FILEPATH);
    if (bluetoothDir.exists() == false) {
        bluetoothDir.mkdirs();
    }
    if (downloadFile(downloadFileUrl, "", destinationFilename) == true) {
        try {
            FileOutputStream out = new FileOutputStream(new File(destinationFilename.replace(".gz", "")));
            FileInputStream fis = new FileInputStream(destinationFilename);
            GZIPInputStream gzin = new GZIPInputStream(new BufferedInputStream(fis));
            int count;
            byte buf[] = new byte[8192];
            while ((count = gzin.read(buf, 0, 8192)) != -1) {
                //System.out.write(x);
                out.write(buf, 0, count);
            }
            out.flush();
            out.close();
            gzin.close();
            File inputFile = new File(destinationFilename);
            inputFile.delete();
        } catch (IOException e) {
            return false;
        }
        return true;
    } else
        return false;
}

From source file:Main.java

public static byte[] gzipDecodeByteArray(byte[] data) {
    GZIPInputStream gzipInputStream = null;
    try {// ww w.  j  av  a 2  s  . co  m
        gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(data));

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
        byte[] buffer = new byte[1024];
        while (gzipInputStream.available() > 0) {
            int count = gzipInputStream.read(buffer, 0, 1024);
            if (count > 0) {
                //System.out.println("Read " + count + " bytes");
                outputStream.write(buffer, 0, count);
            }
        }

        outputStream.close();
        gzipInputStream.close();
        return outputStream.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static void gunzip(File inFile, File outFile) {
    System.out.println("Expanding " + inFile.getAbsolutePath() + " to " + outFile.getAbsolutePath());

    FileOutputStream out = null;//from w  w  w  .  j  a v  a2s.c om
    GZIPInputStream zIn = null;
    FileInputStream fis = null;
    try {
        out = new FileOutputStream(outFile);
        fis = new FileInputStream(inFile);
        zIn = new GZIPInputStream(fis);
        byte[] buffer = new byte[8 * 1024];
        int count = 0;
        do {
            out.write(buffer, 0, count);
            count = zIn.read(buffer, 0, buffer.length);
        } while (count != -1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:net.sf.ehcache.distribution.PayloadUtil.java

/**
 * The fastest Ungzip implementation. See PageInfoTest in ehcache-constructs.
 * A high performance implementation, although not as fast as gunzip3.
 * gunzips 100000 of ungzipped content in 9ms on the reference machine.
 * It does not use a fixed size buffer and is therefore suitable for arbitrary
 * length arrays./*w w w .j a v a2 s.  c  o  m*/
 *
 * @param gzipped
 * @return a plain, uncompressed byte[]
 */
public static byte[] ungzip(final byte[] gzipped) {
    byte[] ungzipped = new byte[0];
    try {
        final GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped));
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length);
        final byte[] buffer = new byte[PayloadUtil.MTU];
        int bytesRead = 0;
        while (bytesRead != -1) {
            bytesRead = inputStream.read(buffer, 0, PayloadUtil.MTU);
            if (bytesRead != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }
        }
        ungzipped = byteArrayOutputStream.toByteArray();
        inputStream.close();
        byteArrayOutputStream.close();
    } catch (IOException e) {
        LOG.fatal("Could not ungzip. Heartbeat will not be working. " + e.getMessage());
    }
    return ungzipped;
}

From source file:net.grinder.util.LogCompressUtil.java

/**
 * Uncompress the given array into the given file.
 * /*from   ww  w .  j av  a2 s .  c  o m*/
 * @param zipEntry
 *            byte array of compressed file
 * @param toFile
 *            file to be written
 */
public static void unCompressGzip(byte[] zipEntry, File toFile) {
    FileOutputStream fos = null;
    GZIPInputStream zipInputStream = null;
    ByteArrayInputStream bio = null;
    try {
        bio = new ByteArrayInputStream(zipEntry);
        zipInputStream = new GZIPInputStream(bio);
        fos = new FileOutputStream(toFile);
        byte[] buffer = new byte[COMPRESS_BUFFER_SIZE];
        int count = 0;
        while ((count = zipInputStream.read(buffer, 0, COMPRESS_BUFFER_SIZE)) != -1) {
            fos.write(buffer, 0, count);
        }
        fos.flush();
    } catch (IOException e) {
        LOGGER.error("Error occurs while uncompress {}", toFile.getAbsolutePath());
        LOGGER.error("Details", e);
        return;
    } finally {
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(bio);
        IOUtils.closeQuietly(zipInputStream);
    }
}

From source file:org.cloudata.core.common.io.CWritableUtils.java

public static byte[] readCompressedByteArray(DataInput in) throws IOException {
    int length = in.readInt();
    if (length == -1)
        return null;
    byte[] buffer = new byte[length];
    in.readFully(buffer); // could/should use readFully(buffer,0,length)?
    GZIPInputStream gzi = new GZIPInputStream(new ByteArrayInputStream(buffer, 0, buffer.length));
    byte[] outbuf = new byte[length];
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    int len;/*from   w  w w  . j  a  v a 2  s . com*/
    while ((len = gzi.read(outbuf, 0, outbuf.length)) != -1) {
        bos.write(outbuf, 0, len);
    }
    byte[] decompressed = bos.toByteArray();
    bos.close();
    gzi.close();
    return decompressed;
}

From source file:org.openbel.framework.core.df.cache.CacheUtil.java

/**
 * Copies the <tt>resource</tt> to <tt>copy</tt>.  Decompression is
 * performed if the resource file is identified as a GZIP-encoded file.
 *
 * @param resource {@link File}, the resource to copy
 * @param resourceLocation {@link String}, the resource location url
 * @param copy {@link File}, the file to copy to
 * @return {@link File}, the copied file
 * @throws ResourceDownloadError Thrown if an IO error copying the resource
 *//*from ww  w .j a va2 s  .  c o  m*/
private static File copyWithDecompression(final File resource, final String resourceLocation, final File copy)
        throws ResourceDownloadError {
    GZIPInputStream gzipis = null;
    FileOutputStream fout = null;
    try {
        MagicNumberFileFilter mnff = new MagicNumberFileFilter(GZIP_MAGIC_NUMBER);

        if (mnff.accept(resource)) {
            gzipis = new GZIPInputStream(new FileInputStream(resource));
            byte[] buffer = new byte[8192];

            fout = new FileOutputStream(copy);

            int length;
            while ((length = gzipis.read(buffer, 0, 8192)) != -1) {
                fout.write(buffer, 0, length);
            }
        } else {
            copyFile(resource, copy);
        }
    } catch (IOException e) {
        String msg = e.getMessage();
        ResourceDownloadError r = new ResourceDownloadError(resourceLocation, msg);
        r.initCause(e);
        throw r;
    } finally {
        // clean up all I/O resources
        closeQuietly(fout);
        closeQuietly(gzipis);
    }

    return copy;
}

From source file:org.mitre.opensextant.util.FileUtility.java

/**
 *
 * @param fname//  ww  w .  jav  a 2s  .c  om
 * @return
 * @throws IOException
 */
public static String readGzipFile(String fname) throws IOException {
    if (fname == null) {
        return null;
    }

    FileInputStream instream = new FileInputStream(fname);
    GZIPInputStream gzin = new GZIPInputStream(new BufferedInputStream(instream), default_buffer);

    byte[] inputBytes = new byte[default_buffer];
    StringBuilder buf = new StringBuilder();

    int readcount = 0;
    while ((readcount = gzin.read(inputBytes, 0, default_buffer)) != -1) {
        buf.append(new String(inputBytes, 0, readcount, default_encoding));
    }
    instream.close();
    gzin.close();

    return buf.toString();

}

From source file:it.geosolutions.tools.compress.file.Extractor.java

/**
 * @author Carlo Cancellieri - carlo.cancellieri@geo-solutions.it
 * /*from   ww  w  . jav  a  2  s.  c o m*/
 *         Extract a GZip file to a tar
 * @param in_file
 *            the input bz2 file to extract
 * @param out_file
 *            the output tar file to extract to
 */
public static void extractGzip(File in_file, File out_file) throws CompressorException {
    FileOutputStream out = null;
    GZIPInputStream zIn = null;
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    try {
        out = new FileOutputStream(out_file);
        fis = new FileInputStream(in_file);
        bis = new BufferedInputStream(fis, Conf.getBufferSize());
        zIn = new GZIPInputStream(bis);
        byte[] buffer = new byte[Conf.getBufferSize()];
        int count = 0;
        while ((count = zIn.read(buffer, 0, Conf.getBufferSize())) != -1) {
            out.write(buffer, 0, count);
        }
    } catch (IOException ioe) {
        String msg = "Problem uncompressing Gzip " + ioe.getMessage() + " ";
        throw new CompressorException(msg + in_file.getAbsolutePath());
    } finally {
        try {
            if (bis != null)
                bis.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
        try {
            if (fis != null)
                fis.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
        try {
            if (out != null)
                out.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
        try {
            if (zIn != null)
                zIn.close();
        } catch (IOException ioe) {
            throw new CompressorException("Error closing stream: " + in_file.getAbsolutePath());
        }
    }
}