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 gunzip(File inFile, File outFile) {
    System.out.println("Expanding " + inFile.getAbsolutePath() + " to " + outFile.getAbsolutePath());

    FileOutputStream out = null;/*from  w w  w. jav  a2s . co m*/
    GZIPInputStream zIn = null;
    FileInputStream fis = null;
    try {
        out = new FileOutputStream(outFile);
        fis = new FileInputStream(inFile);
        zIn = new GZIPInputStream(fis);
        byte[] buffer = new byte[8 * 1024];
        int count = 0;
        do {
            out.write(buffer, 0, count);
            count = zIn.read(buffer, 0, buffer.length);
        } while (count != -1);
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:Main.java

public static File ungzip(File gzip, File toDir) throws IOException {
    toDir.mkdirs();/*  w  w w .ja v  a2s  . c o m*/
    File out = new File(toDir, gzip.getName());
    GZIPInputStream gin = null;
    FileOutputStream fout = null;
    try {
        FileInputStream fin = new FileInputStream(gzip);
        gin = new GZIPInputStream(fin);
        fout = new FileOutputStream(out);
        copy(gin, fout);
        gin.close();
        fout.close();
    } finally {
        closeQuietly(gin);
        closeQuietly(fout);
    }
    return out;
}

From source file:Main.java

public static String uncompress(InputStream inputStream) throws IOException {
    if (inputStream == null) {
        return null;
    }// w  ww .ja va  2  s  .c o m
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    GZIPInputStream gunzip = new GZIPInputStream(inputStream);
    byte[] buffer = new byte[256];
    int n;
    while ((n = gunzip.read(buffer)) >= 0) {
        out.write(buffer, 0, n);
    }
    return out.toString();
}

From source file:Main.java

/**
 * Degzips <strong>all</strong> of the datain the specified {@link ByteBuffer}.
 *
 * @param compressed The compressed buffer.
 * @return The decompressed array.//from   w  ww.  j  a  v a  2 s .com
 * @throws IOException If there is an error decompressing the buffer.
 */
public static byte[] degzip(byte[] compressed) throws IOException {
    try (InputStream is = new GZIPInputStream(new ByteArrayInputStream(compressed));
            ByteArrayOutputStream out = new ByteArrayOutputStream()) {
        byte[] buffer = new byte[1024];

        while (true) {
            int read = is.read(buffer, 0, buffer.length);
            if (read == -1) {
                break;
            }

            out.write(buffer, 0, read);
        }

        return out.toByteArray();
    }
}

From source file:Main.java

/**
 * Descomprime uma string utilizando o GZIP
 * //  ww  w. j  a v  a2  s. c om
 * @param str
 * @param encoding
 * @return
 */
public static String gzipDecompressString(String str, String encoding) {
    String decompressedString = "";

    try {
        byte[] bytes = Base64.decodeBase64(str.getBytes(encoding));
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        Reader reader = new InputStreamReader(gzip, encoding);
        StringBuffer sbuf = new StringBuffer();
        char[] buffer = new char[32 * 1024];
        int nread;
        while ((nread = reader.read(buffer)) >= 0) {
            sbuf.append(buffer, 0, nread);
        }
        decompressedString = sbuf.toString();
        reader.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return decompressedString;
}

From source file:Main.java

/**
 * Degzips the compressed array and places the results into the decompressed array.
 *
 * @param compressed The compressed array.
 * @param decompressed The decompressed array.
 * @throws IOException If an I/O error occurs.
 *///from  ww w .  ja  v a  2s . c  o m
public static void degzip(byte[] compressed, byte[] decompressed) throws IOException {
    try (DataInputStream is = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(compressed)))) {
        is.readFully(decompressed);
    }
}

From source file:Main.java

/**
 * Uncompress gzipped files/*from   w w w . ja  v a  2s.c om*/
 * @param gzippedFile The file to uncompress
 * @param destinationFile The resulting file
 * @throws java.io.IOException thrown if there is a problem finding or writing the files
 */
public static void gunzip(File gzippedFile, File destinationFile) throws IOException {
    int buffer = 2048;

    FileInputStream in = new FileInputStream(gzippedFile);
    GZIPInputStream zipin = new GZIPInputStream(in);

    byte[] data = new byte[buffer];

    // decompress the file
    FileOutputStream out = new FileOutputStream(destinationFile);
    try {
        int length;
        while ((length = zipin.read(data, 0, buffer)) != -1)
            out.write(data, 0, length);
    } finally {
        out.close();

        zipin.close();
        in.close();
    }

}

From source file:edu.cornell.med.icb.goby.reads.ColorSpaceConverter.java

public static void main(final String[] args) throws JSAPException, IOException {
    final JSAP jsap = new JSAP();

    final FlaggedOption sequenceOption = new FlaggedOption("input");
    sequenceOption.setRequired(true);/*from www.j  ava  2 s .c o m*/
    sequenceOption.setLongFlag("input");
    sequenceOption.setShortFlag('i');
    sequenceOption.setStringParser(FileStringParser.getParser().setMustBeFile(true).setMustExist(true));
    sequenceOption.setHelp("The input file (in Fasta format) to convert");
    jsap.registerParameter(sequenceOption);

    final FlaggedOption outputOption = new FlaggedOption("output");
    outputOption.setRequired(false);
    outputOption.setLongFlag("output");
    outputOption.setShortFlag('o');
    outputOption.setStringParser(FileStringParser.getParser().setMustBeFile(true));
    outputOption.setHelp("The output file to write to (default = stdout)");
    jsap.registerParameter(outputOption);

    final FlaggedOption titleOption = new FlaggedOption("title");
    titleOption.setRequired(false);
    titleOption.setLongFlag("title");
    titleOption.setShortFlag('t');
    titleOption.setHelp("Title for this conversion");
    jsap.registerParameter(titleOption);

    final Switch verboseOption = new Switch("verbose");
    verboseOption.setLongFlag("verbose");
    verboseOption.setShortFlag('v');
    verboseOption.setHelp("Verbose output");
    jsap.registerParameter(verboseOption);

    final Switch helpOption = new Switch("help");
    helpOption.setLongFlag("help");
    helpOption.setShortFlag('h');
    helpOption.setHelp("Print this message");
    jsap.registerParameter(helpOption);

    jsap.setUsage("Usage: " + ColorSpaceConverter.class.getName() + " " + jsap.getUsage());

    final JSAPResult result = jsap.parse(args);

    if (result.getBoolean("help")) {
        System.out.println(jsap.getHelp());
        System.exit(0);
    }

    if (!result.success()) {
        final Iterator<String> errors = result.getErrorMessageIterator();
        while (errors.hasNext()) {
            System.err.println(errors.next());
        }
        System.err.println(jsap.getUsage());
        System.exit(1);
    }

    final boolean verbose = result.getBoolean("verbose");

    final File sequenceFile = result.getFile("input");
    if (verbose) {
        System.out.println("Reading sequence from: " + sequenceFile);
    }

    // extract the title to use for the output header
    final String title;
    if (result.contains("title")) {
        title = result.getString("title");
    } else {
        title = sequenceFile.getName();
    }

    Reader inputReader = null;
    PrintWriter outputWriter = null;

    try {
        if ("gz".equals(FilenameUtils.getExtension(sequenceFile.getName()))) {
            inputReader = new InputStreamReader(new GZIPInputStream(FileUtils.openInputStream(sequenceFile)));
        } else {
            inputReader = new FileReader(sequenceFile);
        }
        final FastaParser fastaParser = new FastaParser(inputReader);

        final File outputFile = result.getFile("output");
        final OutputStream outputStream;
        if (outputFile != null) {
            outputStream = FileUtils.openOutputStream(outputFile);
            if (verbose) {
                System.out.println("Writing sequence : " + outputFile);
            }
        } else {
            outputStream = System.out;
        }
        outputWriter = new PrintWriter(outputStream);

        // write the header portion of the output
        outputWriter.print("# ");
        outputWriter.print(new Date());
        outputWriter.print(' ');
        outputWriter.print(ColorSpaceConverter.class.getName());
        for (final String arg : args) {
            outputWriter.print(' ');
            outputWriter.print(arg);
        }
        outputWriter.println();
        outputWriter.print("# Cwd: ");
        outputWriter.println(new File(".").getCanonicalPath());
        outputWriter.print("# Title: ");
        outputWriter.println(title);

        // now parse the input sequence
        long sequenceCount = 0;
        final MutableString descriptionLine = new MutableString();
        final MutableString sequence = new MutableString();
        final MutableString colorSpaceSequence = new MutableString();

        while (fastaParser.hasNext()) {
            fastaParser.next(descriptionLine, sequence);
            outputWriter.print('>');
            outputWriter.println(descriptionLine);

            convert(sequence, colorSpaceSequence);
            CompactToFastaMode.writeSequence(outputWriter, colorSpaceSequence);

            sequenceCount++;
            if (verbose && sequenceCount % 10000 == 0) {
                System.out.println("Converted " + sequenceCount + " entries");
            }
        }

        if (verbose) {
            System.out.println("Conversion complete!");
        }
    } finally {
        IOUtils.closeQuietly(inputReader);
        IOUtils.closeQuietly(outputWriter);
    }
}

From source file:net.es.nsi.common.util.ContentType.java

public static InputStream decode(String contentType, InputStream is) throws IOException {
    if (XGZIP.equalsIgnoreCase(contentType)) {
        return new GZIPInputStream(is);
    } else {//from   w  ww  .  j  ava2 s.c  o m
        return is;
    }
}

From source file:Main.java

public static byte[] readCompressedData(ByteArrayInputStream bais) {
    int uncompressedLength = getLength(bais);
    byte[] output = new byte[uncompressedLength];
    byte[] compressed = readData(bais);
    GZIPInputStream gs;// w w  w . j  a v  a  2  s  .  com
    try {
        gs = new GZIPInputStream(new ByteArrayInputStream(compressed));
        for (int i = 0; i < uncompressedLength; ++i) {
            output[i] = (byte) gs.read();
        }
    } catch (IOException e) {
        throw new RuntimeException("Cannot decompress", e);
    }

    return output;
}