Extract ZIP Files using ZPFS - Java NIO Example

In the previous post, we discussed how to use ZPFS in Java NIO, to create a new ZIP archive and add some files into it.  In this post, we will discuss about extracting files from ZIP Archive using the same technique. At the end of the tutorial, you will realize how easy it is to extract entries from ZIP file using ZPFS. The list of steps involved in extracting files is captured below:

Steps to Extract ZIP File using Java NIO / ZPFS
Steps to Extract ZIP File using Java NIO / ZPFS
We will examine each of these steps now.

1.Define ZIP File System Properties


Like our previous example, we start with creating a ZIP file system. However, in this case, we are dealing with an existing ZIP file. So, the “create” property should be set to False, when you define the HashMap. The encoding needs to be specified correctly as well. Here is a Java code snippet that explains how to define the HashMap.

        /* Define ZIP File System Properies in HashMap */    
        Map<String, String> zip_properties = new HashMap<>(); 
        /* We want to read an existing ZIP File, so we set this to False */
        zip_properties.put("create", "false");
        /* Specify the encoding as UTF -8 */
        zip_properties.put("encoding", "UTF-8");        


2.URI to ZIP File


In this step, we create an URI object that maps to the physical ZIP file on disk that we want to read. We use the create method in java.net.URI class to accomplish this. The code segment is shown below:


        /* Specify the path to the ZIP File that you want to read as a File System */
        URI zip_disk = URI.create("jar:file:/my_zip_file.zip");


3.Create ZIP File System


Ok, we are now ready to read our input ZIP file as a ZIP File System in Java. You can invoke the factory methods available in java.nio.file.FileSystems to get a FileSystem object. You pass the HashMap and URI to the newFileSystem method as shown below:

        try (FileSystem zipfs = FileSystems.newFileSystem(zip_disk, zip_properties)) {


4.Extract from Archive to Disk


Once you have created a ZIP File System, it is very easy to extract an archive entry from the ZIP file to disk. You declare a Path object that points to a file inside the ZIP document. You also define a Path where you want to extract the document. From there, all you need to invoke is a simple copy method in java.nio.file.Files class and specify the source and target path. This is similar to copying files within unix or windows, and handling ZIP files in this fashion gives you a greater ease in managing such files. The code snippet to extract a file is shown below:


             /* Path inside ZIP File */ 
            Path pathInZipfile = Paths.get("tut1.docx");
            /* Path to extract the file to  */
            Path fileOutZip = Paths.get("C:/temp/tut1.docx");  
            /* Extract file to disk */
            Files.copy(pathInZipfile,fileOutZip); 


5.Complete Java NIO Program to Extract ZIP File – ZPFS


And that is all you have to do to extract a ZIP file in Java using ZPFS / NIO. You can try this program on any ZIP file of your interest, and you can even extract all the files inside a ZIP file through the same approach. The complete Java program is provided below:

import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.*;
public class ZPFSExtract {
    public static void main(String [] args) throws Exception {
        /* Define ZIP File System Properies in HashMap */    
        Map<String, String> zip_properties = new HashMap<>(); 
        /* We want to read an existing ZIP File, so we set this to False */
        zip_properties.put("create", "false");
        /* Specify the encoding as UTF -8 */
        zip_properties.put("encoding", "UTF-8");        
        /* Specify the path to the ZIP File that you want to read as a File System */
        URI zip_disk = URI.create("jar:file:/my_zip_file.zip");
        /* Create ZIP file System */
        try (FileSystem zipfs = FileSystems.newFileSystem(zip_disk, zip_properties)) {
             /* Path inside ZIP File */ 
            Path pathInZipfile = Paths.get("tut1.docx");
            /* Path to extract the file to  */
            Path fileOutZip = Paths.get("C:/temp/tut1.docx");  
            /* Extract file to disk */
            Files.copy(pathInZipfile,fileOutZip); 
        } 
    }
}

In the next tutorial, we will discuss how to delete a file entry within a ZIP file using Java using the same approach. Meanwhile, if you have any questions / comments on this approach, you can post us in reply on the same blog.

1 comment: