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:edu.sdsc.scigraph.services.jersey.dynamic.SwaggerFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    boolean gzip = isGzip(request);

    // Capture the output of the filter chain
    ByteArrayResponseWrapper wrappedResp = new ByteArrayResponseWrapper((HttpServletResponse) response);
    chain.doFilter(request, wrappedResp);
    if (gzip) {// w ww.j a v a  2 s  .  co m
        try (InputStream is = new ByteArrayInputStream(wrappedResp.getBytes());
                GZIPInputStream gis = new GZIPInputStream(is);
                ByteArrayOutputStream bs = new ByteArrayOutputStream();
                GZIPOutputStream gzos = new GZIPOutputStream(bs)) {
            byte[] newApi = writeDynamicResource(gis);
            gzos.write(newApi);
            gzos.close();
            byte[] output = bs.toByteArray();
            response.setContentLength(output.length);
            response.getOutputStream().write(output);
        }
    } else {
        try (InputStream is = new ByteArrayInputStream(wrappedResp.getBytes());
                ByteArrayOutputStream bs = new ByteArrayOutputStream()) {
            byte[] newApi = writeDynamicResource(is);
            response.setContentLength(newApi.length);
            response.getOutputStream().write(newApi);
        }

    }
}

From source file:io.covert.binary.analysis.BuildSequenceFileFromTarball.java

public void load(FileSystem fs, Configuration conf, File inputTarball, Path outputDir) throws Exception {
    Text key = new Text();
    BytesWritable val = new BytesWritable();

    Path sequenceName = new Path(outputDir, inputTarball.getName() + ".seq");
    System.out.println("Writing to " + sequenceName);
    SequenceFile.Writer writer = SequenceFile.createWriter(fs, conf, sequenceName, Text.class,
            BytesWritable.class, CompressionType.RECORD);

    InputStream is = new FileInputStream(inputTarball);
    if (inputTarball.toString().toLowerCase().endsWith(".gz")) {
        is = new GZIPInputStream(is);
    } else if (inputTarball.toString().toLowerCase().endsWith(".bz")
            || inputTarball.toString().endsWith(".bz2")) {
        is.read(); // read 'B'
        is.read(); // read 'Z'
        is = new CBZip2InputStream(is);
    }/*from w  ww. ja va  2  s. c  o m*/

    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory()
            .createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) {
        if (!entry.isDirectory()) {

            try {
                final ByteArrayOutputStream outputFileStream = new ByteArrayOutputStream();
                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();
                byte[] outputFile = outputFileStream.toByteArray();
                val.set(outputFile, 0, outputFile.length);

                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(outputFile);
                byte[] digest = md.digest();
                String hexdigest = "";
                for (int i = 0; i < digest.length; i++) {
                    hexdigest += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
                }
                key.set(hexdigest);
                writer.append(key, val);
            } catch (IOException e) {
                System.err.println("Warning: tarball may be truncated: " + inputTarball);
                // Truncated Tarball
                break;
            }
        }
    }
    debInputStream.close();
    writer.close();
}

From source file:com.playonlinux.core.utils.archive.Tar.java

List<File> uncompressTarGzFile(File inputFile, File outputDir, Consumer<ProgressEntity> stateCallback) {
    try (CountingInputStream countingInputStream = new CountingInputStream(new FileInputStream(inputFile));
            InputStream inputStream = new GZIPInputStream(countingInputStream)) {
        final long finalSize = FileUtils.sizeOf(inputFile);
        return uncompress(inputStream, countingInputStream, outputDir, finalSize, stateCallback);
    } catch (IOException e) {
        throw new ArchiveException(TAR_ERROR_MESSAGE, e);
    }//from  w ww. ja  v  a  2 s  .co  m
}

From source file:com.nesscomputing.tinyhttp.HttpContentResponseHandler.java

/**
 * Processes the client response./*from  w w  w  . j  a  v  a  2 s .c o  m*/
 */
public T handle(final HttpRequest request, final HttpResponse response) throws IOException {
    // Find the response stream - the error stream may be valid in cases
    // where the input stream is not.
    InputStream is = null;
    try {
        final HttpEntity httpEntity = response.getEntity();
        if (httpEntity != null) {
            is = httpEntity.getContent();
        }
    } catch (IOException e) {
        log.warn("Could not locate response body stream", e);
        // normal for 401, 403 and 404 responses, for example...
    }

    if (is == null) {
        // Fall back to zero length response.
        is = new NullInputStream(0);
    }

    final Header header = response.getFirstHeader("Content-Encoding");
    if (header != null) {
        final String encoding = StringUtils.trimToEmpty(header.getValue());

        if (StringUtils.equalsIgnoreCase(encoding, "gzip")
                || StringUtils.equalsIgnoreCase(encoding, "x-gzip")) {
            log.debug("Found GZIP stream");
            is = new GZIPInputStream(is);
        } else if (StringUtils.equalsIgnoreCase(encoding, "deflate")) {
            log.debug("Found deflate stream");
            final Inflater inflater = new Inflater(true);
            is = new InflaterInputStream(is, inflater);
        }
    }
    return contentConverter.convert(request, response, is);
}

From source file:me.vertretungsplan.parser.DSBMobileParser.java

private static String decode(String input) throws IOException {
    byte[] inputBytes = Base64.decodeBase64(input);
    InputStream is = new GZIPInputStream(new ByteArrayInputStream(inputBytes));
    return IOUtils.toString(is, ENCODING);
}

From source file:com.softinstigate.restheart.integrationtest.ContentEncodingIT.java

@Test
public void testGzipAcceptEncoding() throws Exception {
    Response resp = notDecompressingExecutor
            .execute(Request.Get(rootUri).addHeader(Headers.ACCEPT_ENCODING_STRING, Headers.GZIP.toString()));

    HttpResponse httpResp = resp.returnResponse();
    assertNotNull(httpResp);//from w w  w .j  a  va2 s  . c o m
    HttpEntity entity = httpResp.getEntity();
    assertNotNull(entity);
    StatusLine statusLine = httpResp.getStatusLine();
    assertNotNull(statusLine);

    String content = EntityUtils.toString(entity);

    Header h = httpResp.getFirstHeader("Content-Encoding");

    assertNotNull("check accept encoding header not null", h);
    assertEquals("check accept encoding header value", Headers.GZIP.toString(), h.getValue());

    assertEquals("check status code", HttpStatus.SC_OK, statusLine.getStatusCode());

    try {
        GZIPInputStream gzipis = new GZIPInputStream(
                new ByteArrayInputStream(content.getBytes(StandardCharsets.ISO_8859_1)));

        while (gzipis.read() > 0) {

        }
    } catch (Exception ex) {
        fail("check decompressing content");
    }
}

From source file:com.atilika.kuromoji.benchmark.Benchmark.java

public void benchmark() throws IOException {

    BufferedReader reader;//from  w ww  .  j  av  a 2  s .c o m

    if (inputFile.getName().endsWith(".gz")) {
        reader = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(inputFile)),
                StandardCharsets.UTF_8));
    } else {
        reader = new BufferedReader(
                new InputStreamReader(new FileInputStream(inputFile), StandardCharsets.UTF_8));
    }

    Writer writer;

    if (outputFile == null) {
        writer = new NullWriter();
    } else {
        writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(outputFile), StandardCharsets.UTF_8));
    }

    Writer statisticsWriter;

    if (statisticsFile == null) {
        statisticsWriter = new NullWriter();
    } else {
        statisticsWriter = new FileWriter(statisticsFile);
    }

    tokenize(reader, writer, statisticsWriter);

    reader.close();
    writer.close();
    statisticsWriter.close();
}

From source file:com.iisigroup.cap.utils.CapSerialization.java

/**
 * decompress byte array data with GZIP.
 * /* w w w  .ja  v  a  2  s  .  c o  m*/
 * @param input
 *            the input compressed data
 * @return the decompress data
 * @throws java.io.IOException
 */
public byte[] decompress(byte[] input) throws java.io.IOException {

    byte[] buf = new byte[2048];
    byte[] result = null;
    java.io.ByteArrayInputStream bain = null;
    ByteArrayOutputStream baout = null;
    GZIPInputStream gzipin = null;
    try {
        bain = new java.io.ByteArrayInputStream(input);
        gzipin = new GZIPInputStream(bain);
        baout = new ByteArrayOutputStream();
        int size;
        while ((size = gzipin.read(buf)) != -1) {
            baout.write(buf, 0, size);
        }
        result = baout.toByteArray();
        return result;
    } finally {
        IOUtils.closeQuietly(bain);
        IOUtils.closeQuietly(baout);
        IOUtils.closeQuietly(gzipin);
    }

}

From source file:com.igormaznitsa.mvngolang.utils.UnpackUtils.java

public static int unpackFileToFolder(@Nonnull final Log logger, @Nullable final String folder,
        @Nonnull final File archiveFile, @Nonnull final File destinationFolder, final boolean makeAllExecutable)
        throws IOException {
    final String normalizedName = archiveFile.getName().toLowerCase(Locale.ENGLISH);

    final ArchEntryGetter entryGetter;

    boolean modeZipFile = false;

    final ZipFile theZipFile;
    final ArchiveInputStream archInputStream;
    if (normalizedName.endsWith(".zip")) {
        logger.debug("Detected ZIP archive");

        modeZipFile = true;//  www  .java  2s  .co m

        theZipFile = new ZipFile(archiveFile);
        archInputStream = null;
        entryGetter = new ArchEntryGetter() {
            private final Enumeration<ZipArchiveEntry> iterator = theZipFile.getEntries();

            @Override
            @Nullable
            public ArchiveEntry getNextEntry() throws IOException {
                ArchiveEntry result = null;
                if (this.iterator.hasMoreElements()) {
                    result = this.iterator.nextElement();
                }
                return result;
            }
        };
    } else {
        theZipFile = null;
        final InputStream in = new BufferedInputStream(new FileInputStream(archiveFile));
        try {
            if (normalizedName.endsWith(".tar.gz")) {
                logger.debug("Detected TAR.GZ archive");
                archInputStream = new TarArchiveInputStream(new GZIPInputStream(in));

                entryGetter = new ArchEntryGetter() {
                    @Override
                    @Nullable
                    public ArchiveEntry getNextEntry() throws IOException {
                        return ((TarArchiveInputStream) archInputStream).getNextTarEntry();
                    }
                };

            } else {
                logger.debug("Detected OTHER archive");
                archInputStream = ARCHIVE_STREAM_FACTORY.createArchiveInputStream(in);
                logger.debug("Created archive stream : " + archInputStream.getClass().getName());

                entryGetter = new ArchEntryGetter() {
                    @Override
                    @Nullable
                    public ArchiveEntry getNextEntry() throws IOException {
                        return archInputStream.getNextEntry();
                    }
                };
            }

        } catch (ArchiveException ex) {
            IOUtils.closeQuietly(in);
            throw new IOException("Can't recognize or read archive file : " + archiveFile, ex);
        } catch (CantReadArchiveEntryException ex) {
            IOUtils.closeQuietly(in);
            throw new IOException("Can't read entry from archive file : " + archiveFile, ex);
        }
    }

    try {

        final String normalizedFolder = folder == null ? null : FilenameUtils.normalize(folder, true) + '/';

        int unpackedFilesCounter = 0;
        while (true) {
            final ArchiveEntry entry = entryGetter.getNextEntry();
            if (entry == null) {
                break;
            }
            final String normalizedPath = FilenameUtils.normalize(entry.getName(), true);

            logger.debug("Detected archive entry : " + normalizedPath);

            if (normalizedFolder == null || normalizedPath.startsWith(normalizedFolder)) {
                final File targetFile = new File(destinationFolder, normalizedFolder == null ? normalizedPath
                        : normalizedPath.substring(normalizedFolder.length()));
                if (entry.isDirectory()) {
                    logger.debug("Folder : " + normalizedPath);
                    if (!targetFile.exists() && !targetFile.mkdirs()) {
                        throw new IOException("Can't create folder " + targetFile);
                    }
                } else {
                    final File parent = targetFile.getParentFile();

                    if (parent != null && !parent.isDirectory() && !parent.mkdirs()) {
                        throw new IOException("Can't create folder : " + parent);
                    }

                    final FileOutputStream fos = new FileOutputStream(targetFile);

                    try {
                        if (modeZipFile) {
                            logger.debug("Unpacking ZIP entry : " + normalizedPath);

                            final InputStream zipEntryInStream = theZipFile
                                    .getInputStream((ZipArchiveEntry) entry);
                            try {
                                if (IOUtils.copy(zipEntryInStream, fos) != entry.getSize()) {
                                    throw new IOException(
                                            "Can't unpack file, illegal unpacked length : " + entry.getName());
                                }
                            } finally {
                                IOUtils.closeQuietly(zipEntryInStream);
                            }
                        } else {
                            logger.debug("Unpacking archive entry : " + normalizedPath);

                            if (!archInputStream.canReadEntryData(entry)) {
                                throw new IOException("Can't read archive entry data : " + normalizedPath);
                            }
                            if (IOUtils.copy(archInputStream, fos) != entry.getSize()) {
                                throw new IOException(
                                        "Can't unpack file, illegal unpacked length : " + entry.getName());
                            }
                        }
                    } finally {
                        fos.close();
                    }

                    if (makeAllExecutable) {
                        try {
                            targetFile.setExecutable(true, true);
                        } catch (SecurityException ex) {
                            throw new IOException("Can't make file executable : " + targetFile, ex);
                        }
                    }
                    unpackedFilesCounter++;
                }
            } else {
                logger.debug("Archive entry " + normalizedPath + " ignored");
            }
        }
        return unpackedFilesCounter;
    } finally {
        IOUtils.closeQuietly(theZipFile);
        IOUtils.closeQuietly(archInputStream);
    }
}

From source file:edu.stanford.muse.index.NEROld.java

public static void readLocationsWG() {
    try {/*from  ww w  .  jav  a  2 s .  c  o m*/
        InputStream is = new GZIPInputStream(
                NER.class.getClassLoader().getResourceAsStream("WG.locations.txt.gz"));
        LineNumberReader lnr = new LineNumberReader(new InputStreamReader(is, "UTF-8"));
        while (true) {
            String line = lnr.readLine();
            if (line == null)
                break;
            StringTokenizer st = new StringTokenizer(line, "\t");
            if (st.countTokens() == 4) {
                String locationName = st.nextToken();
                String canonicalName = locationName.toLowerCase();
                if (locationsToSuppress.contains(canonicalName))
                    continue;
                String lat = st.nextToken();
                String longi = st.nextToken();
                String pop = st.nextToken();
                long popl = Long.parseLong(pop);
                float latf = ((float) Integer.parseInt(lat)) / 100.0f;
                float longif = ((float) Integer.parseInt(longi)) / 100.0f;
                Long existingPop = populations.get(canonicalName);
                if (existingPop == null || popl > existingPop) {
                    populations.put(canonicalName, popl);
                    locations.put(canonicalName,
                            new LocationInfo(locationName, Float.toString(latf), Float.toString(longif)));
                }
            }
        }
    } catch (Exception e) {
        log.warn("Unable to read World Gazetteer file, places info may be inaccurate");
        log.debug(Util.stackTrace(e));
    }
}