1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Java で圧縮ファイル gzip (.gz) ,bzip2 (.bz2) を読み込む

Last updated at Posted at 2022-08-04

以下サンプルでは,単純なテキストファイルを 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);
		}
	}
}

以上.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?