tar.gz
を使用してアーカイブを読んでいます
ArchiveEntry entry = tarArchiveInputStream.getNextEntry();
質問: このArchiveEntryをに変換して、InputStream
実際にファイルを に読み込んで処理できるようにするにはどうすればよいString
ですか?
tar.gz
を使用してアーカイブを読んでいます
ArchiveEntry entry = tarArchiveInputStream.getNextEntry();
質問: このArchiveEntryをに変換して、InputStream
実際にファイルを に読み込んで処理できるようにするにはどうすればよいString
ですか?
IOUtils を使用して、InputStream を完全に読み取ることができます。
import org.apache.commons.compress.utils.IOUtils
byte[] buf = new byte[(int) entry.getSize()];
int readed = IOUtils.readFully(tarArchiveInputStream,buf);
//readed should equal buffer size
if(readed != buf.length) {
throw new RuntimeException("Read bytes count and entry size differ");
}
String string = new String(buf, StandardCharsets.UTF_8);
ファイルが utf-8 以外のエンコーディングである場合は、文字列のコンストラクターで utf-8 の代わりにそれを使用します。
それはすでにInputStream
です。
byte[] buf = new byte[(int) entry.getSize()];
int k = tarArchiveInputStream.read(buf, 0, buf.length);
String s = new String(buf, 0, k);