Title: [1274] trunk/web/web-io: JBEHAVE-181: Refactored ZipFileArchiver to use commons-compress
Revision
1274
Author
mauro
Date
2009-09-24 12:18:02 -0500 (Thu, 24 Sep 2009)

Log Message

JBEHAVE-181: Refactored ZipFileArchiver to use commons-compress

Modified Paths

Diff

Modified: trunk/web/web-io/pom.xml (1273 => 1274)

--- trunk/web/web-io/pom.xml	2009-09-21 23:02:05 UTC (rev 1273)
+++ trunk/web/web-io/pom.xml	2009-09-24 17:18:02 UTC (rev 1274)
@@ -25,5 +25,10 @@
       <artifactId>commons-fileupload</artifactId>
       <version>1.2</version>
     </dependency>
+    <dependency>
+      <groupId>org.apache.commons</groupId>
+      <artifactId>commons-compress</artifactId>
+      <version>1.0</version>
+    </dependency>
   </dependencies>
 </project>

Modified: trunk/web/web-io/src/main/java/org/jbehave/web/io/ZipFileArchiver.java (1273 => 1274)

--- trunk/web/web-io/src/main/java/org/jbehave/web/io/ZipFileArchiver.java	2009-09-21 23:02:05 UTC (rev 1273)
+++ trunk/web/web-io/src/main/java/org/jbehave/web/io/ZipFileArchiver.java	2009-09-24 17:18:02 UTC (rev 1274)
@@ -2,24 +2,25 @@
 
 import static org.apache.commons.lang.StringUtils.removeEnd;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Enumeration;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
 
+import org.apache.commons.compress.archivers.ArchiveInputStream;
+import org.apache.commons.compress.archivers.ArchiveStreamFactory;
+import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipFile;
 import org.apache.commons.io.IOUtils;
 
-// unarchive() impl adapted from http://piotrga.wordpress.com/2008/05/07/how-to-unzip-archive-in-java/
-// works as tested, but should be replaced with commons-compress (http://commons.apache.org/sandbox/compress) when released
 public class ZipFileArchiver implements FileArchiver {
 
 	private static final String ZIP = ".zip";
+	private ArchiveStreamFactory factory = new ArchiveStreamFactory();
 
 	public boolean isArchive(File file) {
 		return file.getName().endsWith(ZIP);
@@ -31,18 +32,21 @@
 
 	public void unarchive(File archive, File outputDir) {
 		try {
+			ArchiveInputStream in = factory.createArchiveInputStream("zip",
+					new FileInputStream(archive));
 			ZipFile zipfile = new ZipFile(archive);
-			for (Enumeration<?> e = zipfile.entries(); e.hasMoreElements();) {
-				ZipEntry entry = (ZipEntry) e.nextElement();
-				unzipEntry(zipfile, entry, outputDir);
+			for (Enumeration<?> e = zipfile.getEntries(); e.hasMoreElements();) {
+				ZipArchiveEntry entry = (ZipArchiveEntry) e.nextElement();
+				unzipEntry(entry, in, outputDir);
 			}
+			in.close();
 		} catch (Exception e) {
 			throw new FileUnarchiveFailedException(archive, outputDir, e);
 		}
 	}
 
-	private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir)
-			throws IOException {
+	private void unzipEntry(ZipArchiveEntry entry, InputStream in,
+			File outputDir) throws IOException {
 
 		if (entry.isDirectory()) {
 			createDir(new File(outputDir, entry.getName()));
@@ -53,32 +57,28 @@
 		if (!outputFile.getParentFile().exists()) {
 			createDir(outputFile.getParentFile());
 		}
+		
+		copy(entry, in, outputDir);
 
-		// Copy entry to output
-		InputStream in = zipfile.getInputStream(entry);
-		copy(in, outputFile);
 	}
 
-	private void copy(InputStream inputStream, File outputFile)
-			throws IOException {
-		InputStream in = new BufferedInputStream(inputStream);
-		OutputStream out = new BufferedOutputStream(new FileOutputStream(
-				outputFile));
-
-		try {
-			IOUtils.copy(in, out);
-		} finally {
-			out.close();
-			in.close();
+	private void createDir(File dir) throws IOException {
+		if (dir.exists()){
+			return;
 		}
-	}
-
-	private void createDir(File dir) throws IOException {
 		if (!dir.mkdirs()) {
 			throw new IOException("Failed to create dir " + dir);
 		}
 	}
 
+	private void copy(ZipArchiveEntry entry, InputStream in, File outputDir)
+			throws FileNotFoundException, IOException {
+		File entryFile = new File(outputDir, entry.getName());
+		OutputStream out = new FileOutputStream(entryFile);
+		IOUtils.copy(in, out);
+		out.close();
+	}
+
 	@SuppressWarnings("serial")
 	public static final class FileUnarchiveFailedException extends
 			RuntimeException {

Modified: trunk/web/web-io/src/test/java/org/jbehave/web/io/ZipFileArchiverTest.java (1273 => 1274)

--- trunk/web/web-io/src/test/java/org/jbehave/web/io/ZipFileArchiverTest.java	2009-09-21 23:02:05 UTC (rev 1273)
+++ trunk/web/web-io/src/test/java/org/jbehave/web/io/ZipFileArchiverTest.java	2009-09-24 17:18:02 UTC (rev 1274)
@@ -27,22 +27,22 @@
 		File zip = resourceFile("dir1.zip");
 		assertTrue(archiver.isArchive(zip));
 		archiver.unarchive(zip, dir);
-		assertFilesUnarchived(asList("dir1", "dir1/file1.txt", "dir1/subdir1", "dir1/subdir1/subfile1.txt"));
+		assertFilesUnarchived(asList("dir1", "dir1/file1.txt", "dir1/subdir1",
+				"dir1/subdir1/subfile1.txt"));
 	}
-	
+
 	private void assertFilesUnarchived(List<String> paths) {
-		for ( String path : paths ){
+		for (String path : paths) {
 			assertFileExists(path);
 		}
 	}
 
 	private void assertFileExists(String path) {
 		assertTrue(new File(dir, path).exists());
-		
 	}
 
-	private File resourceFile( String path ) {
-		return new File(getClass().getClassLoader().getResource(path).getFile());		
+	private File resourceFile(String path) {
+		return new File(getClass().getClassLoader().getResource(path).getFile());
 	}
-	
+
 }
\ No newline at end of file


To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to