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.roche.sequencing.bioinformatics.common.utils.Md5CheckSumUtil.java

public static String md5SumWithSkippingCommentLines(IInputStreamFactory inputStreamFactory)
        throws FileNotFoundException, IOException {
    InputStream inputStream;/*from  w  ww  . ja  v  a  2s  .c  o  m*/
    if (GZipUtil.isCompressed(inputStreamFactory)) {
        inputStream = new GZIPInputStream(inputStreamFactory.createInputStream());
    } else {
        inputStream = new BufferedInputStream(inputStreamFactory.createInputStream());
    }

    MessageDigest md5SumDigest = null;
    try {
        md5SumDigest = MessageDigest.getInstance("MD5");
    } catch (NoSuchAlgorithmException e) {
        throw new AssertionError();
    }

    try {
        byte[] character = new byte[1024];
        StringBuilder currentLine = new StringBuilder();
        int readChars = 0;
        while ((readChars = inputStream.read(character)) != -1) {
            for (int i = 0; i < readChars; ++i) {
                char currentCharacter = (char) character[i];
                if (currentCharacter == StringUtil.NEWLINE_SYMBOL) {
                    currentLine.append(currentCharacter);
                    String line = currentLine.toString();
                    if (!line.startsWith("#")) {
                        md5SumDigest.update(line.getBytes());
                    }

                    currentLine = new StringBuilder();

                } else {
                    currentLine.append(currentCharacter);
                }
            }
        }
    } finally {
        inputStream.close();
    }
    String md5Sum = Hex.encodeHexString(md5SumDigest.digest());
    return md5Sum;
}

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

/**
 * Replaces the given file(-name), that might exist anywhere nested in the
 * given archive, by a new entry with the given content. The replacement is
 * faked by adding a new entry into the archive which will overwrite the
 * existing (older one) on extraction./* w w  w . j a  va 2s . c  om*/
 * 
 * @param name
 *            the name of the file to replace (no path required)
 * @param newContent
 *            the content of the replacement file
 * @param in
 * @return
 * @throws IOException
 * @throws ArchiveException
 * @throws CompressorException
 */
public static File fakeReplaceFile(String name, String newContent, InputStream in) throws IOException {
    Assert.notNull(name);
    Assert.notNull(in);

    File newArchive = FileUtils.createRandomTempFile(".tar.gz");
    newArchive.deleteOnExit();

    TarArchiveOutputStream newArchiveOut = new TarArchiveOutputStream(
            new GZIPOutputStream(new FileOutputStream(newArchive)));
    newArchiveOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

    TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in));
    String pathToReplace = null;
    try {
        // copy the existing entries
        for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) {
            if (nextEntry.getName().endsWith(name)) {
                pathToReplace = nextEntry.getName();
            }
            newArchiveOut.putArchiveEntry(nextEntry);
            IOUtils.copy(archiveIn, newArchiveOut);
            newArchiveOut.closeArchiveEntry();
        }

        if (pathToReplace == null) {
            throw new IllegalStateException("Could not find file " + name + " in the given archive.");
        }
        TarArchiveEntry newEntry = new TarArchiveEntry(pathToReplace);
        newEntry.setSize(newContent.length());
        newArchiveOut.putArchiveEntry(newEntry);
        IOUtils.copy(new ByteArrayInputStream(newContent.getBytes()), newArchiveOut);
        newArchiveOut.closeArchiveEntry();

        return newArchive;
    } finally {
        newArchiveOut.finish();
        newArchiveOut.flush();
        StreamUtils.close(archiveIn);
        StreamUtils.close(newArchiveOut);
    }
}

From source file:twitter4j.internal.http.ApacheHttpResponseImpl.java

ApacheHttpResponseImpl(org.apache.http.HttpResponse con, HttpClientConfiguration conf) throws IOException {
    super(conf);/*from  w  w w  . j  a  v a 2s  . c  o m*/
    this.con = con;
    this.statusCode = con.getStatusLine().getStatusCode();
    // TODO: find out getErrorStream equivalent in HttpClient
    //if (null == (is = con.getErrorStream())) {
    //    is = con.getInputStream();
    //}
    is = con.getEntity().getContent();
    if (is != null && "gzip".equals(con.getEntity().getContentEncoding().getValue())) {
        // the response is gzipped
        is = new GZIPInputStream(is);
    }
}

From source file:gobblin.source.extractor.filebased.GZIPFileDownloader.java

@SuppressWarnings("unchecked")
public Iterator<D> downloadFile(String file) throws IOException {

    log.info("Beginning to download gzip compressed file: " + file);

    try {//from  w  ww  . j a va 2s  .co m
        InputStream inputStream = this.fileBasedExtractor.getCloser()
                .register(this.fileBasedExtractor.getFsHelper().getFileStream(file));
        Iterator<D> fileItr = (Iterator<D>) IOUtils.lineIterator(new GZIPInputStream(inputStream),
                ConfigurationKeys.DEFAULT_CHARSET_ENCODING);
        if (this.fileBasedExtractor.isShouldSkipFirstRecord() && fileItr.hasNext()) {
            fileItr.next();
        }
        return fileItr;
    } catch (FileBasedHelperException e) {
        throw new IOException("Exception while downloading file " + file + " with message " + e.getMessage(),
                e);
    }
}

From source file:com.pinterest.deployservice.common.TarUtils.java

/**
 * Unbundle the given tar bar as a map, with key as file name and value as content.
 *///from   w  w w . ja  v  a2 s  .co  m
public static Map<String, String> untar(InputStream is) throws Exception {
    TarArchiveInputStream tais = new TarArchiveInputStream(new GZIPInputStream(is));
    Map<String, String> data = new HashMap<String, String>();
    TarArchiveEntry entry;
    while ((entry = tais.getNextTarEntry()) != null) {
        String name = entry.getName();
        byte[] content = new byte[(int) entry.getSize()];
        tais.read(content, 0, content.length);
        data.put(name, new String(content, "UTF8"));
    }
    tais.close();
    return data;
}

From source file:com.nebhale.cyclinglibrary.web.GzipFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {

    HttpServletRequest newRequest;//w  w w .ja  va2  s .  c o m
    if (sendsGzipEncoding(request)) {
        newRequest = new DelegatingHttpServletRequest(request, new GZIPInputStream(request.getInputStream()));
    } else {
        String method = request.getMethod();
        if ("POST".equals(method) || "PUT".equals(method)) {
            this.logger.warn("Uncompressed input received for '{} {}'", method, request.getRequestURI());
        }

        newRequest = request;
    }

    HttpServletResponse newResponse;
    OutputStream outputStream;
    if (acceptsGzipEncoding(request)) {
        response.setHeader("Content-Encoding", "gzip");
        outputStream = new GZIPOutputStream(response.getOutputStream());
        newResponse = new DelegatingHttpServletResponse(response, outputStream);
    } else {
        String method = request.getMethod();
        if (!"DELETE".equals(method)) {
            this.logger.warn("Uncompressed output requested for '{} {}'", method, request.getRequestURI());
        }

        outputStream = response.getOutputStream();
        newResponse = response;
    }

    filterChain.doFilter(newRequest, newResponse);
}

From source file:de.tudarmstadt.ukp.csniper.resbuild.BinaryCasReader.java

@Override
public void getNext(CAS aCAS) throws IOException, CollectionException {
    w.resume();//from w ww  .j a v  a  2  s  .c om

    Resource res = nextFile();
    InputStream is = null;
    try {
        is = res.getInputStream();
        if (res.getResource().getFilename().endsWith(".gz")) {
            is = new GZIPInputStream(is);
        }
        CASCompleteSerializer serializer = (CASCompleteSerializer) new ObjectInputStream(
                new BufferedInputStream(is)).readObject();
        ((CASImpl) aCAS).reinit(serializer);
    } catch (ClassNotFoundException e) {
        throw new IOException(e);
    } finally {
        closeQuietly(is);
    }

    w.suspend();
}

From source file:com.adaptris.core.services.GunzipService.java

/**
 * @see com.adaptris.core.Service#doService(AdaptrisMessage)
 *///from   w  ww .ja  v a 2  s.co  m
@Override
public void doService(AdaptrisMessage msg) throws ServiceException {
    GZIPInputStream in = null;
    OutputStream out = null;

    try {
        out = msg.getOutputStream();
        in = new GZIPInputStream(msg.getInputStream());
        StreamUtil.copyStream(in, out);
    } catch (Exception e) {
        throw new ServiceException(e);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }
}

From source file:com.netflix.dyno.connectionpool.impl.utils.ZipUtils.java

/**
 * Decompresses the given byte array without transforming it into a String
 *
 * @param compressed byte array input/*from  ww w. ja  v a 2 s . com*/
 * @return decompressed data in a byte array
 * @throws IOException
 */
public static byte[] decompressBytesNonBase64(byte[] compressed) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(compressed);
    InputStream gis = new GZIPInputStream(is);
    return IOUtils.toByteArray(gis);
}

From source file:com.hs.mail.util.FileUtils.java

public static void uncompress(File srcFile, File destFile) throws IOException {
    InputStream input = null;/*from  ww  w .  j  av  a  2  s.  c  o m*/
    OutputStream output = null;
    try {
        input = new GZIPInputStream(new FileInputStream(srcFile));
        output = new BufferedOutputStream(new FileOutputStream(destFile));
        IOUtils.copyLarge(input, output);
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(input);
    }
}