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.yacy.document.parser.gzipParser.java

@Override
public Document[] parse(final DigestURL location, final String mimeType, final String charset,
        final VocabularyScraper scraper, final int timezoneOffset, final InputStream source)
        throws Parser.Failure, InterruptedException {

    File tempFile = null;/*from  w  ww .  j  a  va  2s  .  c  o  m*/
    Document maindoc = null;
    try {
        int read = 0;
        final byte[] data = new byte[1024];

        final GZIPInputStream zippedContent = new GZIPInputStream(source);

        tempFile = File.createTempFile("gunzip", "tmp");

        // creating a temp file to store the uncompressed data
        final FileOutputStream out = new FileOutputStream(tempFile);

        // reading gzip file and store it uncompressed
        while ((read = zippedContent.read(data, 0, 1024)) != -1) {
            out.write(data, 0, read);
        }
        zippedContent.close();
        out.close();
        final String filename = location.getFileName();
        // create maindoc for this gzip container, register with supplied url & mime
        maindoc = new Document(location, mimeType, charset, this, null, null,
                AbstractParser.singleList(
                        filename.isEmpty() ? location.toTokens() : MultiProtocolURL.unescape(filename)), // title
                null, null, null, null, 0.0d, 0.0d, (Object) null, null, null, null, false, new Date());
        // creating a new parser class to parse the unzipped content
        final String contentfilename = GzipUtils.getUncompressedFilename(location.getFileName());
        final String mime = TextParser.mimeOf(DigestURL.getFileExtension(contentfilename));
        Document[] docs = TextParser.parseSource(location, mime, null, scraper, timezoneOffset, 999, tempFile);
        if (docs != null)
            maindoc.addSubDocuments(docs);
    } catch (final Exception e) {
        if (e instanceof InterruptedException)
            throw (InterruptedException) e;
        if (e instanceof Parser.Failure)
            throw (Parser.Failure) e;

        throw new Parser.Failure("Unexpected error while parsing gzip file. " + e.getMessage(), location);
    } finally {
        if (tempFile != null)
            FileUtils.deletedelete(tempFile);
    }
    return maindoc == null ? null : new Document[] { maindoc };
}

From source file:open.dolphin.infomodel.JsonConverter.java

public Object fromGzippedJson(InputStream is, TypeReference typeRef) {

    GZIPInputStream gis = null;
    try {//from   w w  w.ja va 2  s  . co m
        gis = new GZIPInputStream(is);
        Object obj = fromJson(gis, typeRef);
        return obj;
    } catch (IOException ex) {
        processException(ex);
    } finally {
        try {
            if (gis != null) {
                gis.close();
            }
        } catch (IOException ex) {
        }
    }
    return null;
}

From source file:org.openmrs.module.odkconnector.serialization.serializer.openmrs.CohortSerializerTest.java

@Test
public void serialize_shouldSerializeCohortInformation() throws Exception {

    CohortService cohortService = Context.getCohortService();

    Cohort firstCohort = new Cohort();
    firstCohort.addMember(6);//from w  w w . ja v  a2  s .c o  m
    firstCohort.addMember(7);
    firstCohort.addMember(8);
    firstCohort.setName("First Cohort");
    firstCohort.setDescription("First cohort for testing the serializer");
    cohortService.saveCohort(firstCohort);

    Cohort secondCohort = new Cohort();
    secondCohort.addMember(6);
    secondCohort.addMember(7);
    secondCohort.addMember(8);
    secondCohort.setName("Second Cohort");
    secondCohort.setDescription("Second cohort for testing the serializer");
    cohortService.saveCohort(secondCohort);

    File file = File.createTempFile("CohortSerialization", "Example");
    GZIPOutputStream outputStream = new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

    List<Cohort> cohorts = cohortService.getAllCohorts();
    Serializer serializer = HandlerUtil.getPreferredHandler(Serializer.class, List.class);
    serializer.write(outputStream, cohorts);

    outputStream.close();

    GZIPInputStream inputStream = new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)));
    DataInputStream dataInputStream = new DataInputStream(inputStream);

    Integer cohortCounts = dataInputStream.readInt();
    System.out.println("Number of cohorts: " + cohortCounts);

    for (int i = 0; i < cohortCounts; i++) {
        System.out.println("Cohort ID: " + dataInputStream.readInt());
        System.out.println("Cohort Name: " + dataInputStream.readUTF());
    }

    inputStream.close();
}

From source file:org.pentaho.di.www.SlaveServerTransStatus.java

public SlaveServerTransStatus(Node transStatusNode) {
    this();/* w  ww.ja  v  a 2s.com*/
    id = XMLHandler.getTagValue(transStatusNode, "id");
    transName = XMLHandler.getTagValue(transStatusNode, "transname");
    statusDescription = XMLHandler.getTagValue(transStatusNode, "status_desc");
    errorDescription = XMLHandler.getTagValue(transStatusNode, "error_desc");
    paused = "Y".equalsIgnoreCase(XMLHandler.getTagValue(transStatusNode, "paused"));

    Node statusListNode = XMLHandler.getSubNode(transStatusNode, "stepstatuslist");
    int nr = XMLHandler.countNodes(statusListNode, StepStatus.XML_TAG);
    for (int i = 0; i < nr; i++) {
        Node stepStatusNode = XMLHandler.getSubNodeByNr(statusListNode, StepStatus.XML_TAG, i);
        StepStatus stepStatus = new StepStatus(stepStatusNode);
        stepStatusList.add(stepStatus);
    }

    firstLoggingLineNr = Const.toInt(XMLHandler.getTagValue(transStatusNode, "first_log_line_nr"), 0);
    lastLoggingLineNr = Const.toInt(XMLHandler.getTagValue(transStatusNode, "last_log_line_nr"), 0);

    String loggingString64 = XMLHandler.getTagValue(transStatusNode, "logging_string");
    // This is a Base64 encoded GZIP compressed stream of data.
    try {
        byte[] bytes = new byte[] {};
        if (loggingString64 != null)
            bytes = Base64.decodeBase64(loggingString64.getBytes());
        if (bytes.length > 0) {
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            GZIPInputStream gzip = new GZIPInputStream(bais);
            int c;
            StringBuffer buffer = new StringBuffer();
            while ((c = gzip.read()) != -1)
                buffer.append((char) c);
            gzip.close();
            loggingString = buffer.toString();
        } else {
            loggingString = "";
        }
    } catch (IOException e) {
        loggingString = "Unable to decode logging from remote server : " + e.toString() + Const.CR
                + Const.getStackTracker(e);
    }

    // get the result object, if there is any...
    //
    Node resultNode = XMLHandler.getSubNode(transStatusNode, Result.XML_TAG);
    if (resultNode != null) {
        try {
            result = new Result(resultNode);
        } catch (KettleException e) {
            loggingString += "Unable to serialize result object as XML" + Const.CR + Const.getStackTracker(e)
                    + Const.CR;
        }
    }
}

From source file:org.owasp.dependencycheck.data.update.CpeUpdater.java

/**
 * Extracts the file contained in a gzip archive. The extracted file is placed in the exact same path as the file specified.
 *
 * @param file the archive file/*from w  w w .j  a  v  a  2 s  . com*/
 * @throws FileNotFoundException thrown if the file does not exist
 * @throws IOException thrown if there is an error extracting the file.
 */
private void extractGzip(File file) throws FileNotFoundException, IOException {
    //TODO - move this to a util class as it is duplicative of (copy of) code in the DownloadTask
    final String originalPath = file.getPath();
    final File gzip = new File(originalPath + ".gz");
    if (gzip.isFile() && !gzip.delete()) {
        gzip.deleteOnExit();
    }
    if (!file.renameTo(gzip)) {
        throw new IOException("Unable to rename '" + file.getPath() + "'");
    }
    final File newfile = new File(originalPath);

    final byte[] buffer = new byte[4096];

    GZIPInputStream cin = null;
    FileOutputStream out = null;
    try {
        cin = new GZIPInputStream(new FileInputStream(gzip));
        out = new FileOutputStream(newfile);

        int len;
        while ((len = cin.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
    } finally {
        if (cin != null) {
            try {
                cin.close();
            } catch (IOException ex) {
                LOGGER.trace("ignore", ex);
            }
        }
        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
                LOGGER.trace("ignore", ex);
            }
        }
        if (gzip.isFile()) {
            FileUtils.deleteQuietly(gzip);
        }
    }
}

From source file:org.xwiki.contrib.dokuwiki.text.internal.input.DokuWikiInputFilterStream.java

private String extractGZip(File file) throws IOException {
    GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(file));
    String content = IOUtils.toString(gzipInputStream, StandardCharsets.UTF_8);
    gzipInputStream.close();
    return content;
}

From source file:org.semanticscience.narf.structures.factories.tertiary.ExtractedTertiaryStructureFactory.java

/**
 * Get all nucleic acid tertiary structures produced using information
 * generated by the tertiary structure predictor.
 * /* w ww.jav  a2 s.co  m*/
 * @param aPdbFile
 *            a PDB structure file
 * @param commands
 *            set of commands to modify the execution of the annoator
 * @return a set of annotated nucleic acid tertiary structures
 * @throws FileNotFoundException
 *             if the PDB structure file does not exist
 * @throws IOException
 *             if any IO error occur reading the output of the tertiary
 *             structure annotator or writing the output of the tertiary
 *             structure annotator
 * @throws InvalidResidueException
 *             if any of the residues are invalid
 */
public Set<ExtractedTertiaryStructure> getStructures(File aPdbFile, String[] commands)
        throws FileNotFoundException, IOException, InvalidResidueException {

    if (!aPdbFile.exists() || aPdbFile.isDirectory()) {
        throw new FileNotFoundException("There is no PDB file with the name specified.");
    }

    String extension = PdbHelper.getFileExtension(aPdbFile);
    File directory = new File(FileUtils.getTempDirectoryPath() + "/pdb/");
    File pdbFile = new File(directory.getAbsolutePath() + "/" + aPdbFile.getName());
    FileUtils.copyFile(aPdbFile, pdbFile);
    // check if the file is compressed
    if (extension.equals("gz")) {
        GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(pdbFile));
        File gunzippedFile = new File(directory.getAbsolutePath() + pdbFile.getName().replace(".gz", ""));
        OutputStream out = new FileOutputStream(gunzippedFile);
        byte[] buf = new byte[1024];
        int len;
        while ((len = gzipInputStream.read(buf)) > 0)
            out.write(buf, 0, len);
        gzipInputStream.close();
        out.close();
        pdbFile.delete();
        pdbFile = gunzippedFile;
    }
    String pdbId = PdbHelper.findPdbId(pdbFile);
    int numberOfModels = PdbHelper.findNumberOfModels(pdbFile);
    Set<ExtractedTertiaryStructure> tertiaryAnnotatedStructures = new HashSet<ExtractedTertiaryStructure>();
    if (numberOfModels == 1) {
        File annotatedStructure = this.execute(pdbFile, commands);
        Map<String, Sequence> sequenceMap = this.parseSequences(pdbFile, annotatedStructure);
        Set<NucleotideInteraction> interactions = this.parseInteractions(sequenceMap, annotatedStructure);
        tertiaryAnnotatedStructures
                .add(new ExtractedTertiaryStructure(this, pdbFile, pdbId, 1, sequenceMap, interactions));
    } else {
        for (int modelNumber = 1; modelNumber <= numberOfModels; modelNumber++) {
            File modelFile = PdbHelper.extractModelFromPDB(pdbFile,
                    new File(FileUtils.getTempDirectoryPath() + "/pdb/"), pdbId, modelNumber);
            File outputFile = this.execute(modelFile, commands);
            Map<String, Sequence> sequenceMap = this.parseSequences(modelFile, outputFile);
            Set<NucleotideInteraction> interactions = this.parseInteractions(sequenceMap, outputFile);
            tertiaryAnnotatedStructures.add(new ExtractedTertiaryStructure(this, pdbFile, pdbId, modelNumber,
                    sequenceMap, interactions));
        }
    }
    return tertiaryAnnotatedStructures;
}

From source file:org.mitre.xtext.converters.ArchiveNavigator.java

private File gunzip(File theFile, String fname) throws IOException {

    GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(theFile));
    String outFilename = tempDir.toString() + File.separator + fname + ".tar";
    File outFile = new File(outFilename);
    OutputStream out = new BufferedOutputStream(new FileOutputStream(outFilename));

    byte[] buf = new byte[1024];
    int len;// ww w. j a v a  2s  .  c o m
    while ((len = gzipInputStream.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    gzipInputStream.close();
    out.close();
    return outFile;
}

From source file:com.mingsoft.util.proxy.Result.java

/**
 * ?gzip?/*  w  w  w .j av a2 s.  c  o m*/
 * 
 * @param charSet
 *            ?
 * @return InputStreamReade;
 */
public String getContentForGzip(String charset) {
    if (httpEntity.getContentEncoding().getValue().indexOf("gzip") > -1) {
        try {
            GZIPInputStream gzipis = new GZIPInputStream(httpEntity.getContent());

            InputStreamReader isr = new InputStreamReader(gzipis, charset); // ?????
            java.io.BufferedReader br = new java.io.BufferedReader(isr);
            String tempbf;
            StringBuffer sb = new StringBuffer();
            while ((tempbf = br.readLine()) != null) {
                sb.append(tempbf);
                sb.append("\r\n");
            }
            gzipis.close();
            isr.close();
            return sb.toString();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

From source file:com.sap.nwcloudmanager.api.LogAPI.java

public static void downloadLog(final Context context, final String appId, final String logName,
        final DownloadLogResponseHandler downloadLogResponseHandler) {

    NetWeaverCloudConfig config = NetWeaverCloudConfig.getInstance();
    getHttpClient().setBasicAuth(config.getUsername(), config.getPassword());
    String[] contentTypes = { "application/x-gzip" };
    getHttpClient().get("https://monitoring." + config.getHost() + "/log/api_basic/logs/" + config.getAccount()
            + "/" + appId + "/web/" + logName, null, new BinaryHttpResponseHandler(contentTypes) {

                /*//from w  ww  . j ava2 s .c o m
                 * (non-Javadoc)
                 * 
                 * @see
                 * com.loopj.android.http.BinaryHttpResponseHandler#onSuccess(byte
                 * [])
                 */
                @Override
                public void onSuccess(byte[] arg0) {

                    GZIPInputStream gzis = null;

                    try {
                        gzis = new GZIPInputStream(new ByteArrayInputStream(arg0));

                        StringBuilder string = new StringBuilder();
                        byte[] data = new byte[4028];
                        int bytesRead;
                        while ((bytesRead = gzis.read(data)) != -1) {
                            string.append(new String(data, 0, bytesRead));
                        }

                        File downloadsFile = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
                        if (downloadsFile.exists()) {
                            downloadsFile = new File(
                                    context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
                                            .getAbsolutePath() + "/" + logName);
                            FileOutputStream fos = new FileOutputStream(downloadsFile);
                            fos.write(string.toString().getBytes());
                            fos.close();
                        }

                        downloadLogResponseHandler.onSuccess(downloadsFile.getAbsolutePath());

                    } catch (IOException e) {
                        Log.e(e.getMessage());
                    } finally {
                        try {
                            if (gzis != null) {
                                gzis.close();
                            }
                        } catch (IOException e) {
                        }
                    }
                }

                @Override
                public void onFailure(Throwable arg0) {
                    downloadLogResponseHandler.onFailure(arg0, arg0.getMessage());
                }

            });
}