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:net.zyuiop.remoteworldloader.utils.CompressionUtils.java

public static void uncompressFile(File archive, File target) throws IOException {
    byte[] buffer = new byte[1024];
    if (target.exists())
        target.delete();/*from   w  w  w . java  2  s .c  o  m*/
    target.createNewFile();

    try {
        GZIPInputStream stream = new GZIPInputStream(new FileInputStream(archive));
        FileOutputStream out = new FileOutputStream(target);

        int len;
        while ((len = stream.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }

        stream.close();
        out.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    Bukkit.getLogger().info("Extracted file " + target.getName() + ".");
}

From source file:edu.harvard.i2b2.fhir.Utils.java

public static String unCompressString(final byte[] data, final String encoding) throws IOException {
    if (data == null || data.length == 0) {
        return null;
    } else {//from ww w.  j  a va 2  s  . com
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        GZIPInputStream is = new GZIPInputStream(bais);
        byte[] tmp = new byte[256];
        while (true) {
            int r = is.read(tmp);
            if (r < 0) {
                break;
            }
            buffer.write(tmp, 0, r);
        }
        is.close();

        byte[] content = buffer.toByteArray();
        return new String(content, 0, content.length, encoding);
    }
}

From source file:com.hortonworks.amstore.view.AmbariStoreHelper.java

/**
 * Ungzip an input file into an output file.
 * <p>/*  w  w w.  j a v a2s.co m*/
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.gz' extension. 
 * 
 * @param inputFile     the input .gz file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@File} with the ungzipped content.
 */
public static File unGzip(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException {

    LOG.info(String.format("Ungzipping %s to dir %s.", inputFile.getAbsolutePath(),
            outputDir.getAbsolutePath()));

    final File outputFile = new File(outputDir,
            inputFile.getName().substring(0, inputFile.getName().length() - 3));

    final GZIPInputStream in = new GZIPInputStream(new FileInputStream(inputFile));
    final FileOutputStream out = new FileOutputStream(outputFile);

    IOUtils.copy(in, out);

    in.close();
    out.close();

    return outputFile;
}

From source file:org.apache.ode.daohib.bpel.hobj.GZipDataType.java

/**
 * Decompress (using gzip algorithm) a byte array.
 *//*from   w w w. j  ava  2 s.  c o m*/
public static byte[] gunzip(InputStream input) {
    try {
        GZIPInputStream unzip = new GZIPInputStream(input);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(32 * 1024);
        byte[] buf = new byte[4096];
        int len;
        while ((len = unzip.read(buf)) > 0) {
            baos.write(buf, 0, len);
        }
        unzip.close();
        return baos.toByteArray();
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:de.zib.scalaris.examples.wikipedia.data.Revision.java

/**
 * De-compresses the given text and returns it as a string.
 * //from w  w  w .j  a  va  2  s .  co  m
 * @param text
 *            the compressed text
 * 
 * @return the de-compressed text
 * 
 * @throws RuntimeException
 *             if de-compressing the text did not work
 */
protected static String unpackText(byte[] text) throws RuntimeException {
    try {
        ByteArrayOutputStream unpacked = new ByteArrayOutputStream();
        ByteArrayInputStream bis = new ByteArrayInputStream(text);
        GZIPInputStream gis = new GZIPInputStream(bis);
        byte[] bbuf = new byte[256];
        int read = 0;
        while ((read = gis.read(bbuf)) >= 0) {
            unpacked.write(bbuf, 0, read);
        }
        gis.close();
        return new String(unpacked.toString("UTF-8"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.jivesoftware.os.jive.utils.shell.utils.Unzip.java

public static File unGzip(boolean verbose, File outputDir, String outName, File inputFile,
        boolean deleteOriginal) throws FileNotFoundException, IOException {
    String inFilePath = inputFile.getAbsolutePath();
    if (verbose) {
        System.out.println("unzipping " + inFilePath);
    }/*from  w  ww .j  av  a  2s.  c o  m*/
    GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(inFilePath));

    File outFile = new File(outputDir, outName);
    outFile.getParentFile().mkdirs();
    String outFilePath = outFile.getAbsolutePath();
    OutputStream out = new FileOutputStream(outFilePath);

    byte[] buf = new byte[1024];
    int len;
    while ((len = gzipInputStream.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    gzipInputStream.close();
    out.close();

    if (deleteOriginal) {
        FileUtils.forceDelete(inputFile);
        if (verbose) {
            System.out.println(String.format("deleted original file %s.", inputFile.getAbsolutePath()));
        }
    }
    if (verbose) {
        System.out.println("unzipped " + inFilePath);
    }
    return new File(outFilePath);
}

From source file:com.crawlersick.nettool.GetattchmentFromMail1.java

public static String decompress(byte[] compressed) throws IOException {
    final int BUFFER_SIZE = 32;
    ByteArrayInputStream is = new ByteArrayInputStream(compressed);
    GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE);
    StringBuilder string = new StringBuilder();
    byte[] data = new byte[BUFFER_SIZE];
    int bytesRead;
    while ((bytesRead = gis.read(data)) != -1) {
        string.append(new String(data, 0, bytesRead));
    }//  ww w.  ja  v  a 2  s  . c o  m
    gis.close();
    is.close();
    return string.toString();
}

From source file:org.zaproxy.libs.DownloadTools.java

public static void downloadDriver(String urlStr, String destDir, String destFile) {
    File dest = new File(destDir + destFile);
    if (dest.exists()) {
        System.out.println("Already exists: " + dest.getAbsolutePath());
        return;//from   w w w. j a va2s  .c o m
    }
    File parent = dest.getParentFile();
    if (!parent.exists() && !parent.mkdirs()) {
        System.out.println("Failed to create directory : " + dest.getParentFile().getAbsolutePath());
    }
    byte[] buffer = new byte[1024];
    if (urlStr.endsWith(".zip")) {
        try {
            URL url = new URL(urlStr);
            ZipInputStream zipIn = new ZipInputStream(url.openStream());
            ZipEntry entry;

            boolean isFound = false;
            while ((entry = zipIn.getNextEntry()) != null) {
                if (destFile.equals(entry.getName())) {
                    isFound = true;
                    FileOutputStream out = new FileOutputStream(dest);
                    int read = 0;
                    while ((read = zipIn.read(buffer)) != -1) {
                        out.write(buffer, 0, read);
                    }
                    out.close();
                    System.out.println("Updated: " + dest.getAbsolutePath());

                } else {
                    System.out.println("Found " + entry.getName());
                }
                zipIn.closeEntry();
                entry = zipIn.getNextEntry();
            }

            zipIn.close();

            if (!isFound) {
                System.out.println("Failed to find " + destFile);
                System.exit(1);
            }

        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    } else if (urlStr.endsWith(".tar.gz")) {
        try {
            URL url = new URL(urlStr);
            GZIPInputStream gzis = new GZIPInputStream(url.openStream());

            File tarFile = new File(dest.getAbsolutePath() + ".tar");
            FileOutputStream out = new FileOutputStream(tarFile);

            int len;
            while ((len = gzis.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }

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

            TarArchiveInputStream tar = new TarArchiveInputStream(new FileInputStream(tarFile));
            ArchiveEntry entry;
            boolean isFound = false;
            while ((entry = tar.getNextEntry()) != null) {
                if (destFile.equals(entry.getName())) {
                    out = new FileOutputStream(dest);
                    isFound = true;

                    int read = 0;
                    while ((read = tar.read(buffer)) != -1) {
                        out.write(buffer, 0, read);
                    }
                    out.close();
                    System.out.println("Updated: " + dest.getAbsolutePath());
                }
            }
            tar.close();
            tarFile.delete();
            if (!isFound) {
                System.out.println("Failed to find " + destFile);
                System.exit(1);
            }

        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    }
}

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;
    }// w w w  . jav a2  s .  c om
    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:org.deeplearning4j.util.ArchiveUtils.java

/**
 * Extracts files to the specified destination
 * @param file the file to extract to// ww  w  . ja  va  2 s  .  co  m
 * @param dest the destination directory
 * @throws 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);

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

            //createComplex all non exists folders
            //else you will hit FileNotFoundException for compressed folder
            new File(newFile.getParent()).mkdirs();

            FileOutputStream fos = new FileOutputStream(newFile);

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

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

        zis.closeEntry();
        zis.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();
    }

    target.delete();

}