Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEike Stepper2007-08-01 05:54:11 +0000
committerEike Stepper2007-08-01 05:54:11 +0000
commit6e29a309552378ce4cf7aa7df289c1b353c2fcef (patch)
treed67d8f5154181f727640d13ef351460cd02b27aa
parent3599f5c55ab02ac2b9abdaeb49160b3dc89ca7c1 (diff)
downloadcdo-6e29a309552378ce4cf7aa7df289c1b353c2fcef.tar.gz
cdo-6e29a309552378ce4cf7aa7df289c1b353c2fcef.tar.xz
cdo-6e29a309552378ce4cf7aa7df289c1b353c2fcef.zip
*** empty log message ***
-rw-r--r--plugins/org.eclipse.net4j.tests/.cvsignore1
-rw-r--r--plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/ZipTest.java34
-rw-r--r--plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ZIPUtil.java76
3 files changed, 75 insertions, 36 deletions
diff --git a/plugins/org.eclipse.net4j.tests/.cvsignore b/plugins/org.eclipse.net4j.tests/.cvsignore
index ba077a4031..ef5815dc17 100644
--- a/plugins/org.eclipse.net4j.tests/.cvsignore
+++ b/plugins/org.eclipse.net4j.tests/.cvsignore
@@ -1 +1,2 @@
bin
+*.zip
diff --git a/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/ZipTest.java b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/ZipTest.java
new file mode 100644
index 0000000000..2f007f0580
--- /dev/null
+++ b/plugins/org.eclipse.net4j.tests/src/org/eclipse/net4j/util/tests/ZipTest.java
@@ -0,0 +1,34 @@
+/***************************************************************************
+ * Copyright (c) 2004-2007 Eike Stepper, Germany.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Eike Stepper - initial API and implementation
+ **************************************************************************/
+package org.eclipse.net4j.util.tests;
+
+import org.eclipse.net4j.util.io.ZIPUtil;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * @author Eike Stepper
+ */
+public class ZipTest extends AbstractOMTest
+{
+ public void testZip() throws Exception
+ {
+ File zipFile = newFile("src.zip");
+ File sourceFolder = newFile("src");
+ ZIPUtil.zip(sourceFolder, false, zipFile);
+ }
+
+ private static File newFile(String path) throws IOException
+ {
+ return new File(path).getCanonicalFile();
+ }
+}
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ZIPUtil.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ZIPUtil.java
index fe0559f0c6..6f428e968c 100644
--- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ZIPUtil.java
+++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/io/ZIPUtil.java
@@ -33,7 +33,7 @@ public final class ZIPUtil
{
}
- public static void zip(File zipFile, ZipEntryHandler handler) throws IORuntimeException
+ public static void zip(ZipEntryHandler handler, File zipFile) throws IORuntimeException
{
final byte[] buffer = new byte[DEFALULT_BUFFER_SIZE];
final EntryContext context = new EntryContext();
@@ -41,6 +41,7 @@ public final class ZIPUtil
FileOutputStream fos = IOUtil.openOutputStream(zipFile);
ZipOutputStream zos = null;
InputStream input = null;
+ ZipEntry entry = null;
try
{
@@ -53,24 +54,28 @@ public final class ZIPUtil
break;
}
- String name = context.getName();
- ZipEntry entry = new ZipEntry(name);
- zos.putNextEntry(entry);
-
- if (!context.isDirectory())
+ try
{
- try
+ String name = context.getName();
+ entry = new ZipEntry(name);
+ zos.putNextEntry(entry);
+
+ if (!context.isDirectory())
{
input = context.getInputStream();
IOUtil.copy(input, zos, buffer);
}
- finally
+ }
+ finally
+ {
+ IOUtil.closeSilent(input);
+ if (entry != null)
{
- IOUtil.closeSilent(input);
+ zos.closeEntry();
}
- }
- context.reset();
+ context.reset();
+ }
}
}
catch (IOException ex)
@@ -84,18 +89,9 @@ public final class ZIPUtil
}
}
- public static void zip(File zipFile, File sourceFolder, boolean excludeRoot)
+ public static void zip(File sourceFolder, boolean excludeRoot, File zipFile)
{
- zip(zipFile, new FileSystemZipHandler(sourceFolder, excludeRoot));
- }
-
- public static void main(String[] args) throws Exception
- {
- File zipFile = new File("C:\\org.eclipse.emf.ecore_2.3.1.v200707242120.zip");
- File targetFolder = new File("C:\\_weaver\\org.eclipse.emf.ecore_2.3.1.unzipped");
- unzip(zipFile, targetFolder);
-
- zip(new File("C:\\_weaver\\org.eclipse.emf.ecore_2.3.1.jar"), targetFolder, true);
+ zip(new FileSystemZipHandler(sourceFolder, excludeRoot), zipFile);
}
public static void unzip(File zipFile, UnzipHandler handler) throws IORuntimeException
@@ -166,6 +162,8 @@ public final class ZIPUtil
private InputStream inputStream;
+ private boolean directory;
+
EntryContext()
{
}
@@ -183,17 +181,12 @@ public final class ZIPUtil
boolean isDirectory()
{
- return inputStream == null;
+ return directory;
}
String getName()
{
- if (isDirectory())
- {
- return name;
- }
-
- return name + "/";
+ return name;
}
InputStream getInputStream()
@@ -201,9 +194,10 @@ public final class ZIPUtil
return inputStream;
}
- public void setName(String name)
+ public void setName(String name, boolean directory)
{
- this.name = name;
+ this.name = name + (directory ? "/" : "");
+ this.directory = directory;
}
public void setInputStream(InputStream inputStream)
@@ -223,7 +217,13 @@ public final class ZIPUtil
public FileSystemZipHandler(File sourceFolder, boolean excludeRoot)
{
- sourceFolderLength = sourceFolder.getAbsolutePath().length();
+ File root = excludeRoot ? sourceFolder : sourceFolder.getParentFile();
+ sourceFolderLength = root.getAbsolutePath().length();
+ if (excludeRoot)
+ {
+ ++sourceFolderLength;
+ }
+
files = IOUtil.listBreadthFirst(sourceFolder).iterator();
if (excludeRoot)
{
@@ -236,11 +236,15 @@ public final class ZIPUtil
if (files.hasNext())
{
File file = files.next();
- context.setName(getName(file));
-
- if (file.isFile())
+ String name = getName(file);
+ if (name.length() != 0)
{
- context.setInputStream(IOUtil.openInputStream(file));
+ context.setName(name, file.isDirectory());
+
+ if (file.isFile())
+ {
+ context.setInputStream(IOUtil.openInputStream(file));
+ }
}
}
}

Back to the top