Rename ZIP entries with NIO / ZPFS - Java Example

In this example, we will discuss how to rename entries inside a ZIP file in Java using Zip File System Provider (ZPFS) implementation. ZPFS concept is linked to Java NIO and you will wonder how easily this task can be accomplished with very less coding. Without ZPFS, you will have to extract the entire archive, work harder to rename individual files and then create another ZIP file completely. This gets really complicated, if the size of your zip file is huge or if the number of entries inside the file is massive.  ZPFS overcomes all these hassles by treating your input file as a file system. With this introduction, we provide a step by step guide below for this tutorial:

Create Input ZIP File:

In this step, we create an input ZIP File that we will use inside our Java program. This file has an entry “dest.sql” which we will rename to “dest_2.sql” shortly. A screenshot of the input file is shown below:

Input ZIP File - Rename ZIP Entries in Java
Input ZIP File - Rename ZIP Entries in Java
Access ZIP File – Create ZIP File System

We have discussed this step in our tutorial on adding files to existing ZIP archive. We need to create a ZIP File System for the file that we want to act on. The sample code is shown below:


        /* 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 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)) {


Read ZIP Entry to Rename

Once you have created a ZPFS, all you need to do is to create a Path object that points to the file that you want to rename. (in our case, we want to rename dest.sql). The Java code snippet is provided below:

            /* Access file that needs to be renamed */
            Path pathInZipfile = zipfs.getPath("dest.sql");


Specify new File Name for the ZIP Entry

You will need to specify the new file name through a Path object as shown below:


            /* Specify new file name */
            Path renamedZipEntry = zipfs.getPath("dest_4.sql");

Execute Rename:

In this step, we use the “move” method in java.nio.files.Files class and specify the copyOption as ATOMIC_MOVE, to do the rename. It  is just a one line code and that will rename the ZIP entry. Bang!


            /* Execute rename */
            Files.move(pathInZipfile,renamedZipEntry,StandardCopyOption.ATOMIC_MOVE);

Complete Java Program to rename ZIP Entries:

Putting it all together, the complete Java program that uses ZPFS / NIO to rename ZIP entries is shown below:

import java.util.*;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.*;
import java.nio.file.StandardCopyOption;
public class ZPFSRename {
    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 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)) {
            /* Access file that needs to be renamed */
            Path pathInZipfile = zipfs.getPath("dest.sql");
            /* Specify new file name */
            Path renamedZipEntry = zipfs.getPath("dest_4.sql");
            System.out.println("About to rename an entry from ZIP File" + pathInZipfile.toUri() ); 
            /* Execute rename */
            Files.move(pathInZipfile,renamedZipEntry,StandardCopyOption.ATOMIC_MOVE);
            System.out.println("File successfully renamed");   
        } 
    }
}

The output ZIP file screenshot is provided below. You can see that the file is renamed successfully

Output ZIP File with Renamed ZIP Entries
Output ZIP File with Renamed ZIP Entries
That completes the Java example to rename ZIP file entries using NIO / ZPFS. As you can see, we have not extracted the ZIP file at all! ZPFS makes it really easy to manipulate ZIP archives.

2 comments: