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:Main.java

public static void main(String[] args) throws Exception {
    String inName = "a.pack.gz";
    String outName = "a.unpacked";
    Pack200.Unpacker unpacker = Pack200.newUnpacker();
    JarOutputStream out = new JarOutputStream(new FileOutputStream(outName));
    InputStream in = new FileInputStream(inName);
    in = new GZIPInputStream(in);
    unpacker.unpack(in, out);//from  w ww  . j a v  a 2 s. c om
    out.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    String inName = "abc.pack.gz";
    String outName = "abc";

    Pack200.Unpacker unpacker = Pack200.newUnpacker();
    JarOutputStream out = new JarOutputStream(new FileOutputStream(outName));
    InputStream in = new FileInputStream(inName);
    if (inName.endsWith(".gz")) {
        in = new GZIPInputStream(in);
    }//from ww  w  .  j a v  a2s.  c o m

    unpacker.unpack(in, out);
    out.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    String inName = args[0];// w  w  w .ja v a 2  s .c  om
    String outName;
    if (inName.endsWith(".pack.gz")) {
        outName = inName.substring(0, inName.length() - 8);
    } else if (inName.endsWith(".pack")) {
        outName = inName.substring(0, inName.length() - 5);
    } else {
        outName = inName + ".unpacked";
    }

    JarOutputStream out = null;
    InputStream in = null;

    Pack200.Unpacker unpacker = Pack200.newUnpacker();
    out = new JarOutputStream(new FileOutputStream(outName));
    in = new FileInputStream(inName);
    if (inName.endsWith(".gz"))
        in = new GZIPInputStream(in);
    unpacker.unpack(in, out);
    out.close();
}

From source file:GZIPcompress.java

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        System.out.println("Usage: \nGZIPcompress file\n" + "\tUses GZIP compression to compress "
                + "the file to test.gz");
        System.exit(1);//ww w.  ja  v a2  s.co m
    }
    BufferedReader in = new BufferedReader(new FileReader(args[0]));
    BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("test.gz")));
    System.out.println("Writing file");
    int c;
    while ((c = in.read()) != -1)
        out.write(c);
    in.close();
    out.close();
    System.out.println("Reading file");
    BufferedReader in2 = new BufferedReader(
            new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz"))));
    String s;
    while ((s = in2.readLine()) != null)
        System.out.println(s);
}

From source file:CommandsFromZipFile.java

/**
 *  Sample test to retrieve command value using zip file
 * @param args/* w ww .  j a va 2s .c  o m*/
 */
public static void main(String[] args) {
    //by default it sets to Ctrl+D
    System.setProperty("jline.shutdownhook", "true");
    try {
        ConsoleReader consoleReader = new ConsoleReader();
        consoleReader.setPrompt("carbon>");
        consoleReader.addCompleter(new StringsCompleter(IOUtils.readLines(
                new GZIPInputStream(CommandsFromZipFile.class.getResourceAsStream("commandList.txt.gz")))));
        consoleReader.addCompleter(new FileNameCompleter());
        String line = "";
        String colored = "";

        while ((line = consoleReader.readLine()) != null) {
            if ("clear".equals(line.trim())) {
                System.out.print("\33[2J");
                System.out.flush();
                System.out.print("\33[1;1H");
                System.out.flush();
            } else if ("aback".equals(line.trim())) {
                colored = Ansi.ansi().fg(Ansi.Color.RED).a("Entered command : ")
                        .a(Ansi.Attribute.INTENSITY_BOLD).a(line).a(Ansi.Attribute.INTENSITY_BOLD_OFF)
                        .fg(Ansi.Color.DEFAULT).toString();
                consoleReader.println(colored);
            } else {
                consoleReader.println(line);
            }
        }
    } catch (IOException exception) {
        LOGGER.error(" Unable to load commands from the zip file ", exception);
    } finally {
        try {
            jline.TerminalFactory.get().restore();
        } catch (Exception exception) {
            LOGGER.error(" Unable to restore the terminal ", exception);
        }
    }
}

From source file:com.javacreed.examples.sql.Example2.java

public static void main(final String[] args) throws Exception {
    try (BasicDataSource dataSource = DatabaseUtils.createDataSource();
            Connection connection = dataSource.getConnection()) {
        final ExampleTest test = new ExampleTest(connection, "compressed_table", "compressed") {
            @Override//  w ww.j  a  v a  2s. c  o m
            protected String parseRow(final ResultSet resultSet) throws Exception {
                try (GZIPInputStream in = new GZIPInputStream(resultSet.getBinaryStream("compressed"))) {
                    return IOUtils.toString(in, "UTF-8");
                }
            }

            @Override
            protected void setPreparedStatement(final String data, final PreparedStatement statement)
                    throws Exception {
                // Compress the data before inserting it. We need to compress before inserting the data to make this process
                // as realistic as possible.
                final ByteArrayOutputStream baos = new ByteArrayOutputStream(data.length());
                try (OutputStream out = new GZIPOutputStream(baos, data.length())) {
                    out.write(data.getBytes("UTF-8"));
                }
                statement.setBinaryStream(1, new ByteArrayInputStream(baos.toByteArray()));
            }
        };
        test.runTest();
    }
    Example2.LOGGER.debug("Done");
}

From source file:ReadGZIP.java

public static void main(String[] argv) throws IOException {
    String FILENAME = "file.txt.gz";

    // Since there are 4 constructor calls here, I wrote them out in full.
    // In real life you would probably nest these constructor calls.
    FileInputStream fin = new FileInputStream(FILENAME);
    GZIPInputStream gzis = new GZIPInputStream(fin);
    InputStreamReader xover = new InputStreamReader(gzis);
    BufferedReader is = new BufferedReader(xover);

    String line;/*ww  w.j a va2 s  . c  o m*/
    // Now read lines of text: the BufferedReader puts them in lines,
    // the InputStreamReader does Unicode conversion, and the
    // GZipInputStream "gunzip"s the data from the FileInputStream.
    while ((line = is.readLine()) != null)
        System.out.println("Read: " + line);
}

From source file:edu.msu.cme.rdp.readseq.utils.QualityTrimmer.java

public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("f", "fastq-out", false, "Write fastq instead of fasta file");
    options.addOption("l", "less-than", false, "Trim at <= instead of strictly =");
    options.addOption("i", "illumina", false, "Illumina trimming mode");

    FastqWriter fastqOut = null;//from w ww  . j  a  va 2  s .c  o  m
    FastaWriter fastaOut = null;

    byte qualTrim = -1;

    boolean writeFasta = true;
    boolean trimle = false;
    boolean illumina = false;

    List<SeqReader> readers = new ArrayList();
    List<File> seqFiles = new ArrayList();

    try {
        CommandLine line = new PosixParser().parse(options, args);

        if (line.hasOption("fastq-out")) {
            writeFasta = false;
        }

        if (line.hasOption("less-than")) {
            trimle = true;
        }
        if (line.hasOption("illumina")) {
            illumina = true;
        }

        args = line.getArgs();

        if (args.length < 2) {
            throw new Exception("Unexpected number of arguments");
        }

        if (args[0].length() != 1) {
            throw new Exception("Expected single character quality score");
        }

        qualTrim = FastqCore.Phred33QualFunction.translate(args[0].charAt(0));

        for (int index = 1; index < args.length; index++) {
            File seqFile = new File(args[index]);
            SeqReader reader;
            if (SeqUtils.guessFileFormat(seqFile) == SequenceFormat.FASTA) {
                if (index + 1 == args.length) {
                    throw new Exception("Fasta files must be immediately followed by their quality file");
                }

                File qualFile = new File(args[index + 1]);
                if (SeqUtils.guessFileFormat(qualFile) != SequenceFormat.FASTA) {
                    throw new Exception(seqFile + " was not followed by a fasta quality file");
                }

                reader = new QSeqReader(seqFile, qualFile);
                index++;
            } else {
                if (seqFile.getName().endsWith(".gz")) {
                    reader = new SequenceReader(new GZIPInputStream(new FileInputStream(seqFile)));
                } else {
                    reader = new SequenceReader(seqFile);
                }
            }

            readers.add(reader);
            seqFiles.add(seqFile);
        }
    } catch (Exception e) {
        new HelpFormatter().printHelp("USAGE: QualityTrimmer [options] <ascii_score> <seq_file> [qual_file]",
                options, true);
        System.err.println("Error: " + e.getMessage());
        System.exit(1);
    }

    for (int readerIndex = 0; readerIndex < readers.size(); readerIndex++) {
        File seqFile = seqFiles.get(readerIndex);
        String outStem = "trimmed_" + seqFile.getName().substring(0, seqFile.getName().lastIndexOf("."));
        if (writeFasta) {
            fastaOut = new FastaWriter(outStem + ".fasta");
        } else {
            fastqOut = new FastqWriter(outStem + ".fastq", FastqCore.Phred33QualFunction);
        }

        int[] lengthHisto = new int[200];

        SeqReader reader = readers.get(readerIndex);

        QSequence qseq;

        long totalLength = 0;
        int totalSeqs = 0;
        long trimmedLength = 0;
        int trimmedSeqs = 0;

        int zeroLengthAfterTrimming = 0;

        long startTime = System.currentTimeMillis();

        while ((qseq = (QSequence) reader.readNextSequence()) != null) {
            char[] bases = qseq.getSeqString().toCharArray();
            byte[] qual = qseq.getQuality();

            if (bases.length != qual.length) {
                System.err.println(qseq.getSeqName() + ": Quality length doesn't match seq length for seq");
                continue;
            }

            totalSeqs++;
            totalLength += bases.length;

            int trimIndex = -1;
            if (illumina && qual[bases.length - 1] == qualTrim) {
                trimIndex = bases.length - 1;
                while (trimIndex >= 0 && qual[trimIndex] == qualTrim) {
                    trimIndex--;
                }

                trimIndex++; //Technically we're positioned over the first good base, move back to the last bad base
            } else if (!illumina) {
                for (int index = 0; index < bases.length; index++) {
                    if (qual[index] == qualTrim || (trimle && qual[index] < qualTrim)) {
                        trimIndex = index;
                        break;
                    }
                }
            }

            String outSeq;
            byte[] outQual;
            if (trimIndex == -1) {
                outSeq = qseq.getSeqString();
                outQual = qseq.getQuality();
            } else {
                outSeq = new String(bases, 0, trimIndex);
                outQual = Arrays.copyOfRange(qual, 0, trimIndex);
                trimmedSeqs++;
            }
            int len = outSeq.length();
            trimmedLength += len;

            if (len >= lengthHisto.length) {
                lengthHisto = Arrays.copyOf(lengthHisto, len + 1);
            }
            lengthHisto[len]++;

            if (outSeq.length() == 0) {
                //System.err.println(qseq.getSeqName() + ": length 0 after trimming");
                zeroLengthAfterTrimming++;
                continue;
            }

            if (writeFasta) {
                fastaOut.writeSeq(qseq.getSeqName(), qseq.getDesc(), outSeq);
            } else {
                fastqOut.writeSeq(qseq.getSeqName(), qseq.getDesc(), outSeq, outQual);
            }
        }

        reader.close();

        if (writeFasta) {
            fastaOut.close();
        } else {
            fastqOut.close();
        }

        System.out.println(
                "Processed " + seqFile + " in " + (System.currentTimeMillis() - startTime) / 1000.0 + "s");
        System.out.println("Before trimming:");
        System.out.println("Total Sequences:           " + totalSeqs);
        System.out.println("Total Sequence Data:       " + totalLength);
        System.out.println("Average sequence length:   " + ((float) totalLength / totalSeqs));
        System.out.println();
        System.out.println("After trimming:");
        System.out.println("Total Sequences:           " + (totalSeqs - zeroLengthAfterTrimming));
        System.out.println("Sequences Trimmed:         " + trimmedSeqs);
        System.out.println("Total Sequence Data:       " + trimmedLength);
        System.out.println("Average sequence length:   "
                + ((float) trimmedLength / (totalSeqs - zeroLengthAfterTrimming)));
        System.out.println();

        System.out.println("Length\tCount");
        for (int index = 0; index < lengthHisto.length; index++) {
            if (lengthHisto[index] == 0) {
                continue;
            }

            System.out.println(index + "\t" + lengthHisto[index]);
        }

        System.out.println();
        System.out.println();
        System.out.println();
    }
}

From source file:de.hpi.fgis.hdrs.tools.Loader.java

public static void main(String[] args) throws IOException {

    if (2 > args.length) {
        System.out.println(usage);
        System.out.println(options);
        System.exit(1);//from   w w  w . ja va  2  s .  c  o  m
    }

    if (0 > args[0].indexOf(':')) {
        args[0] += ":" + Configuration.DEFAULT_RPC_PORT;
    }
    Configuration conf = Configuration.create();
    Client client = new Client(conf, args[0]);

    File[] files;
    String options = "";
    if (args[1].startsWith("-")) {
        options = args[1];
        if (3 > args.length) {
            System.out.println(usage);
            System.exit(1);
        }
        if (0 < options.indexOf('d')) {
            File dir = new File(args[2]);
            if (!dir.isDirectory()) {
                throw new IOException("Directory does not exist.");
            }
            files = dir.listFiles();
        } else {
            files = new File[] { new File(args[2]) };
        }
    } else {
        files = new File[] { new File(args[1]) };
    }

    boolean quiet = 0 < options.indexOf('q');
    boolean context = 0 < options.indexOf('c');

    boolean bench = 0 < options.indexOf('b');
    List<BenchSample> benchSamples = null;
    if (bench) {
        benchSamples = new ArrayList<BenchSample>();
    }

    long timeStalled = 0;
    long timeRouterUpdate = 0;
    long abortedTransactions = 0;

    long nBytesTotal = 0;
    long nTriplesTotal = 0;
    long timeTotal = 0;

    for (int i = 0; i < files.length; ++i) {
        Closeable source = null;
        TripleScanner scanner = null;

        try {
            if (0 < options.indexOf('t')) {
                TripleFileReader reader = new TripleFileReader(files[i]);
                reader.open();
                scanner = reader.getScanner();
                source = reader;
            } else if (0 < options.indexOf('z')) {
                GZIPInputStream stream = new GZIPInputStream(new FileInputStream(files[i]));
                BTCParser parser = new BTCParser();
                parser.setSkipContext(!context);
                scanner = new StreamScanner(stream, parser);
                source = stream;
            } else {
                BTCParser parser = new BTCParser();
                parser.setSkipContext(!context);
                FileSource file = new FileSource(files[i], parser);
                scanner = file.getScanner();
                source = file;
            }
        } catch (IOException ioe) {
            System.out.println("Error: Couldn't open " + files[i] + ". See log for details.");
            LOG.error("Error: Couldn't open " + files[i] + ":", ioe);
            continue;
        }

        long nBytes = 0;
        long nTriples = 0;
        long time = System.currentTimeMillis();

        TripleOutputStream out = client.getOutputStream();

        while (scanner.next()) {
            Triple t = scanner.pop();
            out.add(t);
            nBytes += t.serializedSize();
            nTriples++;

            if (!quiet && 0 == (nTriples % (16 * 1024))) {
                System.out.print(String.format("\rloading... %d triples (%.2f MB, %.2f MB/s)", nTriples,
                        LogFormatUtil.MB(nBytes),
                        LogFormatUtil.MBperSec(nBytes, System.currentTimeMillis() - time)));
            }
        }
        out.close();

        time = System.currentTimeMillis() - time;

        scanner.close();
        source.close();

        if (!quiet) {
            System.out.print("\r");
        }
        System.out.println(String.format("%s: %d triples (%.2f MB) loaded " + "in %.2f seconds (%.2f MB/s)",
                files[i], nTriples, LogFormatUtil.MB(nBytes), time / 1000.0,
                LogFormatUtil.MBperSec(nBytes, time)));

        nBytesTotal += nBytes;
        nTriplesTotal += nTriples;
        timeTotal += time;

        timeStalled += out.getTimeStalled();
        timeRouterUpdate += out.getTimeRouterUpdate();
        abortedTransactions += out.getAbortedTransactions();

        if (bench) {
            benchSamples.add(new BenchSample(time, nTriples, nBytes));
        }
    }

    client.close();

    if (0 == nTriplesTotal) {
        System.out.println("No triples loaded.");
        return;
    }

    System.out.println(
            String.format("Done loading.  Totals: %d triples (%.2f MB) loaded " + "in %.2f seconds (%.2f MB/s)",
                    nTriplesTotal, LogFormatUtil.MB(nBytesTotal), timeTotal / 1000.0,
                    LogFormatUtil.MBperSec(nBytesTotal, timeTotal)));

    System.out.println(String.format(
            "  Client stats.  Stalled: %.2f s  RouterUpdate: %.2f s" + "  AbortedTransactions: %d",
            timeStalled / 1000.0, timeRouterUpdate / 1000.0, abortedTransactions));

    if (bench) {
        System.out.println();
        System.out.println("Benchmark Samples:");
        System.out.println("time\tsum T\tsum MB\tMB/s");
        System.out.println(String.format("%.2f\t%d\t%.2f\t%.2f", 0f, 0, 0f, 0f));
        long time = 0, nTriples = 0, nBytes = 0;
        for (BenchSample sample : benchSamples) {
            time += sample.time;
            nTriples += sample.nTriples;
            nBytes += sample.nBytes;
            System.out.println(String.format("%.2f\t%d\t%.2f\t%.2f", time / 1000.0, nTriples,
                    LogFormatUtil.MB(nBytes), LogFormatUtil.MBperSec(sample.nBytes, sample.time)));
        }
    }
}

From source file:Main.java

public static byte[] decompressGZIP(byte bytes[]) throws IOException {
    ByteArrayInputStream is = new ByteArrayInputStream(bytes);
    GZIPInputStream gzipis = new GZIPInputStream(is);
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    int i;/*from w w w  .j a  va 2s .  co m*/
    while ((i = gzipis.read()) != -1) {
        os.write(i);
    }
    gzipis.close();
    os.close();
    return os.toByteArray();
}