0

I try to retrieve a zip file present on the disk using FileSystems.getFileSystem but I have an Exception:

java.nio.file.FileSystemNotFoundException at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171).

This is the code

 Path path = Paths.get("/Users/Franz/Documents/plan.zip");
 FileSystems.getFileSystem(URI.create("jar:" + path.toUri()));

But if i create a newFileSystem and after use getFileSystem it works :

 Path path = Paths.get("/Users/Franz/Documents/plan.zip");

 Map<String, String> env = new HashMap<String, String>();
 env.put("create", "false");
 URI uri = URI.create("jar:" + path.toUri());
 FileSystems.newFileSystem(uri, env);

 FileSystem fileSystem = FileSystems.getFileSystem(URI.create("jar:" + path.toUri())); // return ZipFileSystem
 Path pathFile = fileSystem2.getPath("plan.docx"); // File in the zip file
 Files.exists(pathFile); // returns true;

Can I have directly the ZipFileSystem ?

Thank you.

Bashir
  • 2,057
  • 5
  • 19
  • 44
Franz
  • 41
  • 4

1 Answers1

0

com.sun is considered proprietary and subject to change so try to avoid it. Instead, use something like ZipFile which is very easy to understand. The code example below will work for you if you replace the zip file path to your file location.

It is a bad practice to use Sun's proprietary Java classes?

import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public static void main(String[] args)
{
    try
    {
        ZipFile zipFile = new ZipFile("path to zip here");

        Enumeration<? extends ZipEntry> entries = zipFile.entries();

        while (entries.hasMoreElements()) 
        {
            //entry is your individual file within the zip folder
            ZipEntry entry = entries.nextElement();

            System.out.println(entry.getName());
            if( entry.getName().equals("plan.docx") )
            {
                System.out.println("found file... do some code here");
            }
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
arahman
  • 585
  • 2
  • 9