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 2010/03/12 17:06:20 UTC
svn commit: r922309 - in /commons/proper/compress/trunk/src: changes/
main/java/org/apache/commons/compress/archivers/zip/
test/java/org/apache/commons/compress/archivers/zip/
Author: bodewig
Date: Fri Mar 12 16:06:20 2010
New Revision: 922309
URL: http://svn.apache.org/viewvc?rev=922309&view=rev
Log:
provide access to the general purpose bit flag field of a zip entry and fail when a STORED entry requires a data descriptor. COMPRESS-100
Modified:
commons/proper/compress/trunk/src/changes/changes.xml
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnsupportedZipFeatureException.java
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/ZipArchiveInputStream.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java
Modified: commons/proper/compress/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/changes/changes.xml?rev=922309&r1=922308&r2=922309&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/changes/changes.xml (original)
+++ commons/proper/compress/trunk/src/changes/changes.xml Fri Mar 12 16:06:20 2010
@@ -23,6 +23,12 @@
</properties>
<body>
<release version="1.1" date="as in SVN" description="Release 1.1">
+ <action type="fix" date="2010-03-12" issue="COMPRESS-100">
+ ZipArchiveInputStream will throw an exception if it detects an
+ entry that uses a data descriptor for a STORED entry since it
+ cannot reliably find the end of data for this "compression"
+ method.
+ </action>
<action type="fix" date="2010-03-12" issue="COMPRESS-101">
ZipArchiveInputStream should now properly read archives that
use data descriptors but without the "unofficial" signature.
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java?rev=922309&r1=922308&r2=922309&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/GeneralPurposeBit.java Fri Mar 12 16:06:20 2010
@@ -21,8 +21,9 @@ package org.apache.commons.compress.arch
* Parser/encoder for the "general purpose bit" field in ZIP's local
* file and central directory headers.
* @since Apache Commons Compress 1.1
+ * @NotThreadSafe
*/
-public class GeneralPurposeBit {
+public final class GeneralPurposeBit {
/**
* Indicates that the file is encrypted.
*/
@@ -147,4 +148,22 @@ public class GeneralPurposeBit {
b.useEncryption((generalPurposeFlag & ENCRYPTION_FLAG) != 0);
return b;
}
+
+ public int hashCode() {
+ return 3 * (7 * (13 * (17 * (encryptionFlag ? 1 : 0)
+ + (strongEncryptionFlag ? 1 : 0))
+ + (languageEncodingFlag ? 1 : 0))
+ + (dataDescriptorFlag ? 1 : 0));
+ }
+
+ public boolean equals(Object o) {
+ if (!(o instanceof GeneralPurposeBit)) {
+ return false;
+ }
+ GeneralPurposeBit g = (GeneralPurposeBit) o;
+ return g.encryptionFlag == encryptionFlag
+ && g.strongEncryptionFlag == strongEncryptionFlag
+ && g.languageEncodingFlag == languageEncodingFlag
+ && g.dataDescriptorFlag == dataDescriptorFlag;
+ }
}
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnsupportedZipFeatureException.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnsupportedZipFeatureException.java?rev=922309&r1=922308&r2=922309&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnsupportedZipFeatureException.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnsupportedZipFeatureException.java Fri Mar 12 16:06:20 2010
@@ -70,6 +70,10 @@ public class UnsupportedZipFeatureExcept
* The entry used an unsupported compression method.
*/
public static Feature METHOD = new Feature("compression method");
+ /**
+ * The entry uses a data descriptor.
+ */
+ public static Feature DATA_DESCRIPTOR = new Feature("data descriptor");
private final String name;
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=922309&r1=922308&r2=922309&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 Fri Mar 12 16:06:20 2010
@@ -74,8 +74,7 @@ public class ZipArchiveEntry extends jav
private LinkedHashMap/*<ZipShort, ZipExtraField>*/ extraFields = null;
private UnparseableExtraFieldData unparseableExtra = null;
private String name = null;
- private boolean encrypted;
- private boolean stronglyEncrypted;
+ private GeneralPurposeBit gpb = new GeneralPurposeBit();
/**
* Creates a new zip entry with the specified name.
@@ -479,38 +478,19 @@ public class ZipArchiveEntry extends jav
}
/**
- * Whether the entry is encrypted.
+ * The "general purpose bit" field.
* @since Apache Commons Compress 1.1
*/
- public boolean isEncrypted() {
- return encrypted;
+ public GeneralPurposeBit getGeneralPurposeBit() {
+ return gpb;
}
/**
- * Whether the entry is encrypted using strong encryption.
+ * The "general purpose bit" field.
* @since Apache Commons Compress 1.1
*/
- public boolean isStronglyEncrypted() {
- return stronglyEncrypted && encrypted;
- }
-
- /**
- * Whether the entry is encrypted.
- * @since Apache Commons Compress 1.1
- */
- public void setEncrypted(boolean b) {
- encrypted = b;
- }
-
- /**
- * Whether the entry is encrypted using strong encryption.
- * @since Apache Commons Compress 1.1
- */
- public void setStronglyEncrypted(boolean b) {
- stronglyEncrypted = b;
- if (b) {
- setEncrypted(true);
- }
+ public void setGeneralPurposeBit(GeneralPurposeBit b) {
+ gpb = b;
}
/**
@@ -594,6 +574,7 @@ public class ZipArchiveEntry extends jav
&& Arrays.equals(getCentralDirectoryExtra(),
other.getCentralDirectoryExtra())
&& Arrays.equals(getLocalFileDataExtra(),
- other.getLocalFileDataExtra());
+ other.getLocalFileDataExtra())
+ && gpb.equals(other.gpb);
}
}
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java?rev=922309&r1=922308&r2=922309&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveInputStream.java Fri Mar 12 16:06:20 2010
@@ -139,8 +139,7 @@ public class ZipArchiveInputStream exten
final ZipEncoding entryEncoding =
hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
hasDataDescriptor = gpFlag.usesDataDescriptor();
- current.setEncrypted(gpFlag.usesEncryption());
- current.setStronglyEncrypted(gpFlag.usesStrongEncryption());
+ current.setGeneralPurposeBit(gpFlag);
off += SHORT;
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=922309&r1=922308&r2=922309&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 Fri Mar 12 16:06:20 2010
@@ -368,8 +368,7 @@ public class ZipFile {
final boolean hasUTF8Flag = gpFlag.usesUTF8ForNames();
final ZipEncoding entryEncoding =
hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
- ze.setEncrypted(gpFlag.usesEncryption());
- ze.setStronglyEncrypted(gpFlag.usesStrongEncryption());
+ ze.setGeneralPurposeBit(gpFlag);
off += SHORT;
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java?rev=922309&r1=922308&r2=922309&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java Fri Mar 12 16:06:20 2010
@@ -184,7 +184,8 @@ public abstract class ZipUtil {
* Whether this library is able to read or write the given entry.
*/
static boolean canHandleEntryData(ZipArchiveEntry entry) {
- return supportsEncryptionOf(entry) && supportsMethodOf(entry);
+ return supportsEncryptionOf(entry) && supportsMethodOf(entry)
+ && supportsDataDescriptorFor(entry);
}
/**
@@ -194,7 +195,7 @@ public abstract class ZipUtil {
* @return true if the entry isn't encrypted at all
*/
private static boolean supportsEncryptionOf(ZipArchiveEntry entry) {
- return !entry.isEncrypted();
+ return !entry.getGeneralPurposeBit().usesEncryption();
}
/**
@@ -209,6 +210,17 @@ public abstract class ZipUtil {
}
/**
+ * Whether this entry requires a data descriptor this library can work with.
+ *
+ * @return true if the entry doesn't require any data descriptor
+ * or the method is DEFLATED).
+ */
+ private static boolean supportsDataDescriptorFor(ZipArchiveEntry entry) {
+ return !entry.getGeneralPurposeBit().usesDataDescriptor()
+ || entry.getMethod() == ZipArchiveEntry.DEFLATED;
+ }
+
+ /**
* Checks whether the entry requires features not (yet) supported
* by the library and throws an exception if it does.
*/
@@ -224,5 +236,10 @@ public abstract class ZipUtil {
new UnsupportedZipFeatureException(UnsupportedZipFeatureException
.Feature.METHOD, ze);
}
+ if (!supportsDataDescriptorFor(ze)) {
+ throw
+ new UnsupportedZipFeatureException(UnsupportedZipFeatureException
+ .Feature.DATA_DESCRIPTOR, ze);
+ }
}
}
\ No newline at end of file
Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java?rev=922309&r1=922308&r2=922309&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/EncryptedArchiveTest.java Fri Mar 12 16:06:20 2010
@@ -36,8 +36,8 @@ public class EncryptedArchiveTest extend
try {
zf = new ZipFile(file);
ZipArchiveEntry zae = zf.getEntry("LICENSE.txt");
- assertTrue(zae.isEncrypted());
- assertFalse(zae.isStronglyEncrypted());
+ assertTrue(zae.getGeneralPurposeBit().usesEncryption());
+ assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption());
assertFalse(zf.canReadEntryData(zae));
try {
zf.getInputStream(zae);
@@ -60,8 +60,8 @@ public class EncryptedArchiveTest extend
zin = new ZipArchiveInputStream(new FileInputStream(file));
ZipArchiveEntry zae = zin.getNextZipEntry();
assertEquals("LICENSE.txt", zae.getName());
- assertTrue(zae.isEncrypted());
- assertFalse(zae.isStronglyEncrypted());
+ assertTrue(zae.getGeneralPurposeBit().usesEncryption());
+ assertFalse(zae.getGeneralPurposeBit().usesStrongEncryption());
assertFalse(zin.canReadEntryData(zae));
try {
byte[] buf = new byte[1024];