Example usage for org.apache.commons.compress.compressors.bzip2 BZip2CompressorInputStream close

List of usage examples for org.apache.commons.compress.compressors.bzip2 BZip2CompressorInputStream close

Introduction

In this page you can find the example usage for org.apache.commons.compress.compressors.bzip2 BZip2CompressorInputStream close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:Bzip2Uncompress.java

public static void main(final String[] args) {
    try {/*from  w ww .java2s  . c  o  m*/
        if (2 != args.length) {
            System.out.println("java Bzip2Uncompress <input> <output>");
            System.exit(1);
        }

        final File source = new File(args[0]);
        final File destination = new File(args[1]);
        final FileOutputStream output = new FileOutputStream(destination);
        final BZip2CompressorInputStream input = new BZip2CompressorInputStream(new FileInputStream(source));
        copy(input, output);
        input.close();
        output.close();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}

From source file:com.graphhopper.tools.Bzip2.java

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        throw new IllegalArgumentException("You need to specify the bz2 file!");
    }//www .  j a  v  a2 s . c  om

    String fromFile = args[0];
    if (!fromFile.endsWith(".bz2")) {
        throw new IllegalArgumentException("You need to specify a bz2 file! But was:" + fromFile);
    }
    String toFile = Helper.pruneFileEnd(fromFile);

    FileInputStream in = new FileInputStream(fromFile);
    FileOutputStream out = new FileOutputStream(toFile);
    BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
    try {
        final byte[] buffer = new byte[1024 * 8];
        int n = 0;
        while (-1 != (n = bzIn.read(buffer))) {
            out.write(buffer, 0, n);
        }
    } finally {
        out.close();
        bzIn.close();
    }
}

From source file:CompressTransfer.java

/**
 * ?//from  w  w  w .ja v  a2 s.c  o m
 *
 * @param is
 * @param os
 * @throws Exception
 */
public static void bzdecompress(InputStream is, OutputStream os) throws Exception {

    BZip2CompressorInputStream gis = new BZip2CompressorInputStream(is);

    int count;
    byte data[] = new byte[BUFFER];
    while ((count = gis.read(data, 0, BUFFER)) != -1) {
        os.write(data, 0, count);
    }

    gis.close();
}

From source file:net.orpiske.ssps.common.archive.CompressedArchiveUtils.java

/**
 * Decompress a file//from  w  w w.j a v  a2  s . c  o m
 * @param source the source file to be uncompressed
 * @param destination the destination directory
 * @return the number of bytes read
 * @throws IOException for lower level I/O errors
 */
public static long bzipDecompress(File source, File destination) throws IOException {
    FileOutputStream out;

    prepareDestination(destination);

    out = new FileOutputStream(destination);

    FileInputStream fin = null;
    BufferedInputStream bin = null;
    BZip2CompressorInputStream bzIn = null;

    try {
        fin = new FileInputStream(source);
        bin = new BufferedInputStream(fin);
        bzIn = new BZip2CompressorInputStream(bin);

        IOUtils.copy(bzIn, out);

        bzIn.close();

        fin.close();
        bin.close();
        out.close();
    } catch (IOException e) {
        IOUtils.closeQuietly(out);

        IOUtils.closeQuietly(fin);
        IOUtils.closeQuietly(bin);
        IOUtils.closeQuietly(bzIn);

        throw e;
    }

    return bzIn.getBytesRead();
}

From source file:com.cip.crane.agent.utils.FileExtractUtils.java

/**
 * Unbzip an input file into an output file.
 * <p>//w  ww.j  ava 2 s . com
 * The output file is created in the output folder, having the same name
 * as the input file, minus the '.bz2' extension. 
 * 
 * @param inputFile     the input .bz2 file
 * @param outputDir     the output directory file. 
 * @throws IOException 
 * @throws FileNotFoundException
 *  
 * @return  The {@File} with the unbzipped content.
 */
public static File unBzip(final File inputFile, final File outputDir)
        throws FileNotFoundException, IOException {

    LOG.debug(String.format("Unbzipping %s to dir %s.", inputFile.getAbsolutePath(),
            outputDir.getAbsolutePath()));

    final File outputFile = new File(outputDir,
            inputFile.getName().substring(0, inputFile.getName().length() - 4));

    final BZip2CompressorInputStream in = new BZip2CompressorInputStream(new FileInputStream(inputFile));
    final FileOutputStream out = new FileOutputStream(outputFile);

    IOUtils.copy(in, out);
    in.close();
    out.close();

    return outputFile;
}

From source file:com.yahoo.ycsb.db.RedisClient.java

public static String decompress(String st) {
    if (compress != null && compress.equals("y")) {
        if (compressAlgo != null && (compressAlgo.equals("lz4") || compressAlgo.equals("lz4hc"))) {
            try {
                int split = st.indexOf('|');
                final int decompressedLength = Integer.parseInt(st.substring(0, split));
                LZ4FastDecompressor decompressor = lz4factory.fastDecompressor();
                byte[] restored = new byte[decompressedLength];
                byte[] compressed = st.substring(split + 1, st.length()).getBytes("ISO-8859-1");
                decompressor.decompress(compressed, 0, restored, 0, decompressedLength);
                String ret = new String(restored, "ISO-8859-1");
                return ret;
            } catch (Exception e) {
                e.printStackTrace();/*from   w  ww. jav  a 2  s . c o  m*/
            }
        } else if (compressAlgo != null && compressAlgo.equals("bzip2")) {
            try {
                InputStream in = new StringBufferInputStream(st);
                BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
                byte[] data = st.getBytes("ISO-8859-1");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int n = 0;
                while (-1 != (n = bzIn.read(data))) {
                    baos.write(data, 0, n);
                }
                bzIn.close();
                return baos.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (compressAlgo != null && compressAlgo.equals("snappy")) {
            try {
                byte[] uncompressed = Snappy.uncompress(st.getBytes("ISO-8859-1"));
                String ret = new String(uncompressed, "ISO-8859-1");
                return ret;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    return st;
}

From source file:aarddict.Volume.java

static String decompressBz2(byte[] bytes) throws IOException {
    BZip2CompressorInputStream in = new BZip2CompressorInputStream(new ByteArrayInputStream(bytes));

    int n = 0;// w  w w .ja  v a 2s. c  o  m
    ByteArrayOutputStream out = new ByteArrayOutputStream(bytes.length * 5);
    byte[] buf = new byte[1024];
    try {
        while (-1 != (n = in.read(buf))) {
            out.write(buf, 0, n);
        }
    } finally {
        in.close();
        out.close();
    }
    return utf8(out.toByteArray());
}

From source file:com.enigmastation.ml.bayes.CorpusTest.java

private String expandFile(String fileName) throws URISyntaxException, IOException {
    URL dataResource = this.getClass().getResource("/src/test/resources/publiccorpus/" + fileName);
    File inputFile = new File(dataResource.toURI());
    File outputDirectory = new File(inputFile + ".data");
    outputDirectory.mkdir();// ww w . j  a v  a 2  s .  com

    FileInputStream fis = new FileInputStream(inputFile);
    BufferedInputStream bis = new BufferedInputStream(fis);

    BZip2CompressorInputStream bzip2 = new BZip2CompressorInputStream(bis);
    TarArchive archive = new TarArchive(bzip2);
    archive.extractContents(outputDirectory);
    System.out.printf("%s finished expanding into \"%s\".%n", inputFile.getName(), outputDirectory.toString());
    bzip2.close();

    return "/src/test/resources/publiccorpus/" + inputFile.getName() + ".data";
}

From source file:io.netty.handler.codec.compression.Bzip2EncoderTest.java

@Override
protected ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception {
    InputStream is = new ByteBufInputStream(compressed);
    BZip2CompressorInputStream bzip2Is = new BZip2CompressorInputStream(is);

    byte[] decompressed = new byte[originalLength];
    int remaining = originalLength;
    while (remaining > 0) {
        int read = bzip2Is.read(decompressed, originalLength - remaining, remaining);
        if (read > 0) {
            remaining -= read;/*w  ww  .j  av a  2 s.  c  o m*/
        } else {
            break;
        }
    }
    assertEquals(-1, bzip2Is.read());
    bzip2Is.close();

    return Unpooled.wrappedBuffer(decompressed);
}

From source file:example.TestLineRecordReader.java

public String[] readRecordsDirectly(URL testFileUrl, boolean bzip) throws IOException {
    int MAX_DATA_SIZE = 1024 * 1024;
    byte[] data = new byte[MAX_DATA_SIZE];
    FileInputStream fis = new FileInputStream(testFileUrl.getFile());
    int count;//from   ww  w. j av  a 2s.com
    if (bzip) {
        BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(fis);
        count = bzIn.read(data);
        bzIn.close();
    } else {
        count = fis.read(data);
    }
    fis.close();
    assertTrue("Test file data too big for buffer", count < data.length);
    return new String(data, 0, count, "UTF-8").split("\n");
}