Example usage for java.util.zip GZIPInputStream GZIPInputStream

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

Introduction

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

Prototype

public GZIPInputStream(InputStream in) throws IOException 

Source Link

Document

Creates a new input stream with a default buffer size.

Usage

From source file:com.firegnom.valkyrie.map.tiled.TiledZoneLoader.java

@Override
public Zone load(String name) {
    long startTime, stopTime;
    try {//from  w w w  .  j av a  2  s  . c  o  m
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                return new InputSource(new ByteArrayInputStream(new byte[0]));
            }
        });
        startTime = System.currentTimeMillis();

        // map version
        JSONObject obj;
        int version = 0;
        try {
            obj = new JSONObject(convertStreamToString(rl.getResourceAsStreamDownload(name + ".json")));
            version = obj.getInt("version");
        } catch (JSONException e) {
            throw new ValkyrieRuntimeException(e);
        }

        InputStream inputStream = new GZIPInputStream(
                rl.getResourceAsStream(name + "-ver_" + version + ".tmx.gz", true));

        Document doc = builder.parse(inputStream);
        Element docElement = doc.getDocumentElement();
        stopTime = System.currentTimeMillis();
        Log.d(TAG, "Loaded Zone tmx file in  in :" + ((stopTime - startTime) / 1000) + " secondsand "
                + ((stopTime - startTime) % 1000) + " miliseconds");
        System.gc();
        String orient = docElement.getAttribute("orientation");
        if (!orient.equals("orthogonal")) {
            throw new TiledLoaderException("Only orthogonal maps supported, found: " + orient);
        }

        int width = Integer.parseInt(docElement.getAttribute("width"));
        int height = Integer.parseInt(docElement.getAttribute("height"));
        int tileWidth = Integer.parseInt(docElement.getAttribute("tilewidth"));
        int tileHeight = Integer.parseInt(docElement.getAttribute("tileheight"));

        Zone zone = new Zone(name, width, height, tileWidth, tileHeight);
        // now read the map properties
        startTime = System.currentTimeMillis();
        getZoneProperties(zone, docElement);
        stopTime = System.currentTimeMillis();
        Log.d(TAG, "Loaded Zone Properties  in  in :" + ((stopTime - startTime) / 1000) + " secondsand "
                + ((stopTime - startTime) % 1000) + " miliseconds");
        System.gc();
        startTime = System.currentTimeMillis();

        StringTileSet tileSet = null;
        NodeList setNodes = docElement.getElementsByTagName("tileset");
        int i;
        for (i = 0; i < setNodes.getLength(); i++) {
            Element current = (Element) setNodes.item(i);

            tileSet = getTileSet(zone, current, c);
            tileSet.index = i;
            Log.d(TAG, "Adding tileset to zone tilestets firstGID =" + tileSet.firstGID + ",lastGID="
                    + tileSet.lastGID + ", name=" + tileSet.imageName);
            zone.tileSets.add(tileSet.firstGID, tileSet.lastGID, tileSet);
        }
        stopTime = System.currentTimeMillis();
        Log.d("performance", "Loaded Zone tilesets in  in :" + ((stopTime - startTime) / 1000) + " secondsand "
                + ((stopTime - startTime) % 1000) + " miliseconds");
        System.gc();
        startTime = System.currentTimeMillis();
        NodeList layerNodes = docElement.getElementsByTagName("layer");
        Element current;
        for (i = 0; i < layerNodes.getLength(); i++) {
            current = (Element) layerNodes.item(i);
            Layer layer = getLayer(zone, current);
            layer.index = i;
            zone.layers.add(layer);

        }

        stopTime = System.currentTimeMillis();
        Log.d(TAG, "Loaded Zone Layers in  in :" + ((stopTime - startTime) / 1000) + " secondsand "
                + ((stopTime - startTime) % 1000) + " miliseconds");
        NodeList objectGroupNodes = docElement.getElementsByTagName("objectgroup");
        for (i = 0; i < objectGroupNodes.getLength(); i++) {
            current = (Element) objectGroupNodes.item(i);
            if (current.getAttribute("name").equals("_ContextActions")) {
                zone.contextActions = getActionIndex(current);
            } else {
                appendMapObjects(current, zone.mapObjects);
            }
        }
        System.gc();
        return zone;

    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

}

From source file:com.zimbra.qa.unittest.TestUserServlet.java

private void verifyTarball(ZMailbox mbox, String relativePath, boolean hasMeta, boolean hasBody)
        throws Exception {
    InputStream in = mbox.getRESTResource(relativePath);
    TarInputStream tarIn = new TarInputStream(new GZIPInputStream(in), "UTF-8");
    TarEntry entry = null;/*from   w ww.  j  a v  a2 s  .  c  o m*/
    boolean foundMeta = false;
    boolean foundMessage = false;
    while ((entry = tarIn.getNextEntry()) != null) {
        if (entry.getName().endsWith(".meta")) {
            assertTrue("Fround " + entry.getName(), hasMeta);
            foundMeta = true;
        }
        if (entry.getName().endsWith(".eml")) {
            byte[] content = new byte[(int) entry.getSize()];
            assertEquals(content.length, tarIn.read(content));
            MimeMessage message = new ZMimeMessage(JMSession.getSession(),
                    new SharedByteArrayInputStream(content));
            byte[] body = ByteUtil.getContent(message.getInputStream(), 0);
            if (hasBody) {
                assertTrue(entry.getName() + " has no body", body.length > 0);
            } else {
                assertEquals(entry.getName() + " has a body", 0, body.length);
            }
            foundMessage = true;
        }
    }
    tarIn.close();
    assertTrue(foundMessage);
    if (hasMeta) {
        assertTrue(foundMeta);
    }
}

From source file:ezbake.deployer.publishers.local.BaseLocalPublisher.java

protected String getConfigPath(DeploymentArtifact artifact) throws Exception {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(
            new GZIPInputStream(new ByteArrayInputStream(artifact.getArtifact())));
    File tmpDir = new File("user-conf", ArtifactHelpers.getServiceId(artifact));
    tmpDir.mkdirs();//from  ww w  . j av a 2 s . c om
    String configPath = tmpDir.getAbsolutePath();

    try {
        TarArchiveEntry entry = tarIn.getNextTarEntry();

        while (entry != null) {
            if (entry.getName().startsWith("config/")) {
                File tmpFile = new File(configPath,
                        Files.getNameWithoutExtension(entry.getName()) + ".properties");
                FileOutputStream fos = new FileOutputStream(tmpFile);
                try {
                    IOUtils.copy(tarIn, fos);
                } finally {
                    IOUtils.closeQuietly(fos);
                }
            }
            entry = tarIn.getNextTarEntry();
        }
    } finally {
        IOUtils.closeQuietly(tarIn);
    }
    return configPath;
}

From source file:de.iai.ilcd.model.common.XmlFile.java

/**
 * Decompress content of XML file after loading. The reason for doing this, see {@link #compressContent()}
 * /*www . jav  a2  s  .  co  m*/
 * @throws Exception
 *             if anything goes wrong, just in-memory IO operations, should not happen
 * @see #compressContent()
 */
@PostLoad
protected void decompressContent() throws Exception {
    if (this.compressedContent == null || this.compressedContent.length == 0) {
        this.content = null;
    } else {
        GZIPInputStream gzipIn = new GZIPInputStream(new ByteArrayInputStream(this.compressedContent));
        this.content = new String(IOUtils.toByteArray(gzipIn), "UTF-8");
    }
}

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

/**
 * /*www. j ava2  s  .c  o  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));
    }

}

From source file:de.tudarmstadt.ukp.csniper.webapp.search.xmi.XmiContextProvider.java

@Override
public ItemContext getContext(EvaluationItem aItem, int aLeftSize, int aRightSize) throws IOException {
    Timer timer = new Timer();

    File base = new File(new File(corpusService.getRepositoryPath(), aItem.getCollectionId().toUpperCase()),
            XMI);/*  w  w w . j  a  v a2 s.  co m*/
    String docId = aItem.getDocumentId();
    JCasState state = jcasThreadLocal.get();
    // FIXME sometimes cas is not being reused (because of state.documentId==null - is this only
    // available for a limited timed?)
    if ((state.documentId == null) || (state.collectionId == null)
            || !StringUtils.equals(state.documentId, docId)
            || !StringUtils.equals(state.collectionId, aItem.getCollectionId())) {
        timer.start();

        InputStream is = null;
        try {
            // No need to reset the CAS - the XmiCasDeserializer does that
            is = new GZIPInputStream(new FileInputStream(new File(base, docId + ".xmi.gz")));
            XmiCasDeserializer.deserialize(is, state.jcas.getCas());
            state.documentId = aItem.getDocumentId();
            state.collectionId = aItem.getCollectionId();
        } catch (IllegalStateException e) {
            throw new IOException(e);
        } catch (SAXException e) {
            throw new IOException(e);
        } finally {
            closeQuietly(is);
        }

        timer.stop();
        log.debug("Reading the XMI took " + timer.getTime() + "ms");
    } else {
        log.debug("Reusing CAS");
    }

    timer.reset();
    timer.start();

    // text offset based
    String text = state.jcas.getDocumentText();

    // Absolute offsets
    int windowBegin = Math.max(0, (int) aItem.getBeginOffset() - aLeftSize);
    int windowEnd = Math.min(text.length(), (int) aItem.getEndOffset() + aRightSize);

    // Relative offsets
    int unitBegin = (int) aItem.getBeginOffset() - windowBegin;
    int unitEnd = (int) aItem.getEndOffset() - windowBegin;

    StringBuilder windowText = new StringBuilder(text.substring(windowBegin, windowEnd));

    List<Token> tokens = JCasUtil.selectCovered(state.jcas, Token.class, (int) aItem.getBeginOffset(),
            (int) aItem.getEndOffset());
    int unitEndDisplacement = 0;
    int matchEndDisplacement = 0;
    int matchBeginDisplacement = 0;

    boolean anyMatchSet = false;
    int matchBeginOffset = aItem.getOriginalTextMatchBegin();
    int matchEndOffset = aItem.getOriginalTextMatchEnd();

    if (aItem.isOriginalMatchSet()) {
        matchBeginOffset = aItem.getOriginalTextMatchBegin();
        matchEndOffset = aItem.getOriginalTextMatchEnd();
        anyMatchSet = true;
    } else if (aItem.isTokenMatchSet()) {
        matchBeginOffset = tokens.get(aItem.getTokenMatchBegin()).getBegin();
        matchEndOffset = tokens.get(aItem.getTokenMatchEnd()).getEnd();
        anyMatchSet = true;
    }

    Collections.reverse(tokens);
    // default: output pos with tokens
    if (outputPos) {
        for (Token t : tokens) {
            if (t.getPos() != null && t.getPos().getPosValue() != null) {
                String postfix = "/" + t.getPos().getPosValue();
                windowText.insert(t.getEnd() - windowBegin, postfix);
                unitEndDisplacement += postfix.length();
                if (anyMatchSet) {
                    if ((t.getEnd() <= matchEndOffset) && (t.getBegin() >= matchBeginOffset)) {
                        matchEndDisplacement += postfix.length();
                    }
                    if (t.getEnd() <= matchBeginOffset) {
                        matchBeginDisplacement += postfix.length();
                    }
                }
            }
        }
    }

    ItemContext ctx = new ItemContext(windowText.toString(), windowBegin, windowEnd, unitBegin,
            unitEnd + unitEndDisplacement);

    if (anyMatchSet) {
        ctx.setMatch(matchBeginOffset - windowBegin + matchBeginDisplacement,
                matchEndOffset - windowBegin + matchBeginDisplacement + matchEndDisplacement);
    }

    ctx.setTextLength(text.length());

    timer.stop();
    log.debug("Extracting the context took " + timer.getTime() + "ms");

    return ctx;
}

From source file:it.uniroma2.sag.kelp.wordspace.Wordspace.java

/**
 * Loads the word-vector pairs stored in the file whose path is <code>filename</code>
 * The file can be a plain text file or a .gz archive.
 * <p> /*from w ww . j ava  2 s. c  o  m*/
 * The expected format is: </br>
 * number_of_vectors space_dimensionality</br> 
 * word_i [TAB] 1.0 [TAB] 0 [TAB] vector values comma separated
 * </code> </br></br> Example: </br></br> <code>
 * 3 5</br>
 * dog::n [TAB] 1.0 [TAB] 0 [TAB] 2.1,4.1,1.4,2.3,0.9</br>
 * cat::n [TAB] 1.0 [TAB] 0 [TAB] 3.2,4.3,1.2,2.2,0.8</br>
 * mouse::n [TAB] 1.0 [TAB] 0 [TAB] 2.4,4.4,2.4,1.3,0.92</br>
 *   
 * 
 * @param filename the path of the file containing the word-vector pairs
 * @throws IOException
 */
private void populate(String filename) throws IOException {
    BufferedReader br = null;
    GZIPInputStream gzis = null;
    if (filename.endsWith(".gz")) {
        gzis = new GZIPInputStream(new FileInputStream(filename));
        InputStreamReader reader = new InputStreamReader(gzis, "UTF8");
        br = new BufferedReader(reader);
    } else {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF8"));
    }
    String line;
    ArrayList<String> split;
    String label;
    String[] vSplit;

    Pattern iPattern = Pattern.compile(",");
    float[] v = null;

    while ((line = br.readLine()) != null) {
        if (!line.contains("\t"))
            continue;
        float norm2 = 0;
        split = mySplit(line);
        label = split.get(0);
        vSplit = iPattern.split(split.get(3), 0);
        if (v == null)
            v = new float[vSplit.length];
        for (int i = 0; i < v.length; i++) {
            v[i] = Float.parseFloat(vSplit[i]);
            norm2 += v[i] * v[i];
        }
        float norm = (float) Math.sqrt(norm2);
        for (int i = 0; i < v.length; i++) {
            v[i] /= norm;
        }

        DenseMatrix64F featureVector = new DenseMatrix64F(1, v.length);
        for (int i = 0; i < v.length; i++) {
            featureVector.set(0, i, (double) v[i]);
        }

        DenseVector denseFeatureVector = new DenseVector(featureVector);

        addWordVector(label, denseFeatureVector);

    }
    if (filename.endsWith(".gz")) {
        gzis.close();
    }
    br.close();

}

From source file:com.jaspersoft.jasperserver.core.util.StreamUtil.java

public static Object uncompressObject(byte[] compressed) throws Exception {
    ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
    GZIPInputStream gzis = new GZIPInputStream(bais);
    ObjectInputStream ois = new ObjectInputStream(gzis);
    return ois.readObject();
}

From source file:co.cask.cdap.internal.app.runtime.LocalizationUtils.java

private static void untargz(File tarGzFile, File targetDir) throws IOException {
    try (TarArchiveInputStream tis = new TarArchiveInputStream(
            new GZIPInputStream(new FileInputStream(tarGzFile)))) {
        extractTar(tis, targetDir);/*from  w w w . ja v a  2s . com*/
    }
}

From source file:edu.gslis.ts.DumpThriftData.java

/**
 * @param thriftFile/* w  w  w.j a  va 2  s  .c  o m*/
 */
public void filter(File infile, String parser, GQuery gquery, CollectionStats bgstats) {
    try {
        InputStream in = null;

        if (infile.getName().endsWith(".gz"))
            in = new GZIPInputStream(new FileInputStream(infile));
        else if (infile.getName().endsWith("xz"))
            in = new XZInputStream(new FileInputStream(infile));
        else {
            System.err.println("Regular FileInputStream");
            in = new FileInputStream(infile);
        }

        TTransport inTransport = new TIOStreamTransport(new BufferedInputStream(in));
        TBinaryProtocol inProtocol = new TBinaryProtocol(inTransport);
        inTransport.open();

        try {
            // Run through items in the thrift file
            while (true) {
                final StreamItem item = new StreamItem();
                item.read(inProtocol);
                if (item.body == null || item.body.clean_visible == null) {
                    System.err.println("Body is null.");
                    continue;
                }

                String streamId = "";
                if (item.stream_id != null) {
                    streamId = item.stream_id;
                }

                String dateTime = "";
                long epochTime = 0;
                if (item.stream_time != null && item.stream_time.zulu_timestamp != null) {
                    dateTime = item.stream_time.zulu_timestamp;
                    DateTimeFormatter dtf = ISODateTimeFormat.dateTime();
                    epochTime = dtf.parseMillis(dateTime);
                }

                String source = "";
                if (item.source != null) {
                    source = item.source;
                }

                Map<String, List<Sentence>> parsers = item.body.sentences;
                List<Sentence> sentenceParser = parsers.get(parser);

                QueryDocScorer scorer = new ScorerDirichlet();
                scorer.setCollectionStats(bgstats);
                scorer.setQuery(gquery);

                List<Double> sentenceScores = new ArrayList<Double>();
                List<String> sentences = new ArrayList<String>();
                String sentencesText = "";
                if (sentenceParser != null && sentenceParser.size() > 0) {

                    for (Sentence s : sentenceParser) {
                        try {
                            List<Token> tokens = s.tokens;
                            String sentence = "";
                            for (Token token : tokens) {
                                String tok = token.token;
                                sentence += tok + " ";
                            }
                            FeatureVector sentenceVector = new FeatureVector(sentence, null);
                            SearchHit sentenceHit = new SearchHit();
                            sentenceHit.setFeatureVector(sentenceVector);
                            sentenceHit.setLength(sentenceVector.getLength());
                            double score = scorer.score(sentenceHit);

                            sentenceScores.add(score);
                            sentences.add(sentence);

                            sentencesText += sentence + "\n";
                        } catch (Exception e) {
                            System.err
                                    .println("Issue with sentence " + sentences.size() + " in doc " + streamId);
                            System.err.println("File: " + infile.getAbsolutePath());
                        }
                    }
                    SearchHit docHit = new SearchHit();
                    docHit.setFeatureVector(new FeatureVector(sentencesText, null));
                    double docscore = scorer.score(docHit);
                    for (int i = 0; i < sentenceScores.size(); i++) {
                        System.out.println(infile.getAbsolutePath() + "\t" + source + "\t" + epochTime + "\t"
                                + streamId + "\t" + docscore + "\t" + i + "\t" + sentenceScores.get(i) + "\t"
                                + sentences.get(i));
                    }
                } else if (sentenceParser == null) {
                    System.err.println("Sentence parser null");
                } else if (sentenceParser.size() == 0) {
                    System.err.println("Sentence length 0");
                } else {
                    System.err.println("Other sentence error.");
                }

            }
        } catch (TTransportException te) {
            if (te.getType() == TTransportException.END_OF_FILE) {
            } else {
                throw te;
            }
        }

        inTransport.close();

    } catch (Exception e) {
        System.err.println("Error processing " + infile.getAbsolutePath() + " " + infile.getName());
        e.printStackTrace();
    }
}