0

I am trying to use Java's NIO filesystem to move files from a zip folder to a regular folder in Windows. And it is meant to be used by a custom function in Matlab.

However, when I tried in Matlab cmd the followings,

>> version -release
ans =
    '2020b'
>> version -java
ans =
    'Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode'

%%%%

FilePath=java.nio.file.Paths.get('C:\test',javaArray('java.lang.String', 0));
FileSystem=java.nio.file.FileSystems.newFileSystem(FilePath,[]);

I get the Provider not found exception:

Java exception occurred:
java.nio.file.ProviderNotFoundException: Provider not found

    at java.nio.file.FileSystems.newFileSystem(FileSystems.java:407)

I would not get the exception and would be able to use the methods in NIO filesystem if the path was a zip file and not a regular folder in Windows.


In case an answer of this question applies, I also tried the following but to no avail.

>> FileSystem=java.nio.file.FileSystems.newFileSystem(FilePath,java.util.HashMap);
No method 'java.nio.file.FileSystems.newFileSystem' with matching signature found.

How do I use Java NIO filesystem for a Windows folder in Matlab?

(I am actually not sure if NIO filesystem can unzip automatically. But even if zip files are not involved, the error is encountered when reading regular folders.)

Argyll
  • 8,591
  • 4
  • 25
  • 46
  • Unfortunately ZIP access with `Files.newFileSystem(Path path, Map env)` requires JDK13+. However JDK1.8+ has `java.util.zip.ZipInputStream` and `java.util.zip.ZipFile` which also allow reading from ZIP. – DuncG Aug 16 '22 at 18:28
  • @DuncG: I am actually able to instantiate NIO filesystem with zip files and can load file list within zip file, change name, move files between zip folders, etc. I only get the exception for regular Windows folders. I can edit the question. Is there anything else that may help in clarifying? – Argyll Aug 16 '22 at 18:57
  • Using `newFileSystem(path, ... )` where path is a regular directory (C:\test) will give `ProviderNotFoundException: Provider not found`. But if you use `path = Paths.get("some.zip")` then that should open the zip as a filesystem. From there re-construct the Windows filesystem from the zip filesystem using normal `Files` API calls like `copy(zipPath, targetpath)`/`createDirectories(targetpath)` where targetpath is created from the Windows folder path plus zipPath - something like `targetpath = Paths.get(targetdir, zipPath.toString())` or `targetpath = targetdir.resolve(zipPath))`. – DuncG Aug 16 '22 at 19:35
  • @DuncG: So the NIO FileSystems is meant to be for zip files only? I must have forgotten that from the documentation – Argyll Aug 16 '22 at 20:28
  • The default filesystem is used for `Paths.get / Path.of`, so you can use `Paths.get("C:/Temp").getFileSystem()` or just `FileSystems.getDefault()` for that FS instance. The docs for `newFileSystem` say "Constructs a new FileSystem to access the contents of a file as a file system" – DuncG Aug 16 '22 at 20:40

0 Answers0