I'm reading a tar.gz
archive using
ArchiveEntry entry = tarArchiveInputStream.getNextEntry();
Question: how can I convert this ArchiveEntry to an InputStream
so I can actually read and process the file to a String
?
I'm reading a tar.gz
archive using
ArchiveEntry entry = tarArchiveInputStream.getNextEntry();
Question: how can I convert this ArchiveEntry to an InputStream
so I can actually read and process the file to a String
?
You could use IOUtils to read InputStream fully:
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);
If your file is in other encoding than utf-8, use that instead of utf-8 in the constructor of string.
IOUtils.readFully
can be used directly from commons.compress
. –
Lungworm It is already an InputStream
.
byte[] buf = new byte[(int) entry.getSize()];
int k = tarArchiveInputStream.read(buf, 0, buf.length);
String s = new String(buf, 0, k);
You could use IOUtils to read InputStream fully:
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);
If your file is in other encoding than utf-8, use that instead of utf-8 in the constructor of string.
IOUtils.readFully
can be used directly from commons.compress
. –
Lungworm If you really want to read the file one by one, the TarEntry actually holds the File object in it:
This class represents an entry in a Tar archive. It consists of the entry's header, as well as the entry's File.
Thus just initialize another FileInputStream would be enough:
import org.apache.commons.io.IOUtils;
String file = IOUtils.toString(new FileInputStream(entry.getFile());
© 2022 - 2024 — McMap. All rights reserved.
org.apache.commons.compress.archivers.ArchiveEntry
from Apache's commons-compress? – Buckie