<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: recursive code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 07:40:09 GMT</pubDate>
    <description>DZone Snippets: recursive code</description>
    <item>
      <title>find files in zip, jar, war, ear and nested archives</title>
      <link>http://snippets.dzone.com/posts/show/356</link>
      <description>The following java class takes two arguments; a directory name and a regular expression.  The directory is searched for files matching the regexp.  Zip files (and zip-alikes) are also searched including nested zips.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import java.io.*;&lt;br /&gt;import java.util.*;&lt;br /&gt;import java.util.zip.*;&lt;br /&gt;&lt;br /&gt;public class Finder {&lt;br /&gt;	public static void main(String[] args) throws Exception {&lt;br /&gt;		String path = args[0];&lt;br /&gt;		final String expr = args[1];&lt;br /&gt;&lt;br /&gt;		List l = new ArrayList();&lt;br /&gt;		findFile(new File(path), new P() {&lt;br /&gt;			public boolean accept(String t) {&lt;br /&gt;				return t.matches(expr) || isZip(t);&lt;br /&gt;			}&lt;br /&gt;		}, l);&lt;br /&gt;&lt;br /&gt;		List r = new ArrayList();&lt;br /&gt;		for (Iterator it = l.iterator(); it.hasNext();) {&lt;br /&gt;			File f = (File) it.next();&lt;br /&gt;			String fn = f + "";&lt;br /&gt;			if (fn.matches(expr)) r.add(fn);&lt;br /&gt;			if (isZip(f.getName())) {&lt;br /&gt;				findZip(fn, new FileInputStream(f), new P() {&lt;br /&gt;					public boolean accept(String t) {&lt;br /&gt;						return t.matches(expr);&lt;br /&gt;					}&lt;br /&gt;				}, r);&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		for (Iterator it = r.iterator(); it.hasNext();) {&lt;br /&gt;			System.out.println(it.next());&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	static void findFile(File f, P p, List r) {&lt;br /&gt;		if (f.isDirectory()) {&lt;br /&gt;			File[] files = f.listFiles();&lt;br /&gt;			for (int i = 0; i &lt; files.length; i++) findFile(files[i], p, r);&lt;br /&gt;		} else if (p.accept(f + "")) {&lt;br /&gt;			r.add(f);&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	static void findZip(String f, InputStream in, P p, List r) throws IOException {&lt;br /&gt;		ZipInputStream zin = new ZipInputStream(in);&lt;br /&gt;&lt;br /&gt;		ZipEntry en;&lt;br /&gt;		while ((en = zin.getNextEntry()) != null) {&lt;br /&gt;			if (p.accept(en.getName())) r.add(f + "!" + en);&lt;br /&gt;			if (isZip(en.getName())) findZip(f + "!" + en, zin, p, r);&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	static String[] ZIP_EXTENSIONS = { ".zip", ".jar", ".war", ".ear" };&lt;br /&gt;&lt;br /&gt;	static boolean isZip(String t) {&lt;br /&gt;		for (int i = 0; i &lt; ZIP_EXTENSIONS.length; i++) {&lt;br /&gt;			if (t.endsWith(ZIP_EXTENSIONS[i])) {&lt;br /&gt;				return true;&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		return false;&lt;br /&gt;	}&lt;br /&gt;&lt;br /&gt;	static interface P {&lt;br /&gt;		public boolean accept(String t);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;em&gt;Files are not properly closed!  Don't use this class in a long running application or properly close all streams.&lt;/em&gt;</description>
      <pubDate>Tue, 31 May 2005 22:47:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/356</guid>
      <author>remvee (Remco van 't Veer)</author>
    </item>
  </channel>
</rss>
