Author: dgolovin
Date: 2009-09-08 16:30:44 -0400 (Tue, 08 Sep 2009)
New Revision: 17479
Added:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/DefaultZipEntryVisitor.java
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/IZipEntryVisitor.java
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/UnzipOperation.java
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/ZipArchive.java
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/UnzipOperationTest.java
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipAllTests.java
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipArchiveTest.java
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipBaseTest.java
Modified:
trunk/common/plugins/org.jboss.tools.common/META-INF/MANIFEST.MF
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/test/CommonAllTests.java
Log:
https://jira.jboss.org/jira/browse/JBIDE-3628
Unzip support is added. It is required to give a way to extract templates from seam
plug-in to use them in seam faceted project.
All zip utils are in org.jboss.tools.common.zip package and have 98% coverage by JUnit
tests in org.jboss.tools.common.zip.test package. JUnit tests included in common.test test
suite.
Modified: trunk/common/plugins/org.jboss.tools.common/META-INF/MANIFEST.MF
===================================================================
--- trunk/common/plugins/org.jboss.tools.common/META-INF/MANIFEST.MF 2009-09-08 16:05:15
UTC (rev 17478)
+++ trunk/common/plugins/org.jboss.tools.common/META-INF/MANIFEST.MF 2009-09-08 20:30:44
UTC (rev 17479)
@@ -145,5 +145,6 @@
org.jboss.tools.common.reporting,
org.jboss.tools.common.text,
org.jboss.tools.common.util,
- org.jboss.tools.common.xml
+ org.jboss.tools.common.xml,
+ org.jboss.tools.common.zip
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Added:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/DefaultZipEntryVisitor.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/DefaultZipEntryVisitor.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/DefaultZipEntryVisitor.java 2009-09-08
20:30:44 UTC (rev 17479)
@@ -0,0 +1,14 @@
+package org.jboss.tools.common.zip;
+
+import java.io.IOException;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+public class DefaultZipEntryVisitor implements IZipEntryVisitor {
+
+ public void visiteDirectoryEntry(ZipFile zipFIle, ZipEntry dir) throws IOException {
+ }
+
+ public void visiteFileEntry(ZipFile zipFile, ZipEntry file) throws IOException {
+ }
+}
Property changes on:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/DefaultZipEntryVisitor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/IZipEntryVisitor.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/IZipEntryVisitor.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/IZipEntryVisitor.java 2009-09-08
20:30:44 UTC (rev 17479)
@@ -0,0 +1,10 @@
+package org.jboss.tools.common.zip;
+
+import java.io.IOException;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+public interface IZipEntryVisitor {
+ void visiteDirectoryEntry (ZipFile zipFIle, ZipEntry dir) throws IOException;
+ void visiteFileEntry (ZipFile zipFile, ZipEntry file) throws IOException;
+}
Property changes on:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/IZipEntryVisitor.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/UnzipOperation.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/UnzipOperation.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/UnzipOperation.java 2009-09-08
20:30:44 UTC (rev 17479)
@@ -0,0 +1,93 @@
+package org.jboss.tools.common.zip;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+public class UnzipOperation {
+
+ File zipFile;
+
+ public static final int BUFFER_SIZE = 1024*4;
+
+ public UnzipOperation(String zipFile) {
+ this(new File(zipFile));
+ }
+
+ public UnzipOperation(File zipFile) {
+ this.zipFile = zipFile;
+ }
+
+ public void execute(File destination) throws IOException {
+ ZipArchive archive = new ZipArchive(zipFile);
+ archive.acceptVisitor(new UnzipEntryVisitor(destination));
+ }
+
+ public void execute(String destination) throws IOException {
+ execute(new File(destination));
+ }
+
+ public void execute(File destination,String filter) throws IOException {
+ ZipArchive archive = new ZipArchive(zipFile);
+ archive.acceptVisitor(new FilteredZipEntryVisitor(filter,new
UnzipEntryVisitor(destination)));
+ }
+
+ public void execute(String destination,String filter) throws IOException {
+ execute(new File(destination,filter));
+ }
+
+ public static class FilteredZipEntryVisitor implements IZipEntryVisitor{
+
+ private String filter;
+ private IZipEntryVisitor visitor;
+
+ public FilteredZipEntryVisitor(String filter, IZipEntryVisitor visitor) {
+ this.filter = filter;
+ this.visitor = visitor;
+ }
+
+ public void visiteDirectoryEntry(ZipFile zipFile, ZipEntry dir) throws IOException {
+ if(dir.getName().matches(filter)) {
+ visitor.visiteDirectoryEntry(zipFile, dir);
+ }
+ }
+
+ public void visiteFileEntry(ZipFile zipFile, ZipEntry file) throws IOException {
+ if(file.getName().matches(filter)) {
+ visitor.visiteFileEntry(zipFile, file);
+ }
+ }
+ }
+
+ public static class UnzipEntryVisitor implements IZipEntryVisitor {
+
+ private File destination;
+
+ public UnzipEntryVisitor(File destination) {
+ this.destination = destination;
+ }
+
+ public void visiteDirectoryEntry(ZipFile zipFIle, ZipEntry dir) throws IOException {
+ File entryDir = new File(destination,dir.getName());
+ entryDir.mkdirs();
+ }
+
+ public void visiteFileEntry(ZipFile zipFile, ZipEntry file) throws IOException {
+ File outputFile = new File(destination,file.getName());
+ outputFile.getParentFile().mkdirs();
+ outputFile.createNewFile();
+ BufferedOutputStream out = new BufferedOutputStream(new
FileOutputStream(outputFile),BUFFER_SIZE);
+ BufferedInputStream in = new BufferedInputStream(zipFile.getInputStream(file));
+ byte[] buff = new byte[BUFFER_SIZE];
+ int n = -1;
+ while((n = in.read(buff,0,buff.length))>-1) {
+ out.write(buff, 0, n);
+ }
+ out.flush();
+ }
+ }
+}
Property changes on:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/UnzipOperation.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/ZipArchive.java
===================================================================
---
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/ZipArchive.java
(rev 0)
+++
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/ZipArchive.java 2009-09-08
20:30:44 UTC (rev 17479)
@@ -0,0 +1,52 @@
+package org.jboss.tools.common.zip;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+public class ZipArchive {
+
+ File file = null;
+
+ public ZipArchive(String file) {
+ this(new File(file));
+ }
+
+ public ZipArchive(File file) {
+ this.file = file;
+ }
+
+ public void acceptVisitor(IZipEntryVisitor visitor) throws IOException{
+ ZipFile zipFile = null;
+ zipFile = new ZipFile(this.file);
+ acceptVisitor(zipFile, visitor);
+ }
+
+ public static void acceptVisitor(ZipFile zipFile, IZipEntryVisitor visitor) throws
IOException {
+ try {
+ Enumeration<? extends ZipEntry> entries = zipFile.entries();
+ while(entries.hasMoreElements()) {
+ ZipEntry entry = entries.nextElement();
+ if(entry.isDirectory()) {
+ visitor.visiteDirectoryEntry(zipFile, entry);
+ } else {
+ visitor.visiteFileEntry(zipFile, entry);
+ }
+ }
+ } finally {
+ if(zipFile!=null) {
+ try {
+ zipFile.close();
+ } catch (IOException e) {
+ // Nothing to do with that
+ }
+ }
+ }
+ }
+
+ public String getAbsolutePath() {
+ return file.getAbsolutePath();
+ }
+}
Property changes on:
trunk/common/plugins/org.jboss.tools.common/src/org/jboss/tools/common/zip/ZipArchive.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified:
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/test/CommonAllTests.java
===================================================================
---
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/test/CommonAllTests.java 2009-09-08
16:05:15 UTC (rev 17478)
+++
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/test/CommonAllTests.java 2009-09-08
20:30:44 UTC (rev 17479)
@@ -18,6 +18,7 @@
import org.jboss.tools.common.util.test.HttpUtilTest;
import org.jboss.tools.common.xml.test.SAXValidatorTest;
import org.jboss.tools.common.xml.test.XMLUtilitiesTest;
+import org.jboss.tools.common.zip.test.ZipAllTests;
public class CommonAllTests extends TestCase {
public static final String PLUGIN_ID = "org.jboss.tools.common.test";
@@ -30,6 +31,7 @@
suite.addTestSuite(SAXValidatorTest.class);
suite.addTestSuite(ProblemReportingHelperTest.class);
suite.addTestSuite(CommonPluginTest.class);
+ suite.addTestSuite(ZipAllTests.class);
return suite;
}
}
\ No newline at end of file
Added:
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/UnzipOperationTest.java
===================================================================
---
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/UnzipOperationTest.java
(rev 0)
+++
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/UnzipOperationTest.java 2009-09-08
20:30:44 UTC (rev 17479)
@@ -0,0 +1,44 @@
+package org.jboss.tools.common.zip.test;
+
+import java.io.File;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.Platform;
+import org.jboss.tools.common.zip.UnzipOperation;
+
+public class UnzipOperationTest extends ZipBaseTest {
+
+ private static final String ORG_PACKAGE_FILTER = "org.*";
+
+ public void testUnzipOperationExecuteFile() throws IOException {
+ UnzipOperation unzip = new UnzipOperation(getZip().getAbsolutePath());
+ File destination = new File(getTemp(),Long.toString(System.currentTimeMillis()));
+ destination.mkdirs();
+ unzip.execute(destination);
+ }
+
+ public void testUnzipOperationExecuteFileString() throws IOException {
+ UnzipOperation unzip = new UnzipOperation(getZip().getAbsolutePath());
+ File destination = new File(getTemp(),Long.toString(System.currentTimeMillis()));
+ destination.mkdirs();
+ unzip.execute(destination,ORG_PACKAGE_FILTER);
+ }
+
+ public void testUnzipOperationExecuteString() throws IOException {
+ UnzipOperation unzip = new UnzipOperation(getZip());
+ File destination = new File(getTemp(),Long.toString(System.currentTimeMillis()));
+ destination.mkdirs();
+ unzip.execute(destination.getAbsolutePath());
+ }
+
+ public void testUnzipOperationExecuteStringString() throws IOException {
+ UnzipOperation unzip = new UnzipOperation(getZip());
+ File destination = new File(getTemp(),Long.toString(System.currentTimeMillis()));
+ destination.mkdirs();
+ unzip.execute(destination.getAbsolutePath(),ORG_PACKAGE_FILTER);
+ }
+
+}
Property changes on:
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/UnzipOperationTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipAllTests.java
===================================================================
---
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipAllTests.java
(rev 0)
+++
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipAllTests.java 2009-09-08
20:30:44 UTC (rev 17479)
@@ -0,0 +1,16 @@
+package org.jboss.tools.common.zip.test;
+
+import org.jboss.tools.common.zip.ZipArchive;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class ZipAllTests extends TestCase {
+ public static Test suite() {
+ TestSuite allTests = new TestSuite("org.jboss.tools.common.zip test suite");
+ allTests.addTestSuite(UnzipOperationTest.class);
+ allTests.addTestSuite(ZipArchiveTest.class);
+ return allTests;
+ }
+}
Property changes on:
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipAllTests.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipArchiveTest.java
===================================================================
---
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipArchiveTest.java
(rev 0)
+++
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipArchiveTest.java 2009-09-08
20:30:44 UTC (rev 17479)
@@ -0,0 +1,98 @@
+/**
+ *
+ */
+package org.jboss.tools.common.zip.test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
+
+import org.eclipse.core.internal.localstore.Bucket.Visitor;
+import org.jboss.tools.common.zip.DefaultZipEntryVisitor;
+import org.jboss.tools.common.zip.IZipEntryVisitor;
+import org.jboss.tools.common.zip.UnzipOperation;
+import org.jboss.tools.common.zip.ZipArchive;
+
+import junit.framework.TestCase;
+
+/**
+ * @author eskimo
+ *
+ */
+public class ZipArchiveTest extends ZipBaseTest {
+
+ /**
+ * Test method for {@link
org.jboss.tools.common.zip.ZipArchive#ZipArchive(java.lang.String)}.
+ */
+ public void testZipArchiveString() {
+ ZipArchive archive = new ZipArchive(getZip().getAbsolutePath());
+ assertEquals(getZip().getAbsolutePath(),archive.getAbsolutePath());
+ }
+
+ /**
+ * Test method for {@link
org.jboss.tools.common.zip.ZipArchive#ZipArchive(java.io.File)}.
+ */
+ public void testZipArchiveFile() {
+ ZipArchive archive = new ZipArchive(getZip());
+ assertEquals(getZip().getAbsolutePath(),archive.getAbsolutePath());
+ }
+
+ /**
+ * Test method for {@link
org.jboss.tools.common.zip.ZipArchive#acceptVisitor(org.jboss.tools.common.zip.IZipEntryVisitor)}.
+ */
+ public void testAcceptVisitor() throws IOException{
+ ZipArchive archive = new ZipArchive(getZip());
+ ZipVisitor visitor = new ZipVisitor();
+ archive.acceptVisitor(new
UnzipOperation.FilteredZipEntryVisitor("META-INF.*", visitor));
+ assertTrue(visitor.isDirVisited() && visitor.isFileVisited());
+
+ ZipArchive.acceptVisitor(new ZipFileWrapper(getZip()),visitor);
+ }
+
+ public class ZipVisitor extends DefaultZipEntryVisitor {
+
+ boolean dirVisited = false;
+ boolean fileVisited = false;
+
+ public boolean isDirVisited() {
+ return dirVisited;
+ }
+
+ public boolean isFileVisited() {
+ return fileVisited;
+ }
+
+ @Override
+ public void visiteDirectoryEntry(ZipFile zipFIle, ZipEntry dir)
+ throws IOException {
+ super.visiteDirectoryEntry(zipFIle, dir);
+ this.dirVisited = true;
+ }
+
+ @Override
+ public void visiteFileEntry(ZipFile zipFile, ZipEntry file)
+ throws IOException {
+ super.visiteFileEntry(zipFile, file);
+ this.fileVisited = true;
+ }
+ }
+
+ /**
+ * @author eskimo
+ *
+ */
+ public class ZipFileWrapper extends ZipFile {
+
+ public ZipFileWrapper(File file) throws ZipException, IOException {
+ super(file);
+ }
+
+ @Override
+ public void close() throws IOException {
+ super.close();
+ throw new IOException("Fake error");
+ }
+ }
+}
Property changes on:
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipArchiveTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipBaseTest.java
===================================================================
---
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipBaseTest.java
(rev 0)
+++
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipBaseTest.java 2009-09-08
20:30:44 UTC (rev 17479)
@@ -0,0 +1,37 @@
+package org.jboss.tools.common.zip.test;
+
+import java.io.File;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.Platform;
+
+import junit.framework.TestCase;
+
+public class ZipBaseTest extends TestCase {
+
+ public static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
+ public static final String ORG_ECLIPSE_CORE_RUNTIME_ID =
"org.eclipse.core.runtime";
+
+ private File zip;
+ private File temp;
+
+ @Override
+ protected void setUp() throws Exception {
+ zip = FileLocator.getBundleFile(Platform.getBundle(ORG_ECLIPSE_CORE_RUNTIME_ID));
+ temp = new File(System.getProperty(JAVA_IO_TMPDIR));
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ zip = null;
+ temp = null;
+ }
+
+ public File getZip() {
+ return zip;
+ }
+
+ public File getTemp() {
+ return temp;
+ }
+}
Property changes on:
trunk/common/tests/org.jboss.tools.common.test/src/org/jboss/tools/common/zip/test/ZipBaseTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain