skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Monday, September 10, 2012

Find File in a Zip File Example

Posted by Admin at 1:20 PM – 0 comments
 
Find File in a Zip File Example.
This Java example shows how to find a particular file
in a zip file using ZipFile and ZipEntry Java classes.

    import java.io.IOException;
    import java.util.Enumeration;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
     
    public class FindFileInZipFile {
           
            public static void main(String args[])
            {
                     try
                     {
                            //open the source zip file
                            ZipFile sourceZipFile = new ZipFile("c:/SearchDemo.zip");
                           
                            //File we want to search for inside the zip file
                            String searchFileName = "readme.txt";
     
                            //get all entries                      
                            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();
                                   
                                    /*
                                     * Here, normal compare would not work.
                                     *
                                     * Because zip might contain directories so the entry name will not
                                     * match extactly with the file name we want to search.
                                     *
                                     * Additionally, there might be more than one file with the same
                                     * name in different directories inside the zip archive.
                                     *
                                     * So approch here is to search using indexOf and not using
                                     * equals or equalsIgnoreCase methods.
                                     */
                                    if(entry.getName().toLowerCase().indexOf(searchFileName) != -1)
                                    {
                                            found = true;
                                            System.out.println("Found " + entry.getName());
                                           
                                            /*
                                             * if you want to search only first instance, uncomment the
                                             * following break statement.
                                             */
                                             
                                            //break;                       
                                    }
                            }
                           
                            if(found == false)
                            {
                                    System.out.println("File :" + searchFileName + " Not Found Inside Zip File: " + sourceZipFile.getName());
                            }
     
                            //close the zip file
                            sourceZipFile.close();
           
                     }
                     catch(IOException ioe)
                     {
                            System.out.println("Error opening zip file" + ioe);
                     }
            }
     
     
    }
     


Output of Above Java Program
Trying to search readme.txt in c:\SearchDemo.zip
Found xampplite/htdocs/drupal58/sites/all/README.txt
Found xampplite/htdocs/fun610/modules/README.txt
Found xampplite/htdocs/demo/sites/all/README.txt
Found xampplite/htdocs/fun610/themes/README.txt
Found xampplite/htdocs/knowledge/sites/all/README.txt

Labels: Zip File

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger