Author: bodewig
Date: Sat Jul 30 15:12:32 2011
New Revision: 1152487
URL: http://svn.apache.org/viewvc?rev=1152487&view=rev
Log:
third case of seven: known size, no compression, writing to random access file.
COMPRESS-150
Modified:
commons/proper/compress/branches/zip64/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
commons/proper/compress/branches/zip64/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportTest.java
Modified:
commons/proper/compress/branches/zip64/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/branches/zip64/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java?rev=1152487&r1=1152486&r2=1152487&view=diff
==============================================================================
---
commons/proper/compress/branches/zip64/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
(original)
+++
commons/proper/compress/branches/zip64/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
Sat Jul 30 15:12:32 2011
@@ -444,8 +444,17 @@ public class ZipArchiveOutputStream exte
raf.seek(localDataStart);
writeOut(ZipLong.getBytes(entry.getCrc()));
+ if (!hasZip64Extra(entry)) {
writeOut(ZipLong.getBytes(entry.getCompressedSize()));
writeOut(ZipLong.getBytes(entry.getSize()));
+ } else {
+ writeOut(ZipLong.ZIP64_MAGIC.getBytes());
+ writeOut(ZipLong.ZIP64_MAGIC.getBytes());
+ raf.seek(localDataStart + 3 * WORD + 2 * SHORT
+ + getName(entry).limit() + 2 * SHORT);
+
writeOut(ZipEightByteInteger.getBytes(entry.getCompressedSize()));
+ writeOut(ZipEightByteInteger.getBytes(entry.getSize()));
+ }
raf.seek(save);
}
@@ -689,16 +698,7 @@ public class ZipArchiveOutputStream exte
protected void writeLocalFileHeader(ZipArchiveEntry ze) throws IOException
{
boolean encodable = zipEncoding.canEncode(ze.getName());
-
- final ZipEncoding entryEncoding;
-
- if (!encodable && fallbackToUTF8) {
- entryEncoding = ZipEncodingHelper.UTF8_ZIP_ENCODING;
- } else {
- entryEncoding = zipEncoding;
- }
-
- ByteBuffer name = entryEncoding.encode(ze.getName());
+ ByteBuffer name = getName(ze);
if (createUnicodeExtraFields != UnicodeExtraFieldPolicy.NEVER) {
@@ -713,11 +713,11 @@ public class ZipArchiveOutputStream exte
String comm = ze.getComment();
if (comm != null && !"".equals(comm)) {
- boolean commentEncodable = this.zipEncoding.canEncode(comm);
+ boolean commentEncodable = zipEncoding.canEncode(comm);
if (createUnicodeExtraFields == UnicodeExtraFieldPolicy.ALWAYS
|| !commentEncodable) {
- ByteBuffer commentB = entryEncoding.encode(comm);
+ ByteBuffer commentB = getEntryEncoding(ze).encode(comm);
ze.addExtraField(new UnicodeCommentExtraField(comm,
commentB.array(),
commentB.arrayOffset(),
@@ -880,16 +880,7 @@ public class ZipArchiveOutputStream exte
written += 12;
// CheckStyle:MagicNumber ON
- // file name length
- final ZipEncoding entryEncoding;
-
- if (!encodable && fallbackToUTF8) {
- entryEncoding = ZipEncodingHelper.UTF8_ZIP_ENCODING;
- } else {
- entryEncoding = zipEncoding;
- }
-
- ByteBuffer name = entryEncoding.encode(ze.getName());
+ ByteBuffer name = getName(ze);
writeOut(ZipShort.getBytes(name.limit()));
written += SHORT;
@@ -905,7 +896,7 @@ public class ZipArchiveOutputStream exte
comm = "";
}
- ByteBuffer commentB = entryEncoding.encode(comm);
+ ByteBuffer commentB = getEntryEncoding(ze).encode(comm);
writeOut(ZipShort.getBytes(commentB.limit()));
written += SHORT;
@@ -1163,8 +1154,11 @@ public class ZipArchiveOutputStream exte
+ ", raf: " + (raf != null));
*/
z64 = new Zip64ExtendedInformationExtraField();
- ze.addAsFirstExtraField(z64);
}
+
+ // even if the field is there already, make sure it is the first one
+ ze.addAsFirstExtraField(z64);
+
return z64;
}
@@ -1179,4 +1173,14 @@ public class ZipArchiveOutputStream exte
.HEADER_ID)
!= null;
}
+
+ private ZipEncoding getEntryEncoding(ZipArchiveEntry ze) {
+ boolean encodable = zipEncoding.canEncode(ze.getName());
+ return !encodable && fallbackToUTF8
+ ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
+ }
+
+ private ByteBuffer getName(ZipArchiveEntry ze) throws IOException {
+ return getEntryEncoding(ze).encode(ze.getName());
+ }
}
Modified:
commons/proper/compress/branches/zip64/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/branches/zip64/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportTest.java?rev=1152487&r1=1152486&r2=1152487&view=diff
==============================================================================
---
commons/proper/compress/branches/zip64/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportTest.java
(original)
+++
commons/proper/compress/branches/zip64/src/test/java/org/apache/commons/compress/archivers/zip/Zip64SupportTest.java
Sat Jul 30 15:12:32 2011
@@ -285,12 +285,14 @@ public class Zip64SupportTest {
}
};
+ @Ignore
@Test public void write3EntriesCreatingBigArchiveFile() throws Throwable {
withTemporaryArchive("write3EntriesCreatingBigArchiveFile",
write3EntriesCreatingBigArchive,
true);
}
+ @Ignore
@Test public void write3EntriesCreatingBigArchiveStream() throws Throwable
{
withTemporaryArchive("write3EntriesCreatingBigArchiveStream",
write3EntriesCreatingBigArchive,
@@ -299,26 +301,26 @@ public class Zip64SupportTest {
/*
* One entry of length 5 billion bytes, written without
- * compression to a stream.
+ * compression.
*
- * No Compression + Stream => sizes must be known before data is
- * written and are stored directly inside the LFH. No Data
- * Descriptor at all.
+ * No Compression => sizes are stored directly inside the LFH. No
+ * Data Descriptor at all.
*
* Creates a temporary archive of approx 5GB in size
*/
- @Test public void writeBigStoredEntryToStream() throws Throwable {
- withTemporaryArchive("writeBigStoredEntryToStream",
- new ZipOutputTest() {
+ private static ZipOutputTest writeBigStoredEntry(final boolean knownSize) {
+ return new ZipOutputTest() {
public void test(File f,
ZipArchiveOutputStream zos)
throws IOException {
byte[] buf = new byte[1000 * 1000];
ZipArchiveEntry zae =
new ZipArchiveEntry("0");
+ if (knownSize) {
zae.setSize(FIVE_BILLION);
- zae.setMethod(ZipArchiveEntry.STORED);
zae.setCrc(0x5c316f50L);
+ }
+ zae.setMethod(ZipArchiveEntry.STORED);
zos.putArchiveEntry(zae);
for (int j = 0;
j < FIVE_BILLION / 1000 / 1000;
@@ -451,10 +453,25 @@ public class Zip64SupportTest {
a.close();
}
}
- },
+ };
+ }
+
+ /*
+ * No Compression + Stream => sizes must be known before data is
+ * written.
+ */
+ @Test public void writeBigStoredEntryToStream() throws Throwable {
+ withTemporaryArchive("writeBigStoredEntryToStream",
+ writeBigStoredEntry(true),
false);
}
+ @Test public void writeBigStoredEntryKnownSizeToFile() throws Throwable {
+ withTemporaryArchive("writeBigStoredEntryKnownSizeToFile",
+ writeBigStoredEntry(true),
+ true);
+ }
+
/*
* One entry of length 5 billion bytes, written with
* compression to a stream.