以下サンプルでは,単純なテキストファイルを gzip,bzip2 で圧縮されたファイルを読み込むものとします.
gzip はJVM標準のライブラリだけで読み込み可能です.
java.util.zip.GZIPInputStream を利用します.
package hello.compress;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
public class HelloGZipMain {
public static void main(String[] args) throws Exception {
File gzFile = new File("hello-compress.txt.gz");
try (InputStream is = new FileInputStream(gzFile);
// java.util.zip.GZIPInputStream
GZIPInputStream gzis = new GZIPInputStream(is);
InputStreamReader isr = new InputStreamReader(gzis, StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(isr);) {
String s;
while ((s = br.readLine()) != null) {
System.err.println(s);
}
}
}
}
bzip2 ファイルは Apache Commons Compress を利用するのがベストかと思います.
package hello.compress;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
public class HelloBZ2Main {
public static void main(String[] args) throws Exception {
File bz2File = new File("hello-compress.txt.bz2");
// try-with-resources
try (FileInputStream fis = new FileInputStream(bz2File);
BufferedInputStream bis = new BufferedInputStream(fis);
// see https://mvnrepository.com/artifact/org.apache.commons/commons-compress
// org.apache.commons.compress.compressors.CompressorStreamFactory
CompressorInputStream cis = new CompressorStreamFactory() //
.createCompressorInputStream(bis);
BufferedReader br = new BufferedReader(new InputStreamReader(cis, "UTF-8"));) {
String line;
while ((line = br.readLine()) != null) {
System.err.println(line);
}
} catch (CompressorException e) {
throw new IOException(e);
}
}
}
以上.