What is the fastest way to extract 1 file from a zip file which contain a lot of file?

前端 未结 3 1573
忘掉有多难
忘掉有多难 2021-02-02 01:08

I tried the java.util.zip package, it is too slow.

Then I found LZMA SDK and 7z jbinding but they are also lacking something.

The LZMA SDK does not provide a ki

3条回答
  •  醉话见心
    2021-02-02 01:31

    I have not benchmarked the speed but with java 7 or greater, I extract a file as follows.
    I would imagine that it's faster than the ZipFile API:

    A short example extracting META-INF/MANIFEST.MF from a zip file test.zip:

    // file to extract from zip file
    String file = "MANIFEST.MF";
    // location to extract the file to
    File outputLocation = new File("D:/temp/", file);
    // path to the zip file
    Path zipFile = Paths.get("D:/temp/test.zip");
    
    // load zip file as filesystem
    try (FileSystem fileSystem = FileSystems.newFileSystem(zipFile)) {
        // copy file from zip file to output location
        Path source = fileSystem.getPath("META-INF/" + file);
        Files.copy(source, outputLocation.toPath());
    }
    

提交回复
热议问题