Search in sources :

Example 11 with BZip2CompressorInputStream

use of org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream in project gephi by gephi.

the class ImportUtils method getBzipFile.

public static File getBzipFile(FileObject in, File out, boolean isTar) throws IOException {
    // Stream buffer
    final int BUFF_SIZE = 8192;
    final byte[] buffer = new byte[BUFF_SIZE];
    BZip2CompressorInputStream inputStream = null;
    FileOutputStream outStream = null;
    try {
        FileInputStream is = new FileInputStream(in.getPath());
        inputStream = new BZip2CompressorInputStream(is);
        outStream = new FileOutputStream(out.getAbsolutePath());
        if (isTar) {
            // Read Tar header
            int remainingBytes = readTarHeader(inputStream);
            // Read content
            ByteBuffer bb = ByteBuffer.allocateDirect(4 * BUFF_SIZE);
            byte[] tmpCache = new byte[BUFF_SIZE];
            int nRead, nGet;
            while ((nRead = inputStream.read(tmpCache)) != -1) {
                if (nRead == 0) {
                    continue;
                }
                bb.put(tmpCache);
                bb.position(0);
                bb.limit(nRead);
                while (bb.hasRemaining() && remainingBytes > 0) {
                    nGet = Math.min(bb.remaining(), BUFF_SIZE);
                    nGet = Math.min(nGet, remainingBytes);
                    bb.get(buffer, 0, nGet);
                    outStream.write(buffer, 0, nGet);
                    remainingBytes -= nGet;
                }
                bb.clear();
            }
        } else {
            int len;
            while ((len = inputStream.read(buffer)) > 0) {
                outStream.write(buffer, 0, len);
            }
        }
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    } finally {
        if (inputStream != null) {
            inputStream.close();
        }
        if (outStream != null) {
            outStream.close();
        }
    }
    return out;
}
Also used : BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 12 with BZip2CompressorInputStream

use of org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream in project graphhopper by graphhopper.

the class Bzip2 method main.

public static void main(String[] args) throws IOException {
    if (args.length == 0) {
        throw new IllegalArgumentException("You need to specify the bz2 file!");
    }
    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();
    }
}
Also used : BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) FileOutputStream(java.io.FileOutputStream) FileInputStream(java.io.FileInputStream)

Example 13 with BZip2CompressorInputStream

use of org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream in project netty by netty.

the class Bzip2EncoderTest method decompress.

@Override
protected ByteBuf decompress(ByteBuf compressed, int originalLength) throws Exception {
    InputStream is = new ByteBufInputStream(compressed, true);
    BZip2CompressorInputStream bzip2Is = null;
    byte[] decompressed = new byte[originalLength];
    try {
        bzip2Is = new BZip2CompressorInputStream(is);
        int remaining = originalLength;
        while (remaining > 0) {
            int read = bzip2Is.read(decompressed, originalLength - remaining, remaining);
            if (read > 0) {
                remaining -= read;
            } else {
                break;
            }
        }
        assertEquals(-1, bzip2Is.read());
    } finally {
        if (bzip2Is != null) {
            bzip2Is.close();
        } else {
            is.close();
        }
    }
    return Unpooled.wrappedBuffer(decompressed);
}
Also used : BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) InputStream(java.io.InputStream) ByteBufInputStream(io.netty.buffer.ByteBufInputStream)

Example 14 with BZip2CompressorInputStream

use of org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream in project hadoop by apache.

the class TestLineRecordReader method readRecordsDirectly.

// Gather the records by just splitting on new lines
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;
    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");
}
Also used : BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) FileInputStream(java.io.FileInputStream)

Example 15 with BZip2CompressorInputStream

use of org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream in project beam by apache.

the class FileBasedSinkTest method testCompressionTypeBZIP2.

/** {@link CompressionType#BZIP2} correctly writes BZip2 data. */
@Test
public void testCompressionTypeBZIP2() throws FileNotFoundException, IOException {
    final File file = writeValuesWithWritableByteChannelFactory(CompressionType.BZIP2, "abc", "123");
    // Read Bzip2ed data back in using Apache commons API (de facto standard).
    assertReadValues(new BufferedReader(new InputStreamReader(new BZip2CompressorInputStream(new FileInputStream(file)), StandardCharsets.UTF_8.name())), "abc", "123");
}
Also used : InputStreamReader(java.io.InputStreamReader) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) BufferedReader(java.io.BufferedReader) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)19 FileInputStream (java.io.FileInputStream)11 IOException (java.io.IOException)6 InputStream (java.io.InputStream)6 GZIPInputStream (java.util.zip.GZIPInputStream)6 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 ZipArchiveInputStream (org.apache.commons.compress.archivers.zip.ZipArchiveInputStream)3 GzipCompressorInputStream (org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream)3 BufferedInputStream (java.io.BufferedInputStream)2 ByteBuffer (java.nio.ByteBuffer)2 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)2 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)2 CompressorInputStream (org.apache.commons.compress.compressors.CompressorInputStream)2 CompressionCodec (org.apache.hadoop.io.compress.CompressionCodec)2 Test (org.junit.Test)2 Configuration (com.alibaba.datax.common.util.Configuration)1 CompressException (com.alibaba.otter.node.etl.common.io.compress.exception.CompressException)1