commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r799909 - in /commons/proper/compress/trunk/src: main/java/org/apache/commons/compress/archivers/zip/ test/java/org/apache/commons/compress/ test/java/org/apache/commons/compress/archivers/
Date Sat, 01 Aug 2009 19:17:58 GMT
Author: bodewig
Date: Sat Aug  1 19:17:58 2009
New Revision: 799909

URL: http://svn.apache.org/viewvc?rev=799909&view=rev
Log:
check name/directory/size and lastModified read from zip is consistent with what we write

Modified:
    commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
    commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java

Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java?rev=799909&r1=799908&r2=799909&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
(original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
Sat Aug  1 19:17:58 2009
@@ -87,7 +87,8 @@
     }
 
     public ZipArchiveEntry(File inputFile, String entryName) {
-        this(entryName);
+        this(inputFile.isDirectory() && !entryName.endsWith("/") ? 
+             entryName + "/" : entryName);
         if (inputFile.isFile()){
             setSize(inputFile.length());
         }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java?rev=799909&r1=799908&r2=799909&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/AbstractTestCase.java
Sat Aug  1 19:17:58 2009
@@ -338,4 +338,25 @@
     protected String getExpectedString(ArchiveEntry entry) {
         return entry.getName();
     }
+
+    /**
+     * Creates a temporary directory and a temporary file inside that
+     * directory, returns both of them (the directory is the first
+     * element of the two element array).
+     */
+    protected File[] createTempDirAndFile() throws IOException {
+        File tmpDir = File.createTempFile("testdir", "");
+        tmpDir.delete();
+        tmpDir.mkdir();
+        tmpDir.deleteOnExit();
+        File tmpFile = File.createTempFile("testfile", "", tmpDir);
+        tmpFile.deleteOnExit();
+        FileOutputStream fos = new FileOutputStream(tmpFile);
+        try {
+            fos.write(new byte[] {'f', 'o', 'o'});
+            return new File[] {tmpDir, tmpFile};
+        } finally {
+            fos.close();
+        }            
+    }
 }

Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java?rev=799909&r1=799908&r2=799909&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
(original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/ZipTestCase.java
Sat Aug  1 19:17:58 2009
@@ -24,10 +24,13 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
 
 import org.apache.commons.compress.AbstractTestCase;
 import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
+import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
+import org.apache.commons.compress.archivers.zip.ZipFile;
 import org.apache.commons.compress.utils.IOUtils;
 
 public final class ZipTestCase extends AbstractTestCase {
@@ -153,4 +156,174 @@
         results.contains("test2.xml");
         results.contains("test3.xml");
     }
+
+    public void testDirectoryEntryFromFile() throws Exception {
+        File[] tmp = createTempDirAndFile();
+        File archive = null;
+        ZipArchiveOutputStream zos = null;
+        ZipFile zf = null;
+        try {
+            archive = File.createTempFile("test.", ".zip", tmp[0]);
+            archive.deleteOnExit();
+            zos = new ZipArchiveOutputStream(archive);
+            long beforeArchiveWrite = tmp[0].lastModified();
+            ZipArchiveEntry in = new ZipArchiveEntry(tmp[0], "foo");
+            zos.putArchiveEntry(in);
+            zos.closeArchiveEntry();
+            zos.close();
+            zos = null;
+            zf = new ZipFile(archive);
+            ZipArchiveEntry out = zf.getEntry("foo/");
+            assertNotNull(out);
+            assertEquals("foo/", out.getName());
+            assertEquals(0, out.getSize());
+            // ZIP stores time with a granularity of 2 seconds
+            assertEquals(beforeArchiveWrite / 2000,
+                         out.getLastModifiedDate().getTime() / 2000);
+            assertTrue(out.isDirectory());
+        } finally {
+            ZipFile.closeQuietly(zf);
+            if (zos != null) {
+                zos.close();
+            }
+            if (archive != null) {
+                archive.delete();
+            }
+            tmp[1].delete();
+            tmp[0].delete();
+        }
+    }
+
+    public void testExplicitDirectoryEntry() throws Exception {
+        File[] tmp = createTempDirAndFile();
+        File archive = null;
+        ZipArchiveOutputStream zos = null;
+        ZipFile zf = null;
+        try {
+            archive = File.createTempFile("test.", ".zip", tmp[0]);
+            archive.deleteOnExit();
+            zos = new ZipArchiveOutputStream(archive);
+            long beforeArchiveWrite = tmp[0].lastModified();
+            ZipArchiveEntry in = new ZipArchiveEntry("foo/");
+            in.setTime(beforeArchiveWrite);
+            zos.putArchiveEntry(in);
+            zos.closeArchiveEntry();
+            zos.close();
+            zos = null;
+            zf = new ZipFile(archive);
+            ZipArchiveEntry out = zf.getEntry("foo/");
+            assertNotNull(out);
+            assertEquals("foo/", out.getName());
+            assertEquals(0, out.getSize());
+            assertEquals(beforeArchiveWrite / 2000,
+                         out.getLastModifiedDate().getTime() / 2000);
+            assertTrue(out.isDirectory());
+        } finally {
+            ZipFile.closeQuietly(zf);
+            if (zos != null) {
+                zos.close();
+            }
+            if (archive != null) {
+                archive.delete();
+            }
+            tmp[1].delete();
+            tmp[0].delete();
+        }
+    }
+
+    public void testFileEntryFromFile() throws Exception {
+        File[] tmp = createTempDirAndFile();
+        File archive = null;
+        ZipArchiveOutputStream zos = null;
+        ZipFile zf = null;
+        FileInputStream fis = null;
+        try {
+            archive = File.createTempFile("test.", ".zip", tmp[0]);
+            archive.deleteOnExit();
+            zos = new ZipArchiveOutputStream(archive);
+            ZipArchiveEntry in = new ZipArchiveEntry(tmp[1], "foo");
+            zos.putArchiveEntry(in);
+            byte[] b = new byte[(int) tmp[1].length()];
+            fis = new FileInputStream(tmp[1]);
+            int read;
+            while ((read = fis.read(b)) > 0) {
+                zos.write(b);
+            }
+            fis.close();
+            fis = null;
+            zos.closeArchiveEntry();
+            zos.close();
+            zos = null;
+            zf = new ZipFile(archive);
+            ZipArchiveEntry out = zf.getEntry("foo");
+            assertNotNull(out);
+            assertEquals("foo", out.getName());
+            assertEquals(tmp[1].length(), out.getSize());
+            assertEquals(tmp[1].lastModified() / 2000,
+                         out.getLastModifiedDate().getTime() / 2000);
+            assertFalse(out.isDirectory());
+        } finally {
+            ZipFile.closeQuietly(zf);
+            if (zos != null) {
+                zos.close();
+            }
+            if (archive != null) {
+                archive.delete();
+            }
+            if (fis != null) {
+                fis.close();
+            }
+            tmp[1].delete();
+            tmp[0].delete();
+        }
+    }
+
+    public void testExplicitFileEntry() throws Exception {
+        File[] tmp = createTempDirAndFile();
+        File archive = null;
+        ZipArchiveOutputStream zos = null;
+        ZipFile zf = null;
+        FileInputStream fis = null;
+        try {
+            archive = File.createTempFile("test.", ".zip", tmp[0]);
+            archive.deleteOnExit();
+            zos = new ZipArchiveOutputStream(archive);
+            ZipArchiveEntry in = new ZipArchiveEntry("foo");
+            in.setTime(tmp[1].lastModified());
+            in.setSize(tmp[1].length());
+            zos.putArchiveEntry(in);
+            byte[] b = new byte[(int) tmp[1].length()];
+            fis = new FileInputStream(tmp[1]);
+            int read;
+            while ((read = fis.read(b)) > 0) {
+                zos.write(b);
+            }
+            fis.close();
+            fis = null;
+            zos.closeArchiveEntry();
+            zos.close();
+            zos = null;
+            zf = new ZipFile(archive);
+            ZipArchiveEntry out = zf.getEntry("foo");
+            assertNotNull(out);
+            assertEquals("foo", out.getName());
+            assertEquals(tmp[1].length(), out.getSize());
+            assertEquals(tmp[1].lastModified() / 2000,
+                         out.getLastModifiedDate().getTime() / 2000);
+            assertFalse(out.isDirectory());
+        } finally {
+            ZipFile.closeQuietly(zf);
+            if (zos != null) {
+                zos.close();
+            }
+            if (archive != null) {
+                archive.delete();
+            }
+            if (fis != null) {
+                fis.close();
+            }
+            tmp[1].delete();
+            tmp[0].delete();
+        }
+    }
 }



Mime
View raw message