欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

在ZIP文件中找到指定的文件,ZIP文件指定文件,/**在zip文件中找到

来源: javaer 分享于  点击 4339 次 点评:197

在ZIP文件中找到指定的文件,ZIP文件指定文件,/**在zip文件中找到


/**在zip文件中找到指定文件*使用了ZipFile 和 ZipEntry*/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();                /*                 * 这里用indexOf比较而不用equals或者equalsIgnoreCase                 * 是因为这个zip文件可能含有文件夹那么entry.getName()                 * 返回的就是一个路径,所以用indexOf                 *                  * 此外,这里可能在不同的路径里面有相同的文件名                 */                if(entry.getName().toLowerCase().indexOf(searchFileName) != -1)                {                    found = true;                    System.out.println("Found " + entry.getName());                    /*                     * 如果你想返回匹配的第一个文件,                     * 把下面break注释打开                     */                    //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);         }    }}/**运行输出可能如下:*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*///该片段来自于http://byrx.net
相关栏目:

用户点评