0

I am trying to walk through a zip file and print out all files, but for some reason, it throws a NoSuchFileException as soon as it goes into any subfolder.

    FileSystem fs = FileSystems.newFileSystem(Paths.get(folder.getRoot().getAbsolutePath(), "test.zip"), null);

    Files.walkFileTree(fs.getPath("/"), new SimpleFileVisitor<Path>() {
        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
            System.out.println(file);
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) {
            System.err.println(exc);
            return FileVisitResult.CONTINUE;
        }

    });

How come it even wants to go there if they don't exist? Can anyone help me to solve this problem? The files should actually be there, at least they do when the test is executed by hand.

XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65

2 Answers2

0

Take extra care, when using FileSystem implementations, as they are especially dependent on the JVM you are using, and on the OS! Cf. the Javadoc of FileSystem

A very good example is ZipFileSystem. Its maiden name is com.sun.nio.zipfs.ZipFileSystem, but the IBM JRE has its own implementation in com.ibm.ws.install.ni.framework.io.ZIPFileSystem. Also for example from a ZIP File you cannot access internals ZIPs, because of the layout of the archive. You have to unzip it, and mount the internal ZIP as another FileSystem. (Afterwards, of course, you have to delete these)

And of course see this SO answer: probably your expectations are false. Debug where exactly the exception occurs and correlate that to your expectations.

D. Kovács
  • 1,232
  • 13
  • 25
  • my expectations are right. my output from zipping shows me that files were added to the zip yet i can't access them – XtremeBaumer Dec 06 '17 at 13:23
-1

If i execute this code it just works for me:

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.*;
import java.util.zip.*;

public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        try (ZipFile zipFile = new ZipFile("logs\\subfolder\\subsubfolder\\logs.zip")) {
            zipFile.stream()
               .map(ZipEntry::getName)
               .forEach(System.out::println);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }




//         Path fs = FileSystems.getDefault().getPath("logs", "");
//
//         System.out.println(fs.toAbsolutePath());
//
//         SimpleFileVisitor<Path> sfv = new SimpleFileVisitor<Path>() {
//              @Override
//              public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
//                  System.out.println(file.toAbsolutePath());
//                  return FileVisitResult.CONTINUE;
//              }
//
//              @Override
//              public FileVisitResult visitFileFailed(Path file, IOException exc) {
//                  System.err.println(exc);
//                  return FileVisitResult.CONTINUE;
//              }
//
//          };
//         
//         try {
//              Files.walkFileTree(fs, sfv);
//         } catch (IOException e) {
//             // TODO Auto-generated catch block
//             e.printStackTrace();
//         }

    }

}

this is an image of the project directory structure in eclipse: enter image description here

There is a file called access.log in the logs directory.

and this is my console output:

access.log
Microsoft Access Database (neu).accdb
Microsoft Excel-Arbeitsblatt (neu).xlsx
test/Microsoft Access Database (neu).accdb
test/Microsoft Excel-Arbeitsblatt (neu).xlsx

All files that are in the Zip and its subfolder.

List .zip directories without extracting

code1x1.de
  • 304
  • 2
  • 13