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:com.graphhopper.reader.osm.OSMInputFile.java

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

    InputStream ips = null;/*from  ww  w . j  av a2 s . c  om*/
    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];
    if (ips.read(header) < 0)
        throw new IllegalArgumentException("Input file is not of valid type " + file.getPath());

    /*     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:at.illecker.sentistorm.commons.util.io.IOUtils.java

public static InputStream getInputStream(File file) {
    InputStream in = null;/*  w  w  w.  j a  v a2s  .c  o  m*/
    try {
        in = new FileInputStream(file);

        // unzip if necessary
        if (file.getName().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.petpet.c3po.gatherer.FileExtractor.java

/**
 * Obtains an apache compress {@link ArchiveInputStream} to the given archive
 * file.//from   w  w  w.ja  v  a  2s  . co  m
 * 
 * @param src
 *          the archive file.
 * @return the stream.
 */
private static ArchiveInputStream getStream(String src) {
    FileInputStream fis = null;
    ArchiveInputStream is = null;

    try {

        fis = new FileInputStream(src);

        if (src.endsWith(".zip")) {

            is = new ZipArchiveInputStream(fis);

        } else {

            boolean zip = src.endsWith(".tgz") || src.endsWith(".gz");
            InputStream imp = (zip) ? new GZIPInputStream(fis, BUFFER_SIZE)
                    : new BufferedInputStream(fis, BUFFER_SIZE);
            is = new TarArchiveInputStream(imp, BUFFER_SIZE);

        }

    } catch (IOException e) {
        LOG.warn("An error occurred while obtaining the stream to the archive '{}'. Error: {}", src,
                e.getMessage());
    }
    return is;
}

From source file:com.ifs.megaprofiler.helper.FileExtractor.java

/**
 * Obtains an apache compress {@link ArchiveInputStream} to the given
 * archive file./*from   w w w .  ja  v  a 2 s.  co m*/
 * 
 * @param src
 *            the archive file.
 * @return the stream.
 */
private static ArchiveInputStream getStream(String src) {
    FileInputStream fis = null;
    ArchiveInputStream is = null;

    try {

        fis = new FileInputStream(src);

        if (src.endsWith(".zip")) {

            is = new ZipArchiveInputStream(fis);

        } else {

            boolean zip = src.endsWith(".tgz") || src.endsWith(".gz");
            InputStream imp = (zip) ? new GZIPInputStream(fis, BUFFER_SIZE)
                    : new BufferedInputStream(fis, BUFFER_SIZE);
            is = new TarArchiveInputStream(imp, BUFFER_SIZE);

        }

    } catch (IOException e) {
        // LOG.warn(
        // "An error occurred while obtaining the stream to the archive '{}'. Error: {}",
        // src, e.getMessage() );
    }
    return is;
}

From source file:de.unisb.cs.st.javaslicer.traceResult.TraceResult.java

public TraceResult(File filename) throws IOException {
    final MultiplexedFileReader file = new MultiplexedFileReader(filename);
    if (file.getStreamIds().size() < 2)
        throw new IOException("corrupted data");
    final MultiplexInputStream readClassesStream = file.getInputStream(0);
    if (readClassesStream == null)
        throw new IOException("corrupted data");
    PushbackInputStream pushBackInput = new PushbackInputStream(
            new BufferedInputStream(new GZIPInputStream(readClassesStream, 512), 512), 1);
    final DataInputStream readClassesInputStream = new DataInputStream(pushBackInput);
    final ArrayList<ReadClass> readClasses0 = new ArrayList<ReadClass>();
    final StringCacheInput stringCache = new StringCacheInput();
    int testRead;
    while ((testRead = pushBackInput.read()) != -1) {
        pushBackInput.unread(testRead);/*  w w  w.  jav a2s.c  o  m*/
        readClasses0.add(ReadClass.readFrom(readClassesInputStream, stringCache));
    }
    readClasses0.trimToSize();
    Collections.sort(readClasses0);
    this.readClasses = readClasses0;
    this.instructions = getInstructionArray(readClasses0);

    final MultiplexInputStream threadTracersStream = file.getInputStream(1);
    if (threadTracersStream == null)
        throw new IOException("corrupted data");
    pushBackInput = new PushbackInputStream(
            new BufferedInputStream(new GZIPInputStream(threadTracersStream, 512), 512), 1);
    final DataInputStream threadTracersInputStream = new DataInputStream(pushBackInput);

    final ArrayList<ThreadTraceResult> threadTraces0 = new ArrayList<ThreadTraceResult>();
    while ((testRead = pushBackInput.read()) != -1) {
        pushBackInput.unread(testRead);
        threadTraces0.add(ThreadTraceResult.readFrom(threadTracersInputStream, this, file));
    }
    threadTraces0.trimToSize();
    Collections.sort(threadTraces0);
    this.threadTraces = threadTraces0;
}