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:net.sf.ehcache.distribution.PayloadUtil.java

/**
 * The fastest Ungzip implementation. See PageInfoTest in ehcache-constructs.
 * A high performance implementation, although not as fast as gunzip3.
 * gunzips 100000 of ungzipped content in 9ms on the reference machine.
 * It does not use a fixed size buffer and is therefore suitable for arbitrary
 * length arrays.//from   w w  w  .ja  v  a2  s.c  o  m
 *
 * @param gzipped
 * @return a plain, uncompressed byte[]
 */
public static byte[] ungzip(final byte[] gzipped) {
    byte[] ungzipped = new byte[0];
    try {
        final GZIPInputStream inputStream = new GZIPInputStream(new ByteArrayInputStream(gzipped));
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(gzipped.length);
        final byte[] buffer = new byte[PayloadUtil.MTU];
        int bytesRead = 0;
        while (bytesRead != -1) {
            bytesRead = inputStream.read(buffer, 0, PayloadUtil.MTU);
            if (bytesRead != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }
        }
        ungzipped = byteArrayOutputStream.toByteArray();
        inputStream.close();
        byteArrayOutputStream.close();
    } catch (IOException e) {
        LOG.fatal("Could not ungzip. Heartbeat will not be working. " + e.getMessage());
    }
    return ungzipped;
}

From source file:com.openshift.client.utils.TarFileTestUtils.java

/**
 * Returns all paths within the given archive.
 * //from w w  w  . j a v  a 2 s.c  o  m
 * @param in
 *            the archive
 * @return all paths
 * @throws IOException
 * @throws CompressorException
 */
public static List<String> getAllPaths(InputStream in) throws IOException {
    Assert.notNull(in);

    List<String> paths = new ArrayList<String>();
    TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in));
    try {
        for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) {
            paths.add(nextEntry.getName());
        }
        return paths;
    } finally {
        StreamUtils.close(archiveIn);
    }
}

From source file:gov.nih.nci.iso21090.Ed.java

/**
 * Helper method that examines the data and compression fields together, and returns
 * clients the uncompressed data as a stream.  The only compressions supported now
 * are://  w w  w  . j av  a  2s  .  c o m
 * <ul>
 * <li>no compression
 * <li><code>GZ</code>
 * </ul>
 * Null data fields are returned as an InputSteam with nothing to read.
 * @return uncompressed data
 * @throws IOException if gzip stream is corrupted
 */
public InputStream getDataUncompressed() throws IOException {
    byte[] myBytes = getData() == null ? new byte[] {} : getData();
    if (getCompression() == null) {
        return new ByteArrayInputStream(myBytes);
    }
    if (getCompression() == Compression.GZ) {
        return new GZIPInputStream(new ByteArrayInputStream(myBytes));
    }
    throw new IllegalArgumentException("Unsupported compression: " + getCompression());
}

From source file:eu.eexcess.europeana.clickstream.EuropeanaQueryCollector.java

private void processDirectory(File dir, PrintWriter writer, QueryParser queryParser,
        Set<String> uniqueQueryCollector)
        throws IOException, FileNotFoundException, UnsupportedEncodingException {
    for (File file : dir.listFiles()) {
        if (!file.getName().endsWith(".gz")) {
            continue;
        }//  w w w .j  a  v  a  2  s . c  om
        System.err.println("Parsing file: " + file);

        GZIPInputStream inputStream = new GZIPInputStream(new FileInputStream(file));
        LineIterator iterator = new LineIterator(new InputStreamReader(inputStream, "UTF-8"));
        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            if (line.contains(QUERY_PREFIX) && !line.contains("query=DATA_PROVIDER")
                    && !line.contains("qf=DATA_PROVIDER") && !line.contains("bot") && !line.contains("slurp")
                    && !line.contains("spider")) {
                int start = line.indexOf(QUERY_PREFIX), end = line.indexOf("\"", start),
                        end2 = line.indexOf("&", start);
                if (end2 < end && end2 > 0) {
                    end = end2;
                }
                if (end < 0) {
                    end = line.length();
                }
                try {
                    String query = URLDecoder.decode(line.substring(start + QUERY_PREFIX.length(), end),
                            "UTF-8");
                    if (!query.contains(":")) {
                        Query parsedQuery = queryParser.parse(query);
                        if (parsedQuery instanceof BooleanQuery) {
                            List<BooleanClause> clauses = ((BooleanQuery) parsedQuery).clauses();
                            if (clauses != null) {
                                List<String> queryTerms = new ArrayList<String>();
                                boolean onlyTermQueries = true;
                                for (BooleanClause clause : clauses) {
                                    if (!(clause.getQuery() instanceof TermQuery)) {
                                        // there is at lease a single non term query
                                        onlyTermQueries = false;
                                        break;
                                    } else {
                                        TermQuery termQuery = (TermQuery) clause.getQuery();
                                        if (termQuery.getTerm().field().equals(DEFAULT_FIELD)) {
                                            queryTerms.add(termQuery.getTerm().text());
                                        }
                                    }
                                }

                                if (onlyTermQueries && queryTerms.size() == 2) {
                                    StringBuilder builder = new StringBuilder();
                                    for (String e : new TreeSet<String>(queryTerms)) {
                                        if (builder.length() > 0) {
                                            builder.append('\t');
                                        }
                                        builder.append(e);
                                    }
                                    // queryTerms.stream().map( (a, b) -> {b.append(a)});
                                    String normalisedQuery = builder.toString();
                                    if (uniqueQueryCollector.add(normalisedQuery)) {
                                        StringBuilder b = new StringBuilder();
                                        for (String e : queryTerms) {
                                            if (b.length() > 0) {
                                                b.append('\t');
                                            }
                                            b.append(e);
                                        }
                                        String queryInNaturalSequence = b.toString();
                                        writer.println(queryInNaturalSequence);
                                        System.out.println(queryInNaturalSequence);
                                    }
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        iterator.close();
        inputStream.close();
    }
}

From source file:edu.cornell.med.icb.iterators.TextFileLineIterator.java

/**
 * Create the object using a file./*  w ww. ja v  a  2 s  .c  o  m*/
 * @param fileToRead the filename to read
 * @throws IOException error opening the file to read
 */
public TextFileLineIterator(final File fileToRead) throws IOException {
    this(fileToRead.toString().toLowerCase().endsWith(".gz")
            ? new GZIPInputStream(new FileInputStream(fileToRead))
            : new FileInputStream(fileToRead));
}

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

protected String getArtifactPath(DeploymentArtifact artifact) throws Exception {
    TarArchiveInputStream tarIn = new TarArchiveInputStream(
            new GZIPInputStream(new ByteArrayInputStream(artifact.getArtifact())));
    String jarPath = null;/*from ww  w  . j  a v  a  2 s .  c  o m*/
    try {
        TarArchiveEntry entry = tarIn.getNextTarEntry();

        while (entry != null) {
            if (entry.getName().startsWith("bin/") || entry.getName().endsWith(".war")) {
                boolean isWar = entry.getName().endsWith(".war");
                File tmpFile = File.createTempFile(Files.getNameWithoutExtension(entry.getName()),
                        isWar ? ".war" : ".jar");
                FileOutputStream fos = new FileOutputStream(tmpFile);
                try {
                    IOUtils.copy(tarIn, fos);
                } finally {
                    IOUtils.closeQuietly(fos);
                }
                jarPath = tmpFile.getAbsolutePath();
                break;
            }
            entry = tarIn.getNextTarEntry();
        }
    } finally {
        IOUtils.closeQuietly(tarIn);
    }
    return jarPath;
}

From source file:io.github.azige.whitespace.Cli.java

static void execute(InputStream input, String path) throws IOException, ClassNotFoundException {
    WhitespaceVM vm = new DefaultWhitespaceVM();
    Program program;//from w w  w.  j a  va2 s  .  c  o m
    if (path.endsWith(WHITESPACE_BINARY_FILE_SUFFIX)) {
        ObjectInputStream oinput = new ObjectInputStream(new GZIPInputStream(input));
        program = (Program) oinput.readObject();
    } else {
        Interpreter interpreter = createInterpreter();
        program = interpreter.interpret(createReader(input));
    }
    vm.getProcessor().loadProgram(program);
    vm.getProcessor().executeAll(true);
}

From source file:com.metamx.druid.realtime.firehose.WikipediaIrcDecoder.java

@JsonCreator
public WikipediaIrcDecoder(@JsonProperty("namespaces") Map<String, Map<String, String>> namespaces,
        @JsonProperty("geoIpDatabase") String geoIpDatabase) {
    if (namespaces == null) {
        namespaces = Maps.newHashMap();//www.j  av a 2  s .  c  o  m
    }
    this.namespaces = namespaces;

    File geoDb;
    if (geoIpDatabase != null) {
        geoDb = new File(geoIpDatabase);
    } else {
        try {
            String tmpDir = System.getProperty("java.io.tmpdir");
            geoDb = new File(tmpDir, this.getClass().getCanonicalName() + ".GeoLite2-City.mmdb");
            if (!geoDb.exists()) {
                log.info("Downloading geo ip database to [%s]", geoDb);

                FileUtils.copyInputStreamToFile(new GZIPInputStream(
                        new URL("http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz")
                                .openStream()),
                        geoDb);
            }
        } catch (IOException e) {
            throw new RuntimeException("Unable to download geo ip database [%s]", e);
        }
    }
    try {
        geoLookup = new DatabaseReader(geoDb);
    } catch (IOException e) {
        throw new RuntimeException("Unable to open geo ip lookup database", e);
    }
}

From source file:gzipper.algorithms.Gzip.java

@Override
protected void extract(String path, String name) throws IOException {
    try (TarArchiveInputStream tis = new TarArchiveInputStream(
            new GZIPInputStream(new BufferedInputStream(new FileInputStream(path + name))))) {

        ArchiveEntry entry = tis.getNextEntry();

        /*create main folder of gzip archive*/
        File folder = new File(Settings._outputPath + name.substring(0, 7));
        if (!folder.exists()) {
            folder.mkdir();//from  ww  w .j  a v a  2s .  c  o m
        }
        while (entry != null & _runFlag) {
            String entryName = entry.getName();
            /*check if entry contains a directory*/
            if (entryName.contains("/")) {
                File newFile;
                if (Settings._isUnix) { //check OS for correct file path
                    newFile = new File(folder.getAbsolutePath() + "/" + entryName);
                } else {
                    newFile = new File(folder.getAbsolutePath() + "\\" + entryName);
                }
                /*mkdirs also creates parent directories*/
                if (!newFile.getParentFile().exists()) {
                    newFile.getParentFile().mkdirs();
                }
            }

            String newFilePath;

            if (Settings._isUnix) { //check OS for correct file path
                newFilePath = folder.getAbsolutePath() + "/" + entryName;
            } else {
                newFilePath = folder.getAbsolutePath() + "\\" + entryName;
            }
            /*create new OutputStream and write bytes to file*/
            try (BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream(newFilePath))) {
                byte[] buffer = new byte[4096];
                int readBytes;
                while ((readBytes = tis.read(buffer)) != -1) {
                    buf.write(buffer, 0, readBytes);
                }
            }
            entry = tis.getNextEntry();
        }
    }
}

From source file:gov.nih.nci.caarray.services.external.v1_0.data.JavaDataApiUtils.java

/**
 * Read data fully from the given RemoteInputStream and write it to the given OutputStream. The RemoteInputStream is
 * always closed at the end of this method, regardless of whether an exception occurs.
 * /*from   ww w. ja v  a2  s .c o  m*/
 * @param ris the RemoteInputStream to read from
 * @param ostream the OutputStream to write to
 * @param decompress if true, then the data is expected to be compressed with GZip and will be decompressed before
 *            being written to the OutputStream
 * @throws IOException if there is an error reading from the RemoteInputStream or writing to the OutputStream
 */
public static void readFully(RemoteInputStream ris, OutputStream ostream, boolean decompress)
        throws IOException {
    InputStream istream = null;
    try {
        istream = RemoteInputStreamClient.wrap(ris);
        if (decompress) {
            istream = new GZIPInputStream(istream);
        }
        IOUtils.copy(istream, ostream);
    } finally {
        if (istream != null) {
            istream.close();
        }
    }
}