How to convert ArchiveEntry to InputStream?
Asked Answered
L

3

7

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?

Lungworm answered 7/1, 2016 at 11:12 Comment(2)
To clarify, you mean org.apache.commons.compress.archivers.ArchiveEntry from Apache's commons-compress?Buckie
yes I mean commons.compress.Lungworm
J
4

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.

Juneberry answered 7/1, 2016 at 11:51 Comment(1)
This is probabnly the correct solution as IOUtils.readFully can be used directly from commons.compress.Lungworm
C
4

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);
Chronological answered 7/1, 2016 at 11:16 Comment(0)
J
4

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.

Juneberry answered 7/1, 2016 at 11:51 Comment(1)
This is probabnly the correct solution as IOUtils.readFully can be used directly from commons.compress.Lungworm
S
3

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());
Sabrinasabsay answered 26/10, 2017 at 22:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.