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?

MWiesner
  • 8,868
  • 11
  • 36
  • 70
membersound
  • 81,582
  • 193
  • 585
  • 1,120

3 Answers3

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);
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.

james.garriss
  • 12,959
  • 7
  • 83
  • 96
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());
Yaokai Yang
  • 31
  • 1
  • 6