0

I have been created a application which shall extract single files from tar-archive. The application reads the *.tar properly, but when i try to extract files, the application just create new files with correct filename... The files is empty (0kb). So... I probably just create new files instead of extract...

I'm a totally beginner at this point...

for(TarArchiveEntry tae : tarEntries){
    System.out.println(tarEntries.size());
    try {
        fOutput = new FileOutputStream(new File(tae.getFile(), tae.getName()));
        byte[] buf = new byte[(int) tae.getSize()];
        int len;

        while ((len = tarFile.read(buf)) > 0) {
            fOutput.write(buf, 0, len);
        }

        fOutput.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
user2734182
  • 1,835
  • 4
  • 17
  • 22
  • How is tarFile defined and what's in there? Shouldn't tae be referenced instead of tarFile.read(buf)? – yasd Oct 30 '15 at 15:35

1 Answers1

1

Assuming tarFile is a TarArchiveInputStream you can only read an entry's content right after calling tarFile.getNextTarEntry().

The stream is processed sequentially, so when you invoke getNextTarEntry you skip over the content of the current entry right to the next entry. It looks as if you had read the whole archive in order to fill tarEntries in which case you've already read past the last entry and the stream is exhausted.

Stefan Bodewig
  • 3,260
  • 15
  • 22