[Schmitzm-commits] r1302 - in trunk: src/schmitzm/io src_junit/schmitzm/io

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Nov 23 14:52:52 CET 2010


Author: alfonx
Date: 2010-11-23 14:52:52 +0100 (Tue, 23 Nov 2010)
New Revision: 1302

Modified:
   trunk/src/schmitzm/io/IOUtil.java
   trunk/src_junit/schmitzm/io/IOUtilTest.java
Log:
Added a new method fileFromJarFileUrl

Modified: trunk/src/schmitzm/io/IOUtil.java
===================================================================
--- trunk/src/schmitzm/io/IOUtil.java	2010-11-23 11:24:24 UTC (rev 1301)
+++ trunk/src/schmitzm/io/IOUtil.java	2010-11-23 13:52:52 UTC (rev 1302)
@@ -50,6 +50,8 @@
 import java.nio.channels.FileChannel;
 import java.nio.charset.Charset;
 import java.util.Enumeration;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipException;
 import java.util.zip.ZipFile;
@@ -1225,16 +1227,17 @@
 		return new File(url.getPath()).getName();
 	}
 
-	static public void unzipArchive(File archive, File outputDir) throws ZipException, IOException {
-			ZipFile zipfile = new ZipFile(archive);
-			for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
-				ZipEntry entry = (ZipEntry) e.nextElement();
-				unzipEntry(zipfile, entry, outputDir);
-			}
+	static public void unzipArchive(File archive, File outputDir)
+			throws ZipException, IOException {
+		ZipFile zipfile = new ZipFile(archive);
+		for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
+			ZipEntry entry = (ZipEntry) e.nextElement();
+			unzipEntry(zipfile, entry, outputDir);
+		}
 	}
 
-	static public void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir)
-			throws IOException {
+	static public void unzipEntry(ZipFile zipfile, ZipEntry entry,
+			File outputDir) throws IOException {
 
 		if (entry.isDirectory()) {
 			createDir(new File(outputDir, entry.getName()));
@@ -1246,7 +1249,7 @@
 			createDir(outputFile.getParentFile());
 		}
 
-//		LOGGER.debug("Extracting: " + entry);
+		// LOGGER.debug("Extracting: " + entry);
 		BufferedInputStream inputStream = new BufferedInputStream(
 				zipfile.getInputStream(entry));
 		BufferedOutputStream outputStream = new BufferedOutputStream(
@@ -1261,9 +1264,31 @@
 	}
 
 	static public void createDir(File dir) {
-//		LOGGER.debug("Creating dir " + dir.getName());
+		// LOGGER.debug("Creating dir " + dir.getName());
 		if (!dir.mkdirs())
 			throw new RuntimeException("Can not create dir " + dir);
 	}
 
+	/**
+	 * For an {@link URL} like
+	 * <code>jar:file:/home/stefan/.m2/repository/org/geopublishing/geopublisher/gpcore/1.7-SNAPSHOT/gpcore-1.7-SNAPSHOT-tests.jar!/atlases/ChartDemoAtlas/atlas.gpa</code>
+	 * returns
+	 * <code>/home/stefan/.m2/repository/org/geopublishing/geopublisher/gpcore/1.7-SNAPSHOT/gpcore-1.7-SNAPSHOT-tests.jar</code>
+	 * . <br/>
+	 * Returns <code>null</code> is the URL doesn't match the pattern.
+	 * 
+	 * @param url
+	 */
+	public static File getFileFromJarFileUrl(URL url) {
+		Matcher m = PATTERN_FileFromJarFileUrl.matcher(url.toString());
+		if (m.find())
+			return new File(m.group(1));
+		else
+			return null;
+
+	}
+
+	static final Pattern PATTERN_FileFromJarFileUrl = Pattern
+			.compile("jar:file:(.*)!.*");
+
 }

Modified: trunk/src_junit/schmitzm/io/IOUtilTest.java
===================================================================
--- trunk/src_junit/schmitzm/io/IOUtilTest.java	2010-11-23 11:24:24 UTC (rev 1301)
+++ trunk/src_junit/schmitzm/io/IOUtilTest.java	2010-11-23 13:52:52 UTC (rev 1302)
@@ -74,4 +74,12 @@
 		assertEquals("exterme",
 				IOUtil.getFilename(new URL("http://www.bahn.de/exterme")));
 	}
+
+	@Test
+	public void testGetFileFromJarFileUrl() throws MalformedURLException
+	{
+		URL u = new URL("jar:file:/home/stefan/.m2/repository/org/geopublishing/geopublisher/gpcore/1.7-SNAPSHOT/gpcore-1.7-SNAPSHOT-tests.jar!/atlases/ChartDemoAtlas/atlas.gpa");
+		
+		assertEquals("/home/stefan/.m2/repository/org/geopublishing/geopublisher/gpcore/1.7-SNAPSHOT/gpcore-1.7-SNAPSHOT-tests.jar", IOUtil.getFileFromJarFileUrl(u).toString());
+	}
 }



More information about the Schmitzm-commits mailing list