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:org.ecoinformatics.seek.datasource.EcogridGZippedDataCacheItem.java

/**
 * This method overwrite the super class - EcogridCompressedDataCacheItem.
 * It specifys the gunzip method to uncompress the file. The new ungzip file
 * will be the the cacheitem file name without the file extension. An file
 * location will be the cachedata/unzip/cacheitemname/ If this is tar file
 * too, we will untar it after ungzip it.
 * /*  w ww. ja  va  2  s . c om*/
 * @throws Exception
 */
public void unCompressCacheItem() throws Exception {
    if (unCompressedFilePath != null) {
        log.debug("At unCompressCacheItem method in Zip ojbect");
        // read the gzip file and ungzip it
        GZIPInputStream gZipFileReader = new GZIPInputStream(new FileInputStream(getFile()));
        String unGZipFileName = removeFileExtension(getAbsoluteFileName());
        String unGZipFilePath = unCompressedFilePath + File.separator + unGZipFileName;
        log.debug("The unGzip aboslute file path is " + unGZipFilePath);
        File unGzipFile = new File(unGZipFilePath);
        FileOutputStream fileWriter = new FileOutputStream(unGzipFile);
        byte[] array = new byte[3000 * 1024];
        int len;
        while ((len = gZipFileReader.read(array)) >= 0) {
            fileWriter.write(array, 0, len);
        }
        gZipFileReader.close();
        fileWriter.close();
        // if this is a tar file too, will untar it.
        if (getIsTarFile()) {
            if (unCompressedFilePath != null) {
                log.debug("untar the file after ungzip");
                EcogridTarArchivedDataCacheItem.extractTarFile(unGZipFilePath, unCompressedCacheItemDir);
            }
        }
        unCompressedFileList = unCompressedCacheItemDir.list();
    }
}

From source file:org.intermine.api.lucene.KeywordSearch.java

private static RAMDirectory readRAMDirectory(InputStream is) throws IOException, ClassNotFoundException {
    long time = System.currentTimeMillis();
    GZIPInputStream gzipInput = new GZIPInputStream(is);
    ObjectInputStream objectInput = new ObjectInputStream(gzipInput);

    try {//from w w w . ja v a  2 s  .  com
        Object object = objectInput.readObject();

        if (object instanceof FSDirectory) {
            RAMDirectory directory = (RAMDirectory) object;

            time = System.currentTimeMillis() - time;
            LOG.info("Successfully restored RAM directory" + " from database in " + time + " ms");
            return directory;
        }
    } finally {
        objectInput.close();
        gzipInput.close();
    }
    return null;
}

From source file:com.omertron.thetvdbapi.tools.WebBrowser.java

/**
 * Request the web page at the specified URL
 *
 * @param url/*from w  w  w. jav a  2s .c  o m*/
 * @throws IOException
 */
public static String request(URL url) throws IOException {
    StringBuilder content = new StringBuilder();

    BufferedReader in = null;
    URLConnection cnx = null;
    InputStreamReader isr = null;
    GZIPInputStream zis = null;

    try {
        cnx = openProxiedConnection(url);

        sendHeader(cnx);
        readHeader(cnx);

        // Check the content encoding of the connection. Null content encoding is standard HTTP
        if (cnx.getContentEncoding() == null) {
            //in = new BufferedReader(new InputStreamReader(cnx.getInputStream(), getCharset(cnx)));
            isr = new InputStreamReader(cnx.getInputStream(), "UTF-8");
        } else if (cnx.getContentEncoding().equalsIgnoreCase("gzip")) {
            zis = new GZIPInputStream(cnx.getInputStream());
            isr = new InputStreamReader(zis, "UTF-8");
        } else {
            return "";
        }

        in = new BufferedReader(isr);

        String line;
        while ((line = in.readLine()) != null) {
            content.append(line);
        }
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (isr != null) {
            try {
                isr.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (zis != null) {
            try {
                zis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        if (cnx instanceof HttpURLConnection) {
            ((HttpURLConnection) cnx).disconnect();
        }
    }
    return content.toString();
}

From source file:model.Modele.java

/**
 * Methode qui tlcharger le fichier des donnes du site directement
 *
 * @param jour jour du fichier  telecharger
 * @param mois mois du fichier  telecharger
 * @param annee annee du fichier  telecharger
 * @param heure heure du fichier  telecharger
 * @return nom du fichier tlcharger/*www .  ja  va 2 s. c  o m*/
 * @throws MalformedURLException
 * @throws IOException
 */
public String Download_File(String jour, String mois, String annee, String heure)
        throws MalformedURLException, IOException {

    if (jour.equals("NULL") && heure.equals("NULL")) {

        URL url = new URL("https://donneespubliques.meteofrance.fr/donnees_libres/Txt/Synop/Archive/synop."
                + annee + mois + ".csv.gz");
        File file = new File("synop." + annee + mois + ".csv.gz");
        FileUtils.copyURLToFile(url, file);

        FileInputStream in = new FileInputStream("synop." + annee + mois + ".csv.gz");
        GZIPInputStream zipin = new GZIPInputStream(in);
        byte[] buffer = new byte[8192];
        FileOutputStream out = new FileOutputStream("synop." + annee + mois + ".csv.gz.txt");
        int length;
        while ((length = zipin.read(buffer, 0, 8192)) != -1) {
            out.write(buffer, 0, length);
        }
        out.close();
        zipin.close();

        File f = new File("synop." + annee + mois + ".csv.gz.txt");
        if (f.exists()) {
            nom_fichier = Save_data(f);
        } else {
            JOptionPane.showMessageDialog(null, "Fichier inexistant ou n'a pas pu tre tlcharg !");
        }

    } else {
        if (annee.equals("2017")) {

            URL url = new URL("https://donneespubliques.meteofrance.fr/donnees_libres/Txt/Synop/Archive/synop."
                    + annee + mois + jour + heure + ".csv.gz");
            File file = new File("synop." + annee + mois + jour + heure + ".csv");
            FileUtils.copyURLToFile(url, file);

            File f = new File("synop." + annee + mois + jour + heure + ".csv");
            if (f.exists()) {
                nom_fichier = Save_data(f);
            } else {
                JOptionPane.showMessageDialog(null, "Fichier inexistant ou n'a pas pu tre tlcharg !");
            }

        } else {
            JOptionPane.showMessageDialog(null, "Seulement l'anne 2017 disponible pour cette option !");
        }

    }

    return nom_fichier;
}

From source file:org.asynchttpclient.request.body.multipart.MultipartUploadTest.java

/**
 * Test that the files were sent, based on the response from the servlet
 * //from   ww w .  jav a2s.c  o m
 * @param expectedContents
 * @param sourceFiles
 * @param r
 * @param deflate
 */
private void testSentFile(List<String> expectedContents, List<File> sourceFiles, Response r,
        List<Boolean> deflate) {
    String content = r.getResponseBody();
    assertNotNull("===>" + content);
    logger.debug(content);

    String[] contentArray = content.split("\\|\\|");
    // TODO: this fail on win32
    assertEquals(contentArray.length, 2);

    String tmpFiles = contentArray[1];
    assertNotNull(tmpFiles);
    assertTrue(tmpFiles.trim().length() > 2);
    tmpFiles = tmpFiles.substring(1, tmpFiles.length() - 1);

    String[] responseFiles = tmpFiles.split(",");
    assertNotNull(responseFiles);
    assertEquals(responseFiles.length, sourceFiles.size());

    logger.debug(Arrays.toString(responseFiles));

    int i = 0;
    for (File sourceFile : sourceFiles) {

        File tmp = null;
        try {

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] sourceBytes = null;
            try (FileInputStream instream = new FileInputStream(sourceFile)) {
                byte[] buf = new byte[8092];
                int len = 0;
                while ((len = instream.read(buf)) > 0) {
                    baos.write(buf, 0, len);
                }
                logger.debug("================");
                logger.debug("Length of file: " + baos.toByteArray().length);
                logger.debug("Contents: " + Arrays.toString(baos.toByteArray()));
                logger.debug("================");
                System.out.flush();
                sourceBytes = baos.toByteArray();
            }

            tmp = new File(responseFiles[i].trim());
            logger.debug("==============================");
            logger.debug(tmp.getAbsolutePath());
            logger.debug("==============================");
            System.out.flush();
            assertTrue(tmp.exists());

            byte[] bytes;
            try (FileInputStream instream = new FileInputStream(tmp)) {
                ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
                byte[] buf = new byte[8092];
                int len = 0;
                while ((len = instream.read(buf)) > 0) {
                    baos2.write(buf, 0, len);
                }
                bytes = baos2.toByteArray();
                assertEquals(bytes, sourceBytes);
            }

            if (!deflate.get(i)) {
                String helloString = new String(bytes);
                assertEquals(helloString, expectedContents.get(i));
            } else {
                try (FileInputStream instream = new FileInputStream(tmp)) {
                    ByteArrayOutputStream baos3 = new ByteArrayOutputStream();
                    GZIPInputStream deflater = new GZIPInputStream(instream);
                    try {
                        byte[] buf3 = new byte[8092];
                        int len3 = 0;
                        while ((len3 = deflater.read(buf3)) > 0) {
                            baos3.write(buf3, 0, len3);
                        }
                    } finally {
                        deflater.close();
                    }

                    String helloString = new String(baos3.toByteArray());

                    assertEquals(expectedContents.get(i), helloString);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            fail("Download Exception");
        } finally {
            if (tmp != null)
                FileUtils.deleteQuietly(tmp);
            i++;
        }
    }
}

From source file:net.fabricmc.loom.task.DownloadTask.java

@TaskAction
public void download() {
    try {//w  w  w  .  j a  va  2 s  .c  o m
        LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class);

        downloadMcJson(extension, getLogger());

        Gson gson = new Gson();
        Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class);

        if (!Constants.MINECRAFT_CLIENT_JAR.get(extension).exists() || !Checksum
                .equals(Constants.MINECRAFT_CLIENT_JAR.get(extension), version.downloads.get("client").sha1)) {
            this.getLogger().lifecycle(":downloading client");
            FileUtils.copyURLToFile(new URL(version.downloads.get("client").url),
                    Constants.MINECRAFT_CLIENT_JAR.get(extension));
        }

        if (!Constants.MINECRAFT_SERVER_JAR.get(extension).exists() || !Checksum
                .equals(Constants.MINECRAFT_SERVER_JAR.get(extension), version.downloads.get("server").sha1)) {
            this.getLogger().lifecycle(":downloading server");
            FileUtils.copyURLToFile(new URL(version.downloads.get("server").url),
                    Constants.MINECRAFT_SERVER_JAR.get(extension));
        }

        if (!Constants.POMF_DIR.get(extension).exists()) {
            Constants.POMF_DIR.get(extension).mkdir();
        }

        if (!Constants.MAPPINGS_ZIP.get(extension).exists() && extension.hasPomf()) {
            this.getLogger().lifecycle(":downloading mappings");
            try {
                FileUtils.copyURLToFile(
                        new URL("http://modmuss50.me:8080/job/FabricMC/job/pomf/job/" + extension.version + "/"
                                + extension.pomfVersion + "/artifact/build/libs/pomf-enigma-"
                                + extension.version + "." + extension.pomfVersion + ".zip"),
                        Constants.MAPPINGS_ZIP.get(extension));
            } catch (Exception e) {
                throw new RuntimeException("Failed to download mappings", e);
            }

        }
        if (!extension.hasPomf()) {
            if (Constants.MAPPINGS_DIR_LOCAL.get(extension).exists()) {
                if (Constants.MAPPINGS_TINY_GZ_LOCAL.get(extension).exists()
                        && Constants.MAPPINGS_ZIP_LOCAL.get(extension).exists()) {
                    this.getLogger().lifecycle(":using local mappings!");
                    extension.localMappings = true;

                    //We delete this to make sure they are always re extracted.
                    deleteIfExists(Constants.MAPPINGS_ZIP.get(extension));
                    deleteIfExists(Constants.MAPPINGS_TINY_GZ.get(extension));
                    deleteIfExists(Constants.MAPPINGS_TINY.get(extension));
                    deleteIfExists(Constants.MAPPINGS_DIR.get(extension));

                    Constants.MAPPINGS_TINY_GZ = Constants.MAPPINGS_TINY_GZ_LOCAL;
                    Constants.MAPPINGS_ZIP = Constants.MAPPINGS_ZIP_LOCAL;
                }
            }
        }

        if (!Constants.MAPPINGS_TINY.get(extension).exists() && extension.hasPomf()) {
            if (!Constants.MAPPINGS_TINY_GZ.get(extension).exists() && !extension.localMappings) {
                getLogger().lifecycle(":downloading tiny mappings");
                try {
                    FileUtils.copyURLToFile(
                            new URL("http://modmuss50.me:8080/job/FabricMC/job/pomf/job/" + extension.version
                                    + "/" + extension.pomfVersion + "/artifact/build/libs/pomf-tiny-"
                                    + extension.version + "." + extension.pomfVersion + ".gz"),
                            Constants.MAPPINGS_TINY_GZ.get(extension));
                } catch (Exception e) {
                    throw new RuntimeException("Failed to download mappings", e);
                }
            }
            GZIPInputStream gzipInputStream = new GZIPInputStream(
                    new FileInputStream(Constants.MAPPINGS_TINY_GZ.get(extension)));
            FileOutputStream fileOutputStream = new FileOutputStream(Constants.MAPPINGS_TINY.get(extension));
            int length;
            byte[] buffer = new byte[1024];
            while ((length = gzipInputStream.read(buffer)) > 0) {
                fileOutputStream.write(buffer, 0, length);
            }
            gzipInputStream.close();
            fileOutputStream.close();
        }

        DependencyHandler dependencyHandler = getProject().getDependencies();

        if (getProject().getConfigurations().getByName(Constants.CONFIG_MC_DEPENDENCIES)
                .getState() == Configuration.State.UNRESOLVED) {
            for (Version.Library library : version.libraries) {
                if (library.allowed() && library.getFile(extension) != null) {
                    // By default, they are all available on all sides
                    String configName = Constants.CONFIG_MC_DEPENDENCIES;
                    if (library.name.contains("java3d") || library.name.contains("paulscode")
                            || library.name.contains("lwjgl") || library.name.contains("twitch")
                            || library.name.contains("jinput") || library.name.contains("text2speech")
                            || library.name.contains("objc")) {
                        configName = Constants.CONFIG_MC_DEPENDENCIES_CLIENT;
                    }
                    dependencyHandler.add(configName, library.getArtifactName());
                }
            }
        }

        if (getProject().getConfigurations().getByName(Constants.CONFIG_NATIVES)
                .getState() == Configuration.State.UNRESOLVED) {
            version.libraries.stream().filter(lib -> lib.natives != null)
                    .forEach(lib -> dependencyHandler.add(Constants.CONFIG_NATIVES, lib.getArtifactName()));
        }

        // Force add LaunchWrapper
        dependencyHandler.add(Constants.CONFIG_MC_DEPENDENCIES, "net.minecraft:launchwrapper:1.12");

        Version.AssetIndex assetIndex = version.assetIndex;

        File assets = new File(extension.getFabricUserCache(), "assets-" + extension.version);
        if (!assets.exists()) {
            assets.mkdirs();
        }

        File assetsInfo = new File(assets, "indexes" + File.separator + assetIndex.id + ".json");
        if (!assetsInfo.exists() || !Checksum.equals(assetsInfo, assetIndex.sha1)) {
            this.getLogger().lifecycle(":downloading asset index");
            FileUtils.copyURLToFile(new URL(assetIndex.url), assetsInfo);
        }

        ProgressLogger progressLogger = ProgressLogger.getProgressFactory(getProject(), getClass().getName());
        progressLogger.start("Downloading assets...", "assets");
        AssetIndex index = new Gson().fromJson(new FileReader(assetsInfo), AssetIndex.class);
        Map<String, AssetObject> parent = index.getFileMap();
        final int totalSize = parent.size();
        int position = 0;
        this.getLogger().lifecycle(":downloading assets...");
        for (Map.Entry<String, AssetObject> entry : parent.entrySet()) {
            AssetObject object = entry.getValue();
            String sha1 = object.getHash();
            File file = new File(assets,
                    "objects" + File.separator + sha1.substring(0, 2) + File.separator + sha1);
            if (!file.exists() || !Checksum.equals(file, sha1)) {
                this.getLogger().debug(":downloading asset " + entry.getKey());
                FileUtils.copyURLToFile(new URL(Constants.RESOURCES_BASE + sha1.substring(0, 2) + "/" + sha1),
                        file);
            }
            String assetName = entry.getKey();
            int end = assetName.lastIndexOf("/") + 1;
            if (end > 0) {
                assetName = assetName.substring(end, assetName.length());
            }
            progressLogger.progress(assetName + " - " + position + "/" + totalSize + " ("
                    + (int) ((position / (double) totalSize) * 100) + "%) assets downloaded");
            position++;
        }
        progressLogger.completed();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.csipsimple.service.DownloadLibService.java

private boolean installRemoteLib(RemoteLibInfo lib) {
    String fileName = lib.getFileName();
    File filePath = lib.getFilePath();

    File tmp_gz = new File(filePath, fileName + ".gz");
    File dest = new File(filePath, fileName);

    try {//from w ww. jav a 2  s  . c o  m
        if (dest.exists()) {
            dest.delete();
        }
        RandomAccessFile out = new RandomAccessFile(dest, "rw");
        out.seek(0);
        GZIPInputStream zis = new GZIPInputStream(new FileInputStream(tmp_gz));
        int len;
        byte[] buf = new byte[BUFFER];
        while ((len = zis.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        zis.close();
        out.close();
        Log.d(THIS_FILE, "Ungzip is in : " + dest.getAbsolutePath());
        tmp_gz.delete();
        //Update preferences fields with current stack values
        Editor editor = prefs.edit();
        editor.putString(CURRENT_STACK_ID, lib.getId());
        editor.putString(CURRENT_STACK_VERSION, lib.getVersion());
        editor.putString(CURRENT_STACK_URI, lib.getDownloadUri().toString());
        editor.commit();
        return true;
    } catch (IOException e) {
        Log.e(THIS_FILE, "We failed to install it ", e);
    }
    return false;
}

From source file:org.atomserver.core.filestore.FileBasedContentStorage.java

protected String readFileToString(File file) throws IOException {
    if (log.isTraceEnabled()) {
        log.trace("reading file : " + file);
    }// w  w w.  j a  v  a  2  s  .co  m
    if (file.getName().endsWith(GZIP_EXTENSION)) {
        GZIPInputStream inputStream = new GZIPInputStream(new FileInputStream(file));
        String content = IOUtils.toString(inputStream, UTF_8);
        inputStream.close();
        return content;
    } else {
        return FileUtils.readFileToString(file, UTF_8);
    }
}

From source file:org.smartfrog.avalanche.client.sf.compress.ZipUtils.java

public static boolean gunzip(File gzipFile, File outputFile) throws IOException {
    GZIPInputStream gzipInstream;

    String gzFileName = gzipFile.getName();
    if (!gzipFile.exists()) {
        log.error("The file " + gzFileName + " does not exist");
        return false;
    }//from www .j a  v  a  2 s. c  om
    if (!gzipFile.isFile()) {
        log.error("The file " + gzFileName + " is not a file");
        return false;
    }
    if (outputFile.exists()) {
        // overwrite
    }
    if (outputFile.isDirectory()) {
        log.error("The given path " + outputFile + " is a directory");
        return false;
    }

    if (gzipFile.getAbsolutePath().equals(outputFile.getAbsolutePath())) {
        log.error("Source and destination paths for un-zipping the file are same.");
        return false;
    }

    String file = gzipFile.getPath();
    FileInputStream in = new FileInputStream(file);
    BufferedInputStream src = new BufferedInputStream(in);
    gzipInstream = new GZIPInputStream(src);

    byte[] buffer = new byte[chunkSize];
    int len = 0;

    FileOutputStream out = new FileOutputStream(outputFile);
    BufferedOutputStream output = new BufferedOutputStream(out, chunkSize);

    /*
     * Read from gzip stream which will uncompress and write to output
     * stream
     */
    while ((len = gzipInstream.read(buffer, 0, chunkSize)) != -1) {
        output.write(buffer, 0, len);
    }

    output.flush();
    out.close();

    gzipInstream.close();

    log.info("Uncompress completed for file - " + gzFileName);
    return true;
}

From source file:org.asynchttpclient.async.MultipartUploadTest.java

/**
 * Test that the files were sent, based on the response from the servlet
 * //ww  w  .  ja  v  a  2s  .  c  om
 * @param expectedContents
 * @param sourceFiles
 * @param r
 * @param deflate
 */
private void testSentFile(List<String> expectedContents, List<File> sourceFiles, Response r,
        List<Boolean> deflate) {
    String content = null;
    try {
        content = r.getResponseBody();
        assertNotNull("===>" + content);
        logger.debug(content);
    } catch (IOException e) {
        fail("Unable to obtain content");
    }

    String[] contentArray = content.split("\\|\\|");
    // TODO: this fail on win32
    assertEquals(contentArray.length, 2);

    String tmpFiles = contentArray[1];
    assertNotNull(tmpFiles);
    assertTrue(tmpFiles.trim().length() > 2);
    tmpFiles = tmpFiles.substring(1, tmpFiles.length() - 1);

    String[] responseFiles = tmpFiles.split(",");
    assertNotNull(responseFiles);
    assertEquals(responseFiles.length, sourceFiles.size());

    logger.debug(Arrays.toString(responseFiles));

    int i = 0;
    for (File sourceFile : sourceFiles) {

        FileInputStream instream = null;
        File tmp = null;
        try {

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] sourceBytes = null;
            try {
                instream = new FileInputStream(sourceFile);
                byte[] buf = new byte[8092];
                int len = 0;
                while ((len = instream.read(buf)) > 0) {
                    baos.write(buf, 0, len);
                }
                logger.debug("================");
                logger.debug("Length of file: " + baos.toByteArray().length);
                logger.debug("Contents: " + Arrays.toString(baos.toByteArray()));
                logger.debug("================");
                System.out.flush();
                sourceBytes = baos.toByteArray();
            } finally {
                IOUtils.closeQuietly(instream);
            }

            tmp = new File(responseFiles[i].trim());
            logger.debug("==============================");
            logger.debug(tmp.getAbsolutePath());
            logger.debug("==============================");
            System.out.flush();
            assertTrue(tmp.exists());

            instream = new FileInputStream(tmp);
            ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
            byte[] buf = new byte[8092];
            int len = 0;
            while ((len = instream.read(buf)) > 0) {
                baos2.write(buf, 0, len);
            }
            IOUtils.closeQuietly(instream);

            assertEquals(baos2.toByteArray(), sourceBytes);

            if (!deflate.get(i)) {

                String helloString = new String(baos2.toByteArray());
                assertEquals(helloString, expectedContents.get(i));
            } else {
                instream = new FileInputStream(tmp);

                ByteArrayOutputStream baos3 = new ByteArrayOutputStream();
                GZIPInputStream deflater = new GZIPInputStream(instream);
                try {
                    byte[] buf3 = new byte[8092];
                    int len3 = 0;
                    while ((len3 = deflater.read(buf3)) > 0) {
                        baos3.write(buf3, 0, len3);
                    }
                } finally {
                    deflater.close();
                }

                String helloString = new String(baos3.toByteArray());

                assertEquals(expectedContents.get(i), helloString);

            }
        } catch (Exception e) {
            e.printStackTrace();
            fail("Download Exception");
        } finally {
            if (tmp != null)
                FileUtils.deleteQuietly(tmp);
            IOUtils.closeQuietly(instream);
            i++;
        }
    }
}