Java NIO ZIP File System Provider Example

ZIP File System Provider – Java NIO - Introduction


In this tutorial, we will discuss how to use ZIP File System Provider (ZFSP) which is capable of treating a ZIP file as a file system in Java and manipulate ZIP files using it with some Java examples. ZFSP can treat a ZIP file like a file system (i.e. a ZIP file can be managed as your C drive for example), and makes addition / deletion / creation of ZIP files a cakewalk. Not only this, you can also create your own custom file systems following this approach (More on this later). You need Java SE7 to work with the examples provided in this section. The different examples we will cover in this tutorial series are summarized below:

ZPFS in Java NIO - tutorials
ZPFS in Java NIO - tutorials



Let us start with the easiest of the examples first - Creating a ZIP file using ZFSP file system.

Create ZIP Files Using ZFSP / Java NIO


The high level steps involved in creating a ZIP file using ZFSP and adding some files into it are captured below:
Steps to create ZIP File using ZPFS in Java NIO
Steps to create ZIP File using ZPFS in Java NIO

We will discuss each of these steps in detail as we move on.

1.Define ZIP File System Properties


In this step, we create a HashMap in Java, and define the configurable properties for the ZIP file. The properties of interest are “create” and “encoding”. Since we have to create a new ZIP file, we set create to true and encoding to UTF-8. You can also set other encoding schemes if you want to. A sample code to define the HashMap is shown below:

        /* Define ZIP File System Properies in HashMap */    
        Map<String, String> zip_properties = new HashMap<>(); 
        /* set create to true if you want to create a new ZIP file */
        zip_properties.put("create", "true");
        /* specify encoding to UTF-8 */
        zip_properties.put("encoding", "UTF-8");


2.Configure Physical ZIP File


We use the URI class defined in java.net.URI to point to a physical file on disk. This should not be new to you, and here is the Java code segment to do this;

        /* Locate File on disk for creation */
        URI zip_disk = URI.create("jar:file:/my_zip_file.zip");        


3.Create ZIP File System


You can create a new File System by using the method newFileSystem factory method defined in java.nio.file.FileSystems. You pass the URI and HashMap created in the steps earlier to this method and it returns an object of type java.nio.file.FileSystem which in our case is a ZIP File System. The Java code to create the File System is shown below:

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


4.Copy Files to ZIP Archive


Now that you have created a ZIP File System, you can easily copy (i.e. add ) files to archive, by using methods available in Path and Files class. We will add a simple docx file to the archive as shown below, you can adopt the same approaches to add any file.

            /* Path to source file */   
            Path file_to_zip = Paths.get("tut1.docx");
            /* Path inside ZIP File */
            Path pathInZipfile = zipfs.getPath("tut1.docx");          
            /* Add file to archive */
            Files.copy(file_to_zip,pathInZipfile); 


Complete Program – Create ZIP File using ZPFS in Java NIO


The complete Java program to create a ZIP file using ZPFS is shown below: (Note that we are using new try with resources syntax in this example)

import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.*;
public class ZPFSCreate {
    public static void main(String [] args) throws Exception {
        /* Define ZIP File System Properies in HashMap */    
        Map<String, String> zip_properties = new HashMap<>(); 
        /* set create to true if you want to create a new ZIP file */
        zip_properties.put("create", "true");
        /* specify encoding to UTF-8 */
        zip_properties.put("encoding", "UTF-8");        
        /* Locate File on disk for creation */
        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 to source file */   
            Path file_to_zip = Paths.get("tut1.docx");
            /* Path inside ZIP File */
            Path pathInZipfile = zipfs.getPath("tut1.docx");          
            /* Add file to archive */
            Files.copy(file_to_zip,pathInZipfile); 
        } 
    }
}


You will get a ZIP file created in your file system with a single document in it as per the example code provided above. You can try this example yourself and post a comment if you have any questions.

In the next post, we will examine how to read an archive and extract files from it using ZPFS. Stay connected.

1 comment:

  1. Hi,
    While creating Zip file in local its working fine.But in linux environment I am getting below exception.Can you please help me.

    java.lang.IllegalArgumentException: URI has an authority component
    at sun.nio.fs.UnixUriUtils.fromUri(UnixUriUtils.java:53) ~[na:1.8.0_102]
    at sun.nio.fs.UnixFileSystemProvider.getPath(UnixFileSystemProvider.java:98) ~[na:1.8.0_102]
    at java.nio.file.Paths.get(Paths.java:138) ~[na:1.8.0_102]
    at com.sun.nio.zipfs.ZipFileSystemProvider.uriToPath(ZipFileSystemProvider.java:85) ~[zipfs.jar:1.8.0_102]
    at com.sun.nio.zipfs.ZipFileSystemProvider.newFileSystem(ZipFileSystemProvider.java:107) ~[zipfs.jar:1.8.0_102]
    at java.nio.file.FileSystems.newFileSystem(FileSystems.java:326) ~[na:1.8.0_102]
    at java.nio.file.FileSystems.newFileSystem(FileSystems.java:276) ~[na:1.8.0_102]

    ReplyDelete