You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2013/02/11 15:23:24 UTC
svn commit: r1444779 - in /commons/proper/compress/trunk/src:
main/java/org/apache/commons/compress/archivers/zip/
test/java/org/apache/commons/compress/archivers/zip/
Author: bodewig
Date: Mon Feb 11 14:23:23 2013
New Revision: 1444779
URL: http://svn.apache.org/r1444779
Log:
COMPRESS-214 move getUnixSymlink from ZipArchiveEntry to ZipFile
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntry.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.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=1444779&r1=1444778&r2=1444779&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 Mon Feb 11 14:23:23 2013
@@ -18,7 +18,6 @@
package org.apache.commons.compress.archivers.zip;
import org.apache.commons.compress.archivers.ArchiveEntry;
-import org.apache.commons.compress.utils.IOUtils;
import java.io.File;
import java.io.IOException;
@@ -282,72 +281,6 @@ public class ZipArchiveEntry extends jav
}
/**
- * <p>
- * Convenience method to return this entry's content as a String if isUnixSymlink()
- * returns true, otherwise returns null.
- * This method assumes the symlink's target path has been stored in the zip file using the
- * UTF-8 character encoding.
- * </p><p>
- * Unfortunately, there is no reliable way to automatically obtain the target path's
- * character encoding (within the zip file) should it differ from UTF-8. Zip files
- * do not remember the character set used to encode zip contents.
- * Entry names do not have this problem (thanks to various flags and extra fields),
- * but symlink targets are not so lucky. If you happen to know the character set that was used
- * to encode symlink paths in the zip file, feel free to use the overloaded version:
- * <code>getUnixSymlink(ZipFile zf, String pathCharset)</code>.
- * </p><p>
- * This method can only be used in conjunction with a ZipFile object (since we need
- * the entry's contents); users of ZipArchiveInputStream will need to create their
- * own logic.
- * </p>
- *
- * @param zf ZipFile object that contains this ZipArchiveEntry's contents.
- * @return entry's content as a String (UTF-8 character set assumed).
- * @throws IOException problem with content's input stream
- */
- public String getUnixSymlink(ZipFile zf) throws IOException {
- return getUnixSymlink(zf, "UTF-8");
- }
-
- /**
- * <p>
- * Convenience method to return this entry's content as a String if isUnixSymlink()
- * returns true, otherwise returns null.
- * </p><p>
- * Unfortunately, there is no reliable way to automatically obtain the target path's
- * character encoding (within the zip file) should it differ from UTF-8. Zip files
- * do not remember the character set used to encode zip contents.
- * Entry names do not have this problem (thanks to various flags and extra fields),
- * but symlink targets are not so lucky.
- * </p><p>
- * This method can only be used in conjunction with a ZipFile object (since we need
- * the entry's contents); users of ZipArchiveInputStream will need to create their
- * own logic.
- * </p>
- *
- * @param zf ZipFile object that contains this ZipArchiveEntry's contents.
- * @param pathCharset the character set used to encode the symlink's target path.
- * @return entry's content as a String (using the provided character set).
- * @throws IOException problem with content's input stream
- */
- public String getUnixSymlink(ZipFile zf, String pathCharset) throws IOException {
- if (isUnixSymlink()) {
- InputStream in = null;
- try {
- in = zf.getInputStream(this);
- byte[] symlinkBytes = IOUtils.toByteArray(in);
- return new String(symlinkBytes, pathCharset);
- } finally {
- if (in != null) {
- in.close();
- }
- }
- } else {
- return null;
- }
- }
-
- /**
* Platform specification to put into the "version made
* by" part of the central file header.
*
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java?rev=1444779&r1=1444778&r2=1444779&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java Mon Feb 11 14:23:23 2013
@@ -34,6 +34,8 @@ import java.util.zip.InflaterInputStream
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
+import org.apache.commons.compress.utils.IOUtils;
+
import static org.apache.commons.compress.archivers.zip.ZipConstants.DWORD;
import static org.apache.commons.compress.archivers.zip.ZipConstants.SHORT;
import static org.apache.commons.compress.archivers.zip.ZipConstants.WORD;
@@ -351,6 +353,36 @@ public class ZipFile {
}
/**
+ * <p>
+ * Convenience method to return the entry's content as a String if isUnixSymlink()
+ * returns true for it, otherwise returns null.
+ * </p>
+ *
+ * <p>This method assumes the symbolic link's file name uses the
+ * same encoding that as been specified for this ZipFile.</p>
+ *
+ * @param entry ZipArchiveEntry object that represents the symbolic link
+ * @return entry's content as a String
+ * @throws IOException problem with content's input stream
+ */
+ public String getUnixSymlink(ZipArchiveEntry entry) throws IOException {
+ if (entry != null && entry.isUnixSymlink()) {
+ InputStream in = null;
+ try {
+ in = getInputStream(entry);
+ byte[] symlinkBytes = IOUtils.toByteArray(in);
+ return zipEncoding.decode(symlinkBytes);
+ } finally {
+ if (in != null) {
+ in.close();
+ }
+ }
+ } else {
+ return null;
+ }
+ }
+
+ /**
* Ensures that the close method of this zipfile is called when
* there are no more references to it.
* @see #close()
Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java?rev=1444779&r1=1444778&r2=1444779&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveEntryTest.java Mon Feb 11 14:23:23 2013
@@ -25,7 +25,6 @@ import junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Enumeration;
-import java.util.TreeMap;
import java.util.zip.ZipEntry;
/**
@@ -38,53 +37,6 @@ public class ZipArchiveEntryTest extends
super(name);
}
- public void testUnixSymlinkSampleFile() throws Exception {
- final String entryPrefix = "COMPRESS-214_unix_symlinks/";
- final TreeMap<String, String> expectedVals = new TreeMap<String, String>();
-
- // I threw in some Japanese characters to keep things interesting.
- expectedVals.put(entryPrefix + "link1", "../COMPRESS-214_unix_symlinks/./a/b/c/../../../\uF999");
- expectedVals.put(entryPrefix + "link2", "../COMPRESS-214_unix_symlinks/./a/b/c/../../../g");
- expectedVals.put(entryPrefix + "link3", "../COMPRESS-214_unix_symlinks/././a/b/c/../../../\u76F4\u6A39");
- expectedVals.put(entryPrefix + "link4", "\u82B1\u5B50/\u745B\u5B50");
- expectedVals.put(entryPrefix + "\uF999", "./\u82B1\u5B50/\u745B\u5B50/\u5897\u8C37/\uF999");
- expectedVals.put(entryPrefix + "g", "./a/b/c/d/e/f/g");
- expectedVals.put(entryPrefix + "\u76F4\u6A39", "./g");
-
- // Notice how a directory link might contain a trailing slash, or it might not.
- // Also note: symlinks are always stored as files, even if they link to directories.
- expectedVals.put(entryPrefix + "link5", "../COMPRESS-214_unix_symlinks/././a/b");
- expectedVals.put(entryPrefix + "link6", "../COMPRESS-214_unix_symlinks/././a/b/");
-
- // I looked into creating a test with hard links, but zip does not appear to
- // support hard links, so nevermind.
-
- File archive = getFile("COMPRESS-214_unix_symlinks.zip");
- ZipFile zf = null;
-
- try {
- zf = new ZipFile(archive);
- Enumeration<ZipArchiveEntry> en = zf.getEntries();
- while (en.hasMoreElements()) {
- ZipArchiveEntry zae = en.nextElement();
- if (zae.isUnixSymlink()) {
- String name = zae.getName();
- String link = zae.getUnixSymlink(zf);
- String expected = expectedVals.get(name);
- assertEquals(expected, link);
- } else {
- String link = zae.getUnixSymlink(zf);
- // Should be null if it's not a symlink!
- assertNull(link);
- }
- }
- } finally {
- if (zf != null) {
- zf.close();
- }
- }
- }
-
/**
* test handling of extra fields
*/
Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java?rev=1444779&r1=1444778&r2=1444779&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/ZipFileTest.java Mon Feb 11 14:23:23 2013
@@ -26,6 +26,8 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.Enumeration;
+import java.util.TreeMap;
import java.util.zip.ZipEntry;
import junit.framework.TestCase;
@@ -163,6 +165,45 @@ public class ZipFileTest extends TestCas
assertNotNull(zf.getEntry("test2.xml"));
}
+ public void testUnixSymlinkSampleFile() throws Exception {
+ final String entryPrefix = "COMPRESS-214_unix_symlinks/";
+ final TreeMap<String, String> expectedVals = new TreeMap<String, String>();
+
+ // I threw in some Japanese characters to keep things interesting.
+ expectedVals.put(entryPrefix + "link1", "../COMPRESS-214_unix_symlinks/./a/b/c/../../../\uF999");
+ expectedVals.put(entryPrefix + "link2", "../COMPRESS-214_unix_symlinks/./a/b/c/../../../g");
+ expectedVals.put(entryPrefix + "link3", "../COMPRESS-214_unix_symlinks/././a/b/c/../../../\u76F4\u6A39");
+ expectedVals.put(entryPrefix + "link4", "\u82B1\u5B50/\u745B\u5B50");
+ expectedVals.put(entryPrefix + "\uF999", "./\u82B1\u5B50/\u745B\u5B50/\u5897\u8C37/\uF999");
+ expectedVals.put(entryPrefix + "g", "./a/b/c/d/e/f/g");
+ expectedVals.put(entryPrefix + "\u76F4\u6A39", "./g");
+
+ // Notice how a directory link might contain a trailing slash, or it might not.
+ // Also note: symlinks are always stored as files, even if they link to directories.
+ expectedVals.put(entryPrefix + "link5", "../COMPRESS-214_unix_symlinks/././a/b");
+ expectedVals.put(entryPrefix + "link6", "../COMPRESS-214_unix_symlinks/././a/b/");
+
+ // I looked into creating a test with hard links, but zip does not appear to
+ // support hard links, so nevermind.
+
+ File archive = getFile("COMPRESS-214_unix_symlinks.zip");
+
+ zf = new ZipFile(archive);
+ Enumeration<ZipArchiveEntry> en = zf.getEntries();
+ while (en.hasMoreElements()) {
+ ZipArchiveEntry zae = en.nextElement();
+ String link = zf.getUnixSymlink(zae);
+ if (zae.isUnixSymlink()) {
+ String name = zae.getName();
+ String expected = expectedVals.get(name);
+ assertEquals(expected, link);
+ } else {
+ // Should be null if it's not a symlink!
+ assertNull(link);
+ }
+ }
+ }
+
/*
* ordertest.zip has been handcrafted.
*