Author: bodewig Date: Thu Feb 26 09:07:23 2009 New Revision: 748063 URL: http://svn.apache.org/viewvc?rev=748063&view=rev Log: make sure UnicodeField sees the same bytes that are actually written to the archive when calculating the CRC
Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/AbstractUnicodeExtraField.java commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnicodeCommentExtraField.java commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnicodePathExtraField.java commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/AbstractUnicodeExtraField.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/AbstractUnicodeExtraField.java?rev=748063&r1=748062&r2=748063&view=diff ============================================================================== --- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/AbstractUnicodeExtraField.java (original) +++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/AbstractUnicodeExtraField.java Thu Feb 26 09:07:23 2009 @@ -34,23 +34,32 @@ } /** - * Assemble as unicode path extension form the name and encoding - * of the orginal zip entry. + * Assemble as unicode extension from the name/comment and + * encoding of the orginal zip entry. * - * @param name The file name or comment. + * @param text The file name or comment. * @param zipEncoding The encoding of the filenames in the zip * file, usually <code>"CP437"</code>. */ - protected AbstractUnicodeExtraField(String name, String zipEncoding) { - - byte[] filename = ZipEncodingHelper.encodeName(name, zipEncoding); + protected AbstractUnicodeExtraField(String text, String zipEncoding) { + this(text, ZipEncodingHelper.encodeName(text, zipEncoding)); + } + /** + * Assemble as unicode extension from the name/comment and + * encoding of the orginal zip entry. + * + * @param text The file name or comment. + * @param zipEncoding The encoding of the filenames in the zip + * file, usually <code>"CP437"</code>. + */ + protected AbstractUnicodeExtraField(String text, byte[] bytes) { CRC32 crc32 = new CRC32(); - crc32.update(filename); + crc32.update(bytes); nameCRC32 = crc32.getValue(); try { - unicodeName = name.getBytes("UTF-8"); + unicodeName = text.getBytes("UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException("FATAL: UTF-8 encoding not supported.", e); Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnicodeCommentExtraField.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnicodeCommentExtraField.java?rev=748063&r1=748062&r2=748063&view=diff ============================================================================== --- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnicodeCommentExtraField.java (original) +++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnicodeCommentExtraField.java Thu Feb 26 09:07:23 2009 @@ -42,15 +42,26 @@ } /** - * Assemble as unicode comment extension form the comment and + * Assemble as unicode comment extension from the comment and * encoding of the orginal zip entry. * - * @param name The file name + * @param comment The file comment * @param zipEncoding The encoding of the comment in the zip file, * usually <code>"CP437"</code>. */ - public UnicodeCommentExtraField(String name, String zipEncoding) { - super(name, zipEncoding); + public UnicodeCommentExtraField(String comment, String zipEncoding) { + super(comment, zipEncoding); + } + + /** + * Assemble as unicode comment extension from the comment given as + * text as well as the bytes actually written to the archive. + * + * @param comment The file comment + * @param bytes the bytes actually written to the archive + */ + public UnicodeCommentExtraField(String comment, byte[] bytes) { + super(comment, bytes); } public ZipShort getHeaderId() { Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnicodePathExtraField.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnicodePathExtraField.java?rev=748063&r1=748062&r2=748063&view=diff ============================================================================== --- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnicodePathExtraField.java (original) +++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/UnicodePathExtraField.java Thu Feb 26 09:07:23 2009 @@ -42,7 +42,7 @@ } /** - * Assemble as unicode path extension form the name and encoding + * Assemble as unicode path extension from the name and encoding * of the orginal zip entry. * * @param name The file name @@ -53,6 +53,17 @@ super(name, zipEncoding); } + /** + * Assemble as unicode path extension from the name given as + * text as well as the bytes actually written to the archive. + * + * @param name The file name + * @param bytes the bytes actually written to the archive + */ + public UnicodePathExtraField(String name, byte[] bytes) { + super(name, bytes); + } + public ZipShort getHeaderId() { return UPATH_ID; } Modified: commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java URL: http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java?rev=748063&r1=748062&r2=748063&view=diff ============================================================================== --- commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java (original) +++ commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java Thu Feb 26 09:07:23 2009 @@ -464,14 +464,6 @@ closeEntry(); entry = ze; - if (createUnicodeExtraFields) { - ze.addExtraField(new UnicodePathExtraField(ze.getName(), - encoding)); - if (ze.getComment() != null) { - ze.addExtraField(new UnicodeCommentExtraField(ze.getComment(), - encoding)); - } - } entries.add(entry); if (entry.getMethod() == -1) { // not specified @@ -687,6 +679,17 @@ * @since 1.1 */ protected void writeLocalFileHeader(ZipArchiveEntry ze) throws IOException { + + byte[] name = getBytes(ze.getName()); + if (createUnicodeExtraFields) { + ze.addExtraField(new UnicodePathExtraField(ze.getName(), name)); + String comm = ze.getComment(); + if (comm != null && !"".equals(comm)) { + byte[] commentB = getBytes(comm); + ze.addExtraField(new UnicodeCommentExtraField(comm, commentB)); + } + } + offsets.put(ze, ZipLong.getBytes(written)); writeOut(LFH_SIG); @@ -724,7 +727,6 @@ // CheckStyle:MagicNumber ON // file name length - byte[] name = getBytes(ze.getName()); writeOut(ZipShort.getBytes(name.length)); written += SHORT;