Search ZIP File with Regex - Java NIO Example

In this example, we will discuss how to use regular expressions to search and find entries inside a ZIP file using a Java program. This post can be considered as an extension to the earlier post on Glob pattern based search on ZIP Files using Java.We will be using the same approach as outlined in the Glob tutorial to do a regex based search. The differences alone will be discussed in this tutorial. Readers are advised to go through the Glob tutorial as it contains in depth information on how this code works.

Regex Search ZIP File Entries:

The complete Java program to do a regex based search on ZIP file entries using Java NIO / ZPFS is provided below:


import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.*;
import java.io.IOException;
import java.util.*;
import java.net.URI;

class regexSearch implements FileVisitor {
   
    private final PathMatcher matcher;
  
    public regexSearch(String searchPattern) {
       matcher = FileSystems.getDefault().getPathMatcher("regex:" + searchPattern);
    }
    
    @Override
    public FileVisitResult visitFile(Object file, BasicFileAttributes attrs)
    throws IOException {
            Path my_file = (Path) file;
            Path name = my_file.getFileName();
            if (name != null && matcher.matches(name)) {
                    System.out.println("Searched file was found: " + name + " in " + my_file.toRealPath().toString());
            }
            return FileVisitResult.CONTINUE;         
    }
    /* We don't use these, so just override them */
    @Override
    public FileVisitResult postVisitDirectory(Object dir, IOException exc)
    throws IOException {        
        return FileVisitResult.CONTINUE;
    }
    
    @Override
    public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs)
    throws IOException {
        return FileVisitResult.CONTINUE;
    }
    @Override
    public FileVisitResult visitFileFailed(Object file, IOException exc)
    throws IOException {        
        return FileVisitResult.CONTINUE;
    }
    
    
    public static void main(String args[]) throws IOException {
        
        /* A sample regex pattern for testing */
        String searchPattern="[a-zA-Z]";
        regexSearch walk = new regexSearch(searchPattern);        
        
        /* Define ZIP File System Properies in HashMap */
        Map<String, String> zip_properties = new HashMap<>();
        zip_properties.put("create", "false");
        URI zip_disk = URI.create("jar:file:/sp.zip");
        FileSystem zipfs = FileSystems.newFileSystem(zip_disk, zip_properties);
        
        Iterable<Path> dirs = zipfs.getRootDirectories();
        /* For every root folder inside the ZIP archive */
        for (Path root : dirs) {                
                Files.walkFileTree(root, walk);            
        }        
    }    
}


No comments: