Author: bodewig
Date: Sun Feb 23 19:50:43 2014
New Revision: 1571078
URL: http://svn.apache.org/r1571078
Log:
allow a stack of methods when writing 7z files - preparation for COMPRESS-266
and needed by COMPRESS-257 as well
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
URL:
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java?rev=1571078&r1=1571077&r2=1571078&view=diff
==============================================================================
---
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
(original)
+++
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
Sun Feb 23 19:50:43 2014
@@ -27,8 +27,10 @@ import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.BitSet;
+import java.util.Collections;
import java.util.Date;
import java.util.List;
+import java.util.LinkedList;
import java.util.zip.CRC32;
import org.apache.commons.compress.archivers.ArchiveEntry;
@@ -47,7 +49,7 @@ public class SevenZOutputFile implements
private long fileBytesWritten = 0;
private boolean finished = false;
private CountingOutputStream currentOutputStream;
- private SevenZMethod contentCompression = SevenZMethod.LZMA2;
+ private Iterable<SevenZMethod> contentMethods =
Collections.singletonList(SevenZMethod.LZMA2);
/**
* Opens file to write a 7z archive to.
@@ -67,9 +69,29 @@ public class SevenZOutputFile implements
* <p>Currently only {@link SevenZMethod#COPY}, {@link
* SevenZMethod#LZMA2}, {@link SevenZMethod#BZIP2} and {@link
* SevenZMethod#DEFLATE} are supported.</p>
+ *
+ * <p>This is a short form for passing a single-element iterable
+ * to {@link #setContentMethods}.</p>
*/
public void setContentCompression(SevenZMethod method) {
- this.contentCompression = method;
+ setContentMethods(Collections.singletonList(method));
+ }
+
+ /**
+ * Sets the (compression) methods to use for entry contents - the
+ * default is LZMA2.
+ *
+ * <p>Currently only {@link SevenZMethod#COPY}, {@link
+ * SevenZMethod#LZMA2}, {@link SevenZMethod#BZIP2} and {@link
+ * SevenZMethod#DEFLATE} are supported.</p>
+ *
+ * <p>The methods will be consulted in iteration order to create
+ * the final output.</p>
+ *
+ * @since 1.8
+ */
+ public void setContentMethods(Iterable<SevenZMethod> methods) {
+ this.contentMethods = methods;
}
/**
@@ -238,10 +260,10 @@ public class SevenZOutputFile implements
private CountingOutputStream setupFileOutputStream() throws IOException {
OutputStream out = new OutputStreamWrapper();
- return new CountingOutputStream(Coders
- .addEncoder(out,
- contentCompression,
- null)) {
+ for (SevenZMethod m : reverse(contentMethods)) {
+ out = Coders.addEncoder(out, m, null);
+ }
+ return new CountingOutputStream(out) {
@Override
public void write(final int b) throws IOException {
super.write(b);
@@ -263,6 +285,14 @@ public class SevenZOutputFile implements
};
}
+ private static <T> Iterable<T> reverse(Iterable<T> i) {
+ LinkedList<T> l = new LinkedList();
+ for (T t : i) {
+ l.addFirst(t);
+ }
+ return l;
+ }
+
private void writeHeader(final DataOutput header) throws IOException {
header.write(NID.kHeader);
@@ -336,22 +366,28 @@ public class SevenZOutputFile implements
}
private void writeFolder(final DataOutput header) throws IOException {
- // one coder
- writeUint64(header, 1);
- byte[] id = contentCompression.getId();
- byte[] properties = contentCompression.getProperties();
-
- int codecFlags = id.length;
- if (properties.length > 0) {
- codecFlags |= 0x20;
- }
- header.write(codecFlags);
- header.write(id);
-
- if (properties.length > 0) {
- header.write(properties.length);
- header.write(properties);
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ int numCoders = 0;
+ for (SevenZMethod m : contentMethods) {
+ numCoders++;
+ byte[] id = m.getId();
+ byte[] properties = m.getProperties();
+
+ int codecFlags = id.length;
+ if (properties.length > 0) {
+ codecFlags |= 0x20;
+ }
+ bos.write(codecFlags);
+ bos.write(id);
+
+ if (properties.length > 0) {
+ bos.write(properties.length);
+ bos.write(properties);
+ }
}
+
+ writeUint64(header, numCoders);
+ header.write(bos.toByteArray());
}
private void writeSubStreamsInfo(final DataOutput header) throws
IOException {