Author: bodewig
Date: Wed Feb 25 16:09:12 2009
New Revision: 747841
URL: http://svn.apache.org/viewvc?rev=747841&view=rev
Log:
add an option to implicitly create Unicode Extra Fields in
ZiparchiveOutputStream
Modified:
commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStream.java
commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
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=747841&r1=747840&r2=747841&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
Wed Feb 25 16:09:12 2009
@@ -270,6 +270,11 @@
private boolean useEFS = true;
/**
+ * whether to create UnicodePathExtraField-s for each entry.
+ */
+ private boolean createUnicodeExtraFields = false;
+
+ /**
* Creates a new ZIP OutputStream filtering the underlying stream.
* @param out the outputstream to zip
* @since 1.1
@@ -355,6 +360,15 @@
}
/**
+ * Whether to create Unicode Extra Fields for all entries.
+ *
+ * <p>Defaults to false.</p>
+ */
+ public void setCreateUnicodeExtraFields(boolean b) {
+ createUnicodeExtraFields = b;
+ }
+
+ /**
* Finishs writing the contents and closes this as well as the
* underlying stream.
*
@@ -450,6 +464,14 @@
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
Modified:
commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java?rev=747841&r1=747840&r2=747841&view=diff
==============================================================================
---
commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
(original)
+++
commons/sandbox/compress/trunk/src/main/java/org/apache/commons/compress/archivers/zip/ZipEncodingHelper.java
Wed Feb 25 16:09:12 2009
@@ -75,17 +75,23 @@
* </pre>
*
* @param name The filename or comment with possible non-ASCII
- * unicode characters.
+ * unicode characters. Must not be null.
* @param encoding A valid encoding name. The standard zip
* encoding is <code>"CP437"</code>,
* <code>"UTF-8"</code> is supported in ZIP file
- * version <code>6.3</code> or later.
+ * version <code>6.3</code> or later. If null,
+ * will use the platform's {...@link
+ * java.lang.String#getBytes default encoding}.
* @return A byte array containing the mapped file
* name. Unmappable characters or malformed character
* sequences are mapped to a sequence of utf-16 words
* encoded in the format <code>%Uxxxx</code>.
*/
static final byte[] encodeName(String name, String encoding) {
+ if (encoding == null) {
+ return name.getBytes();
+ }
+
Charset cs = Charset.forName(encoding);
CharsetEncoder enc = cs.newEncoder();
Modified:
commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
URL:
http://svn.apache.org/viewvc/commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java?rev=747841&r1=747840&r2=747841&view=diff
==============================================================================
---
commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
(original)
+++
commons/sandbox/compress/trunk/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
Wed Feb 25 16:09:12 2009
@@ -39,20 +39,44 @@
private static final String EURO_FOR_DOLLAR_TXT = "\u20AC_for_Dollar.txt";
private static final String OIL_BARREL_TXT = "\u00D6lf\u00E4sser.txt";
- public void xtestUtf8FileRoundtrip() throws IOException {
- testFileRoundtrip(UTF_8, true);
+ public void testUtf8FileRoundtripExplicitUnicodeExtra()
+ throws IOException {
+ testFileRoundtrip(UTF_8, true, true);
+ }
+
+ public void testUtf8FileRoundtripNoEFSExplicitUnicodeExtra()
+ throws IOException {
+ testFileRoundtrip(UTF_8, false, true);
+ }
+
+ public void testCP437FileRoundtripExplicitUnicodeExtra()
+ throws IOException {
+ testFileRoundtrip(CP437, false, true);
+ }
+
+ public void testASCIIFileRoundtripExplicitUnicodeExtra()
+ throws IOException {
+ testFileRoundtrip(US_ASCII, false, true);
+ }
+
+ public void testUtf8FileRoundtripImplicitUnicodeExtra()
+ throws IOException {
+ testFileRoundtrip(UTF_8, true, false);
}
- public void testUtf8FileRoundtripNoEFS() throws IOException {
- testFileRoundtrip(UTF_8, false);
+ public void testUtf8FileRoundtripNoEFSImplicitUnicodeExtra()
+ throws IOException {
+ testFileRoundtrip(UTF_8, false, false);
}
- public void testCP437FileRoundtrip() throws IOException {
- testFileRoundtrip(CP437, false);
+ public void testCP437FileRoundtripImplicitUnicodeExtra()
+ throws IOException {
+ testFileRoundtrip(CP437, false, false);
}
- public void testASCIIFileRoundtrip() throws IOException {
- testFileRoundtrip(US_ASCII, false);
+ public void testASCIIFileRoundtripImplicitUnicodeExtra()
+ throws IOException {
+ testFileRoundtrip(US_ASCII, false, false);
}
/*
@@ -75,7 +99,8 @@
}
}
- private static void testFileRoundtrip(String encoding, boolean withEFS)
+ private static void testFileRoundtrip(String encoding, boolean withEFS,
+ boolean withExplicitUnicodeExtra)
throws IOException {
try {
@@ -88,7 +113,7 @@
File file = File.createTempFile(encoding + "-test", ".zip");
try {
- createTestFile(file, encoding, withEFS);
+ createTestFile(file, encoding, withEFS, withExplicitUnicodeExtra);
testFile(file, encoding);
} finally {
if (file.exists()) {
@@ -98,7 +123,8 @@
}
private static void createTestFile(File file, String encoding,
- boolean withEFS)
+ boolean withEFS,
+ boolean withExplicitUnicodeExtra)
throws UnsupportedEncodingException, IOException {
ZipArchiveOutputStream zos = null;
@@ -106,10 +132,12 @@
zos = new ZipArchiveOutputStream(file);
zos.setEncoding(encoding);
zos.setUseLanguageEncodingFlag(withEFS);
+ zos.setCreateUnicodeExtraFields(!withExplicitUnicodeExtra);
ZipArchiveEntry ze = new ZipArchiveEntry(OIL_BARREL_TXT);
- if (!ZipEncodingHelper.canEncodeName(ze.getName(),
- zos.getEncoding())) {
+ if (withExplicitUnicodeExtra
+ && !ZipEncodingHelper.canEncodeName(ze.getName(),
+ zos.getEncoding())) {
ze.addExtraField(new UnicodePathExtraField(ze.getName(),
zos.getEncoding()));
}
@@ -119,8 +147,9 @@
zos.closeEntry();
ze = new ZipArchiveEntry(EURO_FOR_DOLLAR_TXT);
- if (!ZipEncodingHelper.canEncodeName(ze.getName(),
- zos.getEncoding())) {
+ if (withExplicitUnicodeExtra
+ && !ZipEncodingHelper.canEncodeName(ze.getName(),
+ zos.getEncoding())) {
ze.addExtraField(new UnicodePathExtraField(ze.getName(),
zos.getEncoding()));
}
@@ -131,8 +160,9 @@
ze = new ZipArchiveEntry(ASCII_TXT);
- if (!ZipEncodingHelper.canEncodeName(ze.getName(),
- zos.getEncoding())) {
+ if (withExplicitUnicodeExtra
+ && !ZipEncodingHelper.canEncodeName(ze.getName(),
+ zos.getEncoding())) {
ze.addExtraField(new UnicodePathExtraField(ze.getName(),
zos.getEncoding()));
}