Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-14 of 14 total

find files in zip, jar, war, ear and nested archives

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.

import java.io.*;
import java.util.*;
import java.util.zip.*;

public class Finder {
	public static void main(String[] args) throws Exception {
		String path = args[0];
		final String expr = args[1];

		List l = new ArrayList();
		findFile(new File(path), new P() {
			public boolean accept(String t) {
				return t.matches(expr) || isZip(t);
			}
		}, l);

		List r = new ArrayList();
		for (Iterator it = l.iterator(); it.hasNext();) {
			File f = (File) it.next();
			String fn = f + "";
			if (fn.matches(expr)) r.add(fn);
			if (isZip(f.getName())) {
				findZip(fn, new FileInputStream(f), new P() {
					public boolean accept(String t) {
						return t.matches(expr);
					}
				}, r);
			}
		}

		for (Iterator it = r.iterator(); it.hasNext();) {
			System.out.println(it.next());
		}
	}

	static void findFile(File f, P p, List r) {
		if (f.isDirectory()) {
			File[] files = f.listFiles();
			for (int i = 0; i < files.length; i++) findFile(files[i], p, r);
		} else if (p.accept(f + "")) {
			r.add(f);
		}
	}

	static void findZip(String f, InputStream in, P p, List r) throws IOException {
		ZipInputStream zin = new ZipInputStream(in);

		ZipEntry en;
		while ((en = zin.getNextEntry()) != null) {
			if (p.accept(en.getName())) r.add(f + "!" + en);
			if (isZip(en.getName())) findZip(f + "!" + en, zin, p, r);
		}
	}

	static String[] ZIP_EXTENSIONS = { ".zip", ".jar", ".war", ".ear" };

	static boolean isZip(String t) {
		for (int i = 0; i < ZIP_EXTENSIONS.length; i++) {
			if (t.endsWith(ZIP_EXTENSIONS[i])) {
				return true;
			}
		}
		return false;
	}

	static interface P {
		public boolean accept(String t);
	}
}



Files are not properly closed! Don't use this class in a long running application or properly close all streams.

Get HTML list of Array Keys

Sometimes I find it useful having an overview about the keys an array has; PHP function Get_Array_Keys_UL will create an nested HTML Unordered List string of the keys of a given array. Makes best sense with literal keys, e.g. $_POST:
/**
 * Creates recursively a nested HTML UL of array keys.
 * @param array $array Array
 * @return string Nested UL string
 */
function Get_Array_Keys_UL($array=array()) {
 $recursion=__FUNCTION__;
 if (empty($array)) return '';
 $out='<ul>'."\n";
 foreach ($array as $key => $elem)
   $out .= '<li>'.$key.$recursion($elem).'</li>'."\n";
 $out .= '</ul>'."\n";  
 return $out;
}


Use like this:
$ul=Get_Array_Keys_UL($_POST);
echo $ul;

Get (recursive) directory listing in array #2

XoloX's cute function directoryToArray($directory, $recursive) will become "real recursive" by adding the recursion's directory content to array $array_items:
if($recursive) { 
  #directoryToArray($directory. "/" . $file, true); 
  $array_items=array_merge($array_items,directoryToArray($directory. "/" . $file, $recursive));
} 

Get (recursive) directory listing in array

Updated to include drwitt's fix. Shame on me! Well, as long as I learn from my mistakes :)

Get a (recursive) directory listing in an array. Directory's are included in this list. If this behavior is not wanted, remove the two lines:

$file = $directory . "/" . $file;
$array_items[] = preg_replace("/\/\//si", "/", $file);


To get a non recursive directory listing, use it like this:

$files = directoryToArray("./", false);


And to get a recursive directory listing, use it like this:

$files = directoryToArray("./", true);


Once you have an array of files, you can iterate over the directories/files like this:

echo '<ul>';

foreach ($files as $file) {
	echo '<li>' . $file . '</li>';
}

echo '</ul>';


or like this:

echo '<ul>';

for ($i = 0; $i <= count($files); $i++) {
	echo '<li>' . $files[$i] . '</li>';
}

echo '</ul>';


Code:
function directoryToArray($directory, $recursive) {
	$array_items = array();
	if ($handle = opendir($directory)) {
		while (false !== ($file = readdir($handle))) {
			if ($file != "." && $file != "..") {
				if (is_dir($directory. "/" . $file)) {
					if($recursive) {
						$array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
					}
					$file = $directory . "/" . $file;
					$array_items[] = preg_replace("/\/\//si", "/", $file);
				} else {
					$file = $directory . "/" . $file;
					$array_items[] = preg_replace("/\/\//si", "/", $file);
				}
			}
		}
		closedir($handle);
	}
	return $array_items;
}


Hope someone can use it!
« Newer Snippets
Older Snippets »
Showing 11-14 of 14 total