javawindowszipunzipzip4j

Can I skip symbolic links when extracting a zipfile in Windows using zip4j?


I'm using the zip4j library to extract a zip file containing symbolic links under Windows.

My code is as follows:

final String path = App.class.getResource("/file.zip").getPath();
final ZipFile zipFile = new ZipFile(path);
final Path tempDirectory = Files.createTempDirectory("tmp");
zipFile.extractAll(tempDirectory.toString());

(Minimal working example is available at https://github.com/maikelsteneker/zip4j-symlink)

This zip file contains a symbolic link. Creating such symbolic links requires Administrator rights in Windows. As a result, the following exception occurs:

Exception in thread "main" net.lingala.zip4j.exception.ZipException: java.nio.file.FileSystemException: C:\Users\Maikel\AppData\Local\Temp\tmp10742489646321434610\symlink.txt: A required privilege is not held by the client.

        at net.lingala.zip4j.tasks.AsyncZipTask.performTaskWithErrorHandling(AsyncZipTask.java:53)
        at net.lingala.zip4j.tasks.AsyncZipTask.execute(AsyncZipTask.java:40)
        at net.lingala.zip4j.ZipFile.extractAll(ZipFile.java:436)
        at zip4j.symlink.App.main(App.java:17)
Caused by: java.nio.file.FileSystemException: C:\Users\Maikel\AppData\Local\Temp\tmp10742489646321434610\symlink.txt: A required privilege is not held by the client.

        at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:92)
        at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103)
        at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:108)
        at java.base/sun.nio.fs.WindowsFileSystemProvider.createSymbolicLink(WindowsFileSystemProvider.java:585)
        at java.base/java.nio.file.Files.createSymbolicLink(Files.java:1058)
        at net.lingala.zip4j.tasks.AbstractExtractFileTask.createSymLink(AbstractExtractFileTask.java:108)
        at net.lingala.zip4j.tasks.AbstractExtractFileTask.extractFile(AbstractExtractFileTask.java:61)
        at net.lingala.zip4j.tasks.ExtractAllFilesTask.executeTask(ExtractAllFilesTask.java:38)
        at net.lingala.zip4j.tasks.ExtractAllFilesTask.executeTask(ExtractAllFilesTask.java:16)
        at net.lingala.zip4j.tasks.AsyncZipTask.performTaskWithErrorHandling(AsyncZipTask.java:46)
        ... 3 more

Is there a way to skip these symbolic links in order to avoid this exception? Ideally, I'd like to skip them only if these rights are not available (some users may have Administrator rights or have otherwise configured their system to allow the creation of symbolic links). I'm looking for an elegant solution; I don't want to work around the library.


Solution

  • The library doesn't seem to currently support such functionality. I'd recommend filing a bug/feature request. You could suggest a custom error handler which allows ignoring certain errors or possibly a custom filter which allows choosing which files to extract on the fly.