0

I am extracting a TarGz file and am getting a (No such file or directory) with the following line:

        FileOutputStream fos = new FileOutputStream(outputPath + entry.getName());

My code:

private static void extractTarGz(String filePath, String outputPath) throws IOException {
    try{
        TarArchiveInputStream tais = new TarArchiveInputStream(new GzipCompressorInputStream( new BufferedInputStream( new FileInputStream(filePath))));
        TarArchiveEntry entry;
        
        while ((entry = (TarArchiveEntry) tais.getNextEntry()) != null) {
          //Create directories as required
          if (entry.isDirectory()) {
            File f = new File(outputPath + entry.getName());
            f.mkdirs();
          }else {
            int count;
            int size = (int)entry.getSize();
            byte data[] = new byte[size];
            FileOutputStream fos = new FileOutputStream(outputPath + entry.getName());
            BufferedOutputStream dest = new BufferedOutputStream(fos,size);
            while ((count = tais.read(data, 0, size)) != -1) {
              dest.write(data, 0, count);
            }
            dest.close();
        
          }
        
        }
      } catch(Exception e) {
          log.error(e);
      }
}

It gets to parentdir/childdir/file and then gives the error. I even tried the following to create the file, but still no luck

if (!f.exists()) {
    f.createNewFile();
}

Any help understand what I am missing?

user1971376
  • 93
  • 1
  • 4
  • 10

1 Answers1

-1

Does

entry.getName()

contain the file type in the name? eg. .jpg? .gif? etc. Also f is missing a ;

Justin
  • 37
  • 1
  • 1
  • 7
  • Apologies for that typo, I will correct that. Yes entry.getName() does get the file extention too, eg `stack1.PNG`. Thanks – user1971376 Sep 02 '22 at 19:42
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 09 '22 at 07:17