Get InputStream out of TarArchiveInputStream

1.9k views Asked by At

I'm getting files from an FTP. The files I'm getting are either text files or tar.gz

For text files I just send them to S3. If I encounter a tar.gz I want to untar it and save each file with the same method.

public void handleFile() {

    try (InputStream fileStream = ftpFileService.getInputStream(file)) {
        if (file.getName().lastIndexOf(".TGZ") > -1) {
            TarArchiveInputStream tar = new TarArchiveInputStream(new GzipCompressorInputStream(fileStream));
            TarArchiveEntry entry = null;
            while ((entry = tar.getNextTarEntry()) != null) {
                LOGGER.info("fileName to save {}", entry.getName());
                saveStreamToS3(entry.getName(), new InputStream(tar));
            }
            tar.close();
        } else {
            LOGGER.info("fileName to save {}", fileName.getName());
            saveStreamToS3(fileName.getName(), fileStream);
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage());
    }
}

I tried saving the entry directly using new FileInputStream(entry.getFile()) but this returns null.

Do I need to make a saveTarStreamToS3() or can I make an InputStream out of a TarArchiveInputStream?

1

There are 1 answers

1
Peter Lawrey On BEST ANSWER

FileInputStream only reads real files. It doesn't read data from inside an archive.

There are two possible solutions

  • Use InputStream which FileInputStream and TarArchiveInputStream impliements
  • Copy the file to disk, read using FileInputStream, delete afterwards.

The purpose on the interface InputStream is so you don't need to know where the data is coming from, and this is the natural way to solve this.

can I make an InputStream out of a TarArchiveInputStream

TarArchiveInputStream implements InputStream so there is nothing to do.