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;
}
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();
}
}
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);
}
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");
}
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");
}
Aggregations