Example usage for java.util.zip GZIPInputStream close

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

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

@Nullable
public static String decompress(String input) {
    StringBuilder sb = new StringBuilder();
    ByteArrayInputStream is = null;
    GZIPInputStream gis = null;
    try {/* w ww.j  a v a 2  s. c o m*/
        final byte[] bytes = Base64.decode(input, Base64.DEFAULT);
        is = new ByteArrayInputStream(bytes);
        gis = new GZIPInputStream(is, BUFFER_SIZE);

        int cache;
        final byte[] data = new byte[BUFFER_SIZE];
        while ((cache = gis.read(data)) != -1) {
            sb.append(new String(data, 0, cache));
        }
    } catch (IOException e) {
        return null;
    } finally {
        try {
            if (gis != null)
                gis.close();
            if (is != null)
                is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:com.norman0406.slimgress.API.Interface.Interface.java

private static String decompressGZIP(HttpEntity compressedEntity) throws IOException {
    final int bufferSize = 8192;
    InputStream input = compressedEntity.getContent();
    GZIPInputStream gzipStream = new GZIPInputStream(input, bufferSize);
    StringBuilder string = new StringBuilder();
    byte[] data = new byte[bufferSize];
    int bytesRead;
    while ((bytesRead = gzipStream.read(data)) != -1) {
        string.append(new String(data, 0, bytesRead));
    }/*from   w  w w  .  j  av a  2s .c o m*/
    gzipStream.close();
    input.close();
    return string.toString();
}

From source file:org.apache.ojb.broker.Identity.java

/**
 * Factory method that returns an Identity object created from a serializated representation.
 * /*ww  w.  j  a va 2 s .c  o  m*/
 * @param anArray The serialized representation
 * @return The identity
 * @see {@link #serialize}.
 * @deprecated
 */
public static Identity fromByteArray(final byte[] anArray) throws PersistenceBrokerException {
    // reverse of the serialize() algorithm:
    // read from byte[] with a ByteArrayInputStream, decompress with
    // a GZIPInputStream and then deserialize by reading from the ObjectInputStream
    try {
        final ByteArrayInputStream bais = new ByteArrayInputStream(anArray);
        final GZIPInputStream gis = new GZIPInputStream(bais);
        final ObjectInputStream ois = new ObjectInputStream(gis);
        final Identity result = (Identity) ois.readObject();
        ois.close();
        gis.close();
        bais.close();
        return result;
    } catch (Exception ex) {
        throw new PersistenceBrokerException(ex);
    }
}

From source file:Main.java

public static byte[] gzipDecodeByteArray(byte[] data) {
    GZIPInputStream gzipInputStream = null;
    try {/*  ww  w. j  av  a2  s  . c  o 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:org.apache.carbondata.hadoop.util.ObjectSerializationUtil.java

/**
 * Converts Base64 string to object.//from www  .  j  av a 2s.  c o  m
 *
 * @param objectString serialized object in string format
 * @return Object after convert string to object
 * @throws IOException
 */
public static Object convertStringToObject(String objectString) throws IOException {
    if (objectString == null) {
        return null;
    }

    byte[] bytes = Base64.decodeBase64(objectString.getBytes(CarbonCommonConstants.DEFAULT_CHARSET));

    ByteArrayInputStream bais = null;
    GZIPInputStream gis = null;
    ObjectInputStream ois = null;

    try {
        bais = new ByteArrayInputStream(bytes);
        gis = new GZIPInputStream(bais);
        ois = new ObjectInputStream(gis);
        return ois.readObject();
    } catch (ClassNotFoundException e) {
        throw new IOException("Could not read object", e);
    } finally {
        try {
            if (ois != null) {
                ois.close();
            }
            if (gis != null) {
                gis.close();
            }
            if (bais != null) {
                bais.close();
            }
        } catch (IOException e) {
            LOG.error(e);
        }
    }
}

From source file:uk.ac.ebi.embl.api.validation.helper.FileUtils.java

public static String getdeCompressedFile(String file) throws IOException {
    if (file == null) {
        return null;
    }//  ww w.  j  a  v  a  2 s  . c om
    File zipFile = new File(file);
    if (!zipFile.exists()) {
        return null;
    }

    if (!zipFile.getName().matches("^.+\\.gz$")) {
        return file;
    }
    byte[] buffer = new byte[1024];
    GZIPInputStream zis = new GZIPInputStream(new FileInputStream(zipFile));
    File tempFile = new File(zipFile.getParent() + File.separator
            + zipFile.getName().substring(0, zipFile.getName().lastIndexOf(".gz")));
    if (!tempFile.exists()) {
        tempFile.createNewFile();
    }

    FileOutputStream fos = new FileOutputStream(tempFile);
    int len;
    while ((len = zis.read(buffer)) > 0) {
        fos.write(buffer, 0, len);
    }

    fos.close();

    zis.close();

    return tempFile.getAbsolutePath();
}

From source file:com.taobao.tair.etc.TranscoderUtil.java

public static byte[] decompress(byte[] in) {
    ByteArrayOutputStream bos = null;

    if (in != null) {
        ByteArrayInputStream bis = new ByteArrayInputStream(in);

        bos = new ByteArrayOutputStream();

        GZIPInputStream gis = null;

        try {/*from  w w w .  ja  v a  2  s. c  o m*/
            gis = new GZIPInputStream(bis);

            byte[] buf = new byte[8192];
            int r = -1;

            while ((r = gis.read(buf)) > 0) {
                bos.write(buf, 0, r);
            }
        } catch (IOException e) {
            bos = null;
            throw new RuntimeException(e);
        } finally {
            try {
                gis.close();
                bos.close();
            } catch (Exception e) {
            }
        }
    }

    return (bos == null) ? null : bos.toByteArray();
}

From source file:org.datavec.api.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 * @param file the file to extract to//from   w w  w  .  j  a  v a  2  s  .  co  m
 * @param dest the destination directory
 * @throws java.io.IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip") || file.endsWith(".jar")) {
        //getFromOrigin the zip file content
        ZipInputStream zis = new ZipInputStream(fin);
        //getFromOrigin the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();

            File newFile = new File(dest + File.separator + fileName);

            if (ze.isDirectory()) {
                newFile.mkdirs();
                zis.closeEntry();
                ze = zis.getNextEntry();
                continue;
            }

            log.info("file unzip : " + newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(data)) > 0) {
                fos.write(data, 0, len);
            }

            fos.flush();
            fos.close();
            zis.closeEntry();
            ze = zis.getNextEntry();
        }

        zis.close();

    }

    else if (file.endsWith(".tar")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(in);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

            /** If the entry is a directory, createComplex the directory. **/

            if (entry.isDirectory()) {

                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();
                ;

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

            /** If the entry is a directory, createComplex the directory. **/

            if (entry.isDirectory()) {

                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".gz")) {
        GZIPInputStream is2 = new GZIPInputStream(fin);
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        OutputStream fos = FileUtils.openOutputStream(extracted);
        IOUtils.copyLarge(is2, fos);
        is2.close();
        fos.flush();
        fos.close();
    }

}

From source file:org.canova.api.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 * @param file the file to extract to//from   www  .j av a 2  s.c  o  m
 * @param dest the destination directory
 * @throws java.io.IOException
 */
public static void unzipFileTo(String file, String dest) throws IOException {
    File target = new File(file);
    if (!target.exists())
        throw new IllegalArgumentException("Archive doesnt exist");
    FileInputStream fin = new FileInputStream(target);
    int BUFFER = 2048;
    byte data[] = new byte[BUFFER];

    if (file.endsWith(".zip")) {
        //getFromOrigin the zip file content
        ZipInputStream zis = new ZipInputStream(fin);
        //getFromOrigin the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while (ze != null) {
            String fileName = ze.getName();

            File newFile = new File(dest + File.separator + fileName);

            if (ze.isDirectory()) {
                newFile.mkdirs();
                zis.closeEntry();
                ze = zis.getNextEntry();
                continue;
            }

            log.info("file unzip : " + newFile.getAbsoluteFile());

            //create all non exists folders
            //else you will hit FileNotFoundException for compressed folder

            FileOutputStream fos = new FileOutputStream(newFile);

            int len;
            while ((len = zis.read(data)) > 0) {
                fos.write(data, 0, len);
            }

            fos.flush();
            fos.close();
            zis.closeEntry();
            ze = zis.getNextEntry();
        }

        zis.close();

    }

    else if (file.endsWith(".tar")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(in);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

            /** If the entry is a directory, createComplex the directory. **/

            if (entry.isDirectory()) {

                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();
                ;

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".tar.gz") || file.endsWith(".tgz")) {

        BufferedInputStream in = new BufferedInputStream(fin);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);
        TarArchiveInputStream tarIn = new TarArchiveInputStream(gzIn);

        TarArchiveEntry entry = null;

        /** Read the tar entries using the getNextEntry method **/

        while ((entry = (TarArchiveEntry) tarIn.getNextEntry()) != null) {

            log.info("Extracting: " + entry.getName());

            /** If the entry is a directory, createComplex the directory. **/

            if (entry.isDirectory()) {

                File f = new File(dest + File.separator + entry.getName());
                f.mkdirs();
            }
            /**
             * If the entry is a file,write the decompressed file to the disk
             * and close destination stream.
             **/
            else {
                int count;

                FileOutputStream fos = new FileOutputStream(dest + File.separator + entry.getName());
                BufferedOutputStream destStream = new BufferedOutputStream(fos, BUFFER);
                while ((count = tarIn.read(data, 0, BUFFER)) != -1) {
                    destStream.write(data, 0, count);
                }

                destStream.flush();

                IOUtils.closeQuietly(destStream);
            }
        }

        /** Close the input stream **/

        tarIn.close();
    }

    else if (file.endsWith(".gz")) {
        GZIPInputStream is2 = new GZIPInputStream(fin);
        File extracted = new File(target.getParent(), target.getName().replace(".gz", ""));
        if (extracted.exists())
            extracted.delete();
        extracted.createNewFile();
        OutputStream fos = FileUtils.openOutputStream(extracted);
        IOUtils.copyLarge(is2, fos);
        is2.close();
        fos.flush();
        fos.close();
    }

}

From source file:it.marcoberri.mbfasturl.action.Commons.java

/**
 * //from  w  w w  .  j  a v a 2 s .co m
 * @param log
 * @param path
 * @param url
 */
public static void downloadGeoIp2(Logger log, String path, String url) {

    long ts = System.currentTimeMillis();
    final String action = "downloadGeoIp2";

    try {
        org.apache.commons.io.FileUtils.forceMkdir(new File(path));
    } catch (final IOException ex) {
    }

    final String fileName = url.substring(url.lastIndexOf("/"), url.length());

    HttpUtil.downloadData(url, path + "/" + fileName);

    try {
        final File f = new File(path + "/" + fileName);

        final GZIPInputStream gzis = new GZIPInputStream(new FileInputStream(f));

        final byte[] buffer = new byte[1024];
        final FileOutputStream out = new FileOutputStream(
                new File(path + "/" + fileName.replaceAll(".gz", "")));
        int len;
        while ((len = gzis.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        gzis.close();
        out.close();

        writeEventLog(action, true, "File size:" + f.length(), "Download Resources",
                (System.currentTimeMillis() - ts));

    } catch (final IOException ex) {

        log.fatal(ex);
        writeEventLog(action, false, ex.getMessage(), "Download Resources", (System.currentTimeMillis() - ts));
    }

}