Find File in a Zip File - Java File Path IO

Java examples for File Path IO:Zip File

Description

Find File in a Zip File

Demo Code

 
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
 
public class Main {
       //from   w w w .java  2  s.c  o m
        public static void main(String args[])
        {
                 try
                 {
                        ZipFile sourceZipFile = new ZipFile("c:/Demo.zip");
                        String searchFileName = "readme.txt";
                        Enumeration e = sourceZipFile.entries();
                        boolean found = false;
                       
                        System.out.println("Trying to search " + searchFileName + " in " + sourceZipFile.getName());
                       
                        while(e.hasMoreElements())
                        {
                                ZipEntry entry = (ZipEntry)e.nextElement();
                               
                                if(entry.getName().toLowerCase().indexOf(searchFileName) != -1)
                                {
                                        found = true;
                                        System.out.println("Found " + entry.getName());
                                }
                        }
                       
                        if(found == false)
                        {
                                System.out.println("File :" + searchFileName + " Not Found Inside Zip File: " + sourceZipFile.getName());
                        }
                        sourceZipFile.close();
       
                 }
                 catch(IOException ioe)
                 {
                        System.out.println("Error opening zip file" + ioe);
                 }
        }
}

Result


Related Tutorials