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, int size) throws IOException 

Source Link

Document

Creates a new input stream with the specified buffer size.

Usage

From source file:Main.java

static public InputStream createInputStream(File file) throws IOException {
    String filename = file.getName().toLowerCase();
    if (filename.endsWith(".gz")) {
        // a buffered output stream sped processing up by 4X! in prod
        GZIPInputStream gis = new GZIPInputStream(new FileInputStream(file), 1024 * 1024); // 1 MB buffer
        return new BufferedInputStream(gis, 1024 * 1024); // 1 MB buffer
        /**        } else if (filename.endsWith(".lzf")) {
         // a buffered output stream is not necessary
         return new LZFInputStream(new FileInputStream(file));
         *//*from  ww w  .  j a va 2 s.c o  m*/
    } else {
        return null;
    }
}

From source file:Main.java

@Nullable
public static String decompress(String input) {
    StringBuilder sb = new StringBuilder();
    ByteArrayInputStream is = null;
    GZIPInputStream gis = null;/*from w w  w  . j  av a 2 s .  c  om*/
    try {
        final byte[] bytes = Base64.decode(input, Base64.DEFAULT);
        is = new ByteArrayInputStream(bytes);
        gis = new GZIPInputStream(is, BUFFER_SIZE);

        int cache;
        final byte[] data = new byte[BUFFER_SIZE];
        while ((cache = gis.read(data)) != -1) {
            sb.append(new String(data, 0, cache));
        }
    } catch (IOException e) {
        return null;
    } finally {
        try {
            if (gis != null)
                gis.close();
            if (is != null)
                is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:com.milaboratory.util.CompressionType.java

private static InputStream createInputStream(CompressionType ct, InputStream is) throws IOException {
    switch (ct) {
    case None://w  w  w  .j a  v a  2 s . c  o m
        return is;
    case GZIP:
        return new GZIPInputStream(is, 2048);
    case BZIP2:
        CompressorStreamFactory factory = new CompressorStreamFactory();
        try {
            return factory.createCompressorInputStream(CompressorStreamFactory.BZIP2,
                    new BufferedInputStream(is));
        } catch (CompressorException e) {
            throw new IOException(e);
        }
    }
    throw new NullPointerException();
}

From source file:edu.umd.cs.hcil.twitterreplay.GzippedFileReader.java

public void open() throws FileNotFoundException, IOException {
    FileInputStream inFileStream = new FileInputStream(mFile);
    GZIPInputStream gzippedStream = new GZIPInputStream(inFileStream, 65536);
    BufferedReader reader = new BufferedReader(new InputStreamReader(gzippedStream, Charset.forName("UTF-8")));
    mReaderIterator = new LineIterator(reader);
}

From source file:com.milaboratory.core.io.CompressionType.java

private static InputStream createInputStream(CompressionType ct, InputStream is, int buffer)
        throws IOException {
    switch (ct) {
    case None://from   w  ww  .  j  ava 2s . co m
        return is;
    case GZIP:
        return new GZIPInputStream(is, buffer);
    case BZIP2:
        CompressorStreamFactory factory = new CompressorStreamFactory();
        try {
            return factory.createCompressorInputStream(CompressorStreamFactory.BZIP2,
                    new BufferedInputStream(is));
        } catch (CompressorException e) {
            throw new IOException(e);
        }
    }
    throw new NullPointerException();
}

From source file:org.zywx.wbpalmstar.engine.eservice.EServiceTest.java

private static byte[] toByteArray(HttpEntity entity) throws Exception {
    if (entity == null) {
        throw new Exception("HTTP entity may not be null");
    }/*from w  w w.  ja v  a 2s.c  o  m*/
    InputStream instream = entity.getContent();
    if (instream == null) {
        return new byte[] {};
    }
    long len = entity.getContentLength();
    if (len > Integer.MAX_VALUE) {
        throw new Exception("HTTP entity too large to be buffered in memory");
    }
    Header contentEncoding = entity.getContentEncoding();
    boolean gzip = false;
    if (null != contentEncoding) {
        if ("gzip".equalsIgnoreCase(contentEncoding.getValue())) {
            instream = new GZIPInputStream(instream, 2048);
            gzip = true;
        }
    }
    ByteArrayBuffer buffer = new ByteArrayBuffer(1024 * 8);
    // \&:38, \n:10, \r:13, \':39, \":34, \\:92
    try {
        if (gzip) {
            int lenth = 0;
            while (lenth != -1) {
                byte[] buf = new byte[2048];
                try {
                    lenth = instream.read(buf, 0, buf.length);
                    if (lenth != -1) {
                        buffer.append(buf, 0, lenth);
                    }
                } catch (EOFException e) {
                    int tl = buf.length;
                    int surpl;
                    for (int k = 0; k < tl; ++k) {
                        surpl = buf[k];
                        if (surpl != 0) {
                            buffer.append(surpl);
                        }
                    }
                    lenth = -1;
                }
            }
            int bl = buffer.length();
            ByteArrayBuffer temBuffer = new ByteArrayBuffer((int) (bl * 1.4));
            for (int j = 0; j < bl; ++j) {
                int cc = buffer.byteAt(j);
                //               if (cc == 34 || cc == 39 || cc == 92 || cc == 10
                //                     || cc == 13 || cc == 38) {
                //                  temBuffer.append('\\');
                //               }
                temBuffer.append(cc);
            }
            buffer = temBuffer;
        } else {
            int c;
            while ((c = instream.read()) != -1) {
                //               if (c == 34 || c == 39 || c == 92 || c == 10 || c == 13
                //                     || c == 38) {
                //                  buffer.append('\\');
                //               }
                buffer.append(c);
            }
        }
    } catch (Exception e) {
        instream.close();
    } finally {
        instream.close();
    }
    return buffer.toByteArray();
}

From source file:io.ecarf.core.cloud.task.processor.reason.phase2.DoReasonTask8IntTest.java

@Test
@Ignore/*from w w w  .  j  a  va  2s  . com*/
public void testCsvParser() throws FileNotFoundException, IOException {

    String filename = "/var/folders/3h/0whnrhjn1ddfb5p9pq_c6_mh0000gn/T//ecarf-evm-1_1456690870927_QueryResults_0";
    int rows = 0;

    try (BufferedReader reader = new BufferedReader(
            new InputStreamReader(new GZIPInputStream(new FileInputStream(filename), Constants.GZIP_BUF_SIZE)),
            Constants.GZIP_BUF_SIZE);) {

        Iterable<CSVRecord> records = CSVFormat.DEFAULT.withHeader().withSkipHeaderRecord().parse(reader);

        for (CSVRecord record : records) {

            ETriple instanceTriple = ETriple.fromCSV(record.values());
            rows++;
        }
    }

    assertEquals(8091263, rows);

}

From source file:at.illecker.sentistorm.commons.util.io.IOUtils.java

public static InputStream getInputStream(String fileOrUrl, boolean unzip) {
    InputStream in = null;/*w  w  w .  ja v a2  s . co  m*/
    try {
        if (fileOrUrl.matches("https?://.*")) {
            URL u = new URL(fileOrUrl);
            URLConnection uc = u.openConnection();
            in = uc.getInputStream();
        } else {
            // 1) check if file is within jar
            in = IOUtils.class.getClassLoader().getResourceAsStream(fileOrUrl);

            // windows File.separator is \, but getting resources only works with /
            if (in == null) {
                in = IOUtils.class.getClassLoader().getResourceAsStream(fileOrUrl.replaceAll("\\\\", "/"));
            }

            // 2) if not found in jar, load from the file system
            if (in == null) {
                in = new FileInputStream(fileOrUrl);
            }
        }

        // unzip if necessary
        if ((unzip) && (fileOrUrl.endsWith(".gz"))) {
            in = new GZIPInputStream(in, GZIP_FILE_BUFFER_SIZE);
        }

        // buffer input stream
        in = new BufferedInputStream(in);

    } catch (FileNotFoundException e) {
        LOG.error("FileNotFoundException: " + e.getMessage());
    } catch (IOException e) {
        LOG.error("IOException: " + e.getMessage());
    }

    return in;
}

From source file:com.graphhopper.reader.OSMInputFile.java

@SuppressWarnings("unchecked")
private InputStream decode(File file) throws IOException {
    final String name = file.getName();

    InputStream ips = null;//  w  ww  . jav  a  2s  . c o  m
    try {
        ips = new BufferedInputStream(new FileInputStream(file), 50000);
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    }
    ips.mark(10);

    // check file header
    byte header[] = new byte[6];
    ips.read(header);

    /*     can parse bz2 directly with additional lib
     if (header[0] == 'B' && header[1] == 'Z')
     {
     return new CBZip2InputStream(ips);
     }
     */
    if (header[0] == 31 && header[1] == -117) {
        ips.reset();
        return new GZIPInputStream(ips, 50000);
    } else if (header[0] == 0 && header[1] == 0 && header[2] == 0 && header[4] == 10 && header[5] == 9
            && (header[3] == 13 || header[3] == 14)) {
        ips.reset();
        binary = true;
        return ips;
    } else if (header[0] == 'P' && header[1] == 'K') {
        ips.reset();
        ZipInputStream zip = new ZipInputStream(ips);
        zip.getNextEntry();

        return zip;
    } else if (name.endsWith(".osm") || name.endsWith(".xml")) {
        ips.reset();
        return ips;
    } else if (name.endsWith(".bz2") || name.endsWith(".bzip2")) {
        String clName = "org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream";
        try {
            Class clazz = Class.forName(clName);
            ips.reset();
            Constructor<InputStream> ctor = clazz.getConstructor(InputStream.class, boolean.class);
            return ctor.newInstance(ips, true);
        } catch (Exception e) {
            throw new IllegalArgumentException("Cannot instantiate " + clName, e);
        }
    } else {
        throw new IllegalArgumentException("Input file is not of valid type " + file.getPath());
    }
}

From source file:com.kolich.common.util.io.GZIPCompressor.java

/**
 * Given a GZIP'ed compressed InputStream, uncompresses it and returns
 * the result as new byte array.  Does NOT close the InputStream; it's
 * up to the caller to close the InputStream when necessary.
 * @param toUncompress//from w  w  w .j a  v a2s  .co m
 * @return
 */
public static final byte[] uncompress(final InputStream is, final int size) {
    GZIPInputStream gzis = null;
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        gzis = new GZIPInputStream(is, size);
        IOUtils.copyLarge(gzis, baos);
        return baos.toByteArray();
    } catch (Exception e) {
        throw new GZIPCompressorException(e);
    } finally {
        IOUtils.closeQuietly(gzis);
    }
}