FileOutputStream(No such file or directory)(No such file or directory)

229 views Asked by At

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?

1

There are 1 answers

2
Justin On

Does

entry.getName()

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