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 2014/02/26 14:16:50 UTC
svn commit: r1572043 - in /commons/proper/compress/trunk/src:
main/java/org/apache/commons/compress/archivers/sevenz/
test/java/org/apache/commons/compress/archivers/sevenz/
Author: bodewig
Date: Wed Feb 26 13:16:50 2014
New Revision: 1572043
URL: http://svn.apache.org/r1572043
Log:
COMPRESS-258 provide access to methods that have been used when creating the archive - method part, configuration properties to follow
Modified:
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java?rev=1572043&r1=1572042&r2=1572043&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/Coders.java Wed Feb 26 13:16:50 2014
@@ -49,6 +49,14 @@ class Coders {
}
return null;
}
+ static SevenZMethod findBySignature(byte[] sig) {
+ for (final CoderId coderId : coderTable) {
+ if (Arrays.equals(coderId.method.getId(), sig)) {
+ return coderId.method;
+ }
+ }
+ return null;
+ }
static InputStream addDecoder(final InputStream is,
final Coder coder, final byte[] password) throws IOException {
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java?rev=1572043&r1=1572042&r2=1572043&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java (original)
+++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java Wed Feb 26 13:16:50 2014
@@ -27,6 +27,7 @@ import java.io.InputStream;
import java.io.RandomAccessFile;
import java.util.Arrays;
import java.util.BitSet;
+import java.util.LinkedList;
import java.util.zip.CRC32;
import org.apache.commons.compress.utils.BoundedInputStream;
@@ -800,9 +801,11 @@ public class SevenZFile implements Close
new ByteArrayInputStream(new byte[0]), 0);
return;
}
+ final SevenZArchiveEntry file = archive.files[currentEntryIndex];
if (currentFolderIndex == folderIndex) {
// need to advance the folder input stream past the current file
drainPreviousEntry();
+ file.setContentMethods(archive.files[currentEntryIndex - 1].getContentMethods());
} else {
currentFolderIndex = folderIndex;
if (currentFolderInputStream != null) {
@@ -814,9 +817,8 @@ public class SevenZFile implements Close
final int firstPackStreamIndex = archive.streamMap.folderFirstPackStreamIndex[folderIndex];
final long folderOffset = SIGNATURE_HEADER_SIZE + archive.packPos +
archive.streamMap.packStreamOffsets[firstPackStreamIndex];
- currentFolderInputStream = buildDecoderStack(folder, folderOffset, firstPackStreamIndex);
+ currentFolderInputStream = buildDecoderStack(folder, folderOffset, firstPackStreamIndex, file);
}
- final SevenZArchiveEntry file = archive.files[currentEntryIndex];
final InputStream fileStream = new BoundedInputStream(
currentFolderInputStream, file.getSize());
if (file.getHasCrc()) {
@@ -838,16 +840,20 @@ public class SevenZFile implements Close
}
private InputStream buildDecoderStack(final Folder folder, final long folderOffset,
- final int firstPackStreamIndex) throws IOException {
+ final int firstPackStreamIndex, SevenZArchiveEntry entry) throws IOException {
file.seek(folderOffset);
InputStream inputStreamStack = new BoundedRandomAccessFileInputStream(file,
archive.packSizes[firstPackStreamIndex]);
+ LinkedList<SevenZMethodConfiguration> methods = new LinkedList<SevenZMethodConfiguration>();
for (final Coder coder : folder.getOrderedCoders()) {
if (coder.numInStreams != 1 || coder.numOutStreams != 1) {
throw new IOException("Multi input/output stream coders are not yet supported");
}
+ SevenZMethod method = Coders.findBySignature(coder.decompressionMethodId);
+ methods.addFirst(new SevenZMethodConfiguration(method));
inputStreamStack = Coders.addDecoder(inputStreamStack, coder, password);
}
+ entry.setContentMethods(methods);
if (folder.hasCrc) {
return new CRC32VerifyingInputStream(inputStreamStack,
folder.getUnpackSize(), folder.crc);
Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java?rev=1572043&r1=1572042&r2=1572043&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZFileTest.java Wed Feb 26 13:16:50 2014
@@ -45,21 +45,22 @@ public class SevenZFileTest extends Abst
}
public void test7zUnarchive() throws Exception {
- test7zUnarchive(getFile("bla.7z"));
+ test7zUnarchive(getFile("bla.7z"), SevenZMethod.LZMA);
}
public void test7zDeflateUnarchive() throws Exception {
- test7zUnarchive(getFile("bla.deflate.7z"));
+ test7zUnarchive(getFile("bla.deflate.7z"), SevenZMethod.DEFLATE);
}
public void test7zDecryptUnarchive() throws Exception {
if (isStrongCryptoAvailable()) {
- test7zUnarchive(getFile("bla.encrypted.7z"), "foo".getBytes("UTF-16LE"));
+ test7zUnarchive(getFile("bla.encrypted.7z"), SevenZMethod.LZMA, // stack LZMA + AES
+ "foo".getBytes("UTF-16LE"));
}
}
- private void test7zUnarchive(File f) throws Exception {
- test7zUnarchive(f, null);
+ private void test7zUnarchive(File f, SevenZMethod m) throws Exception {
+ test7zUnarchive(f, m, null);
}
public void testEncryptedArchiveRequiresPassword() throws Exception {
@@ -102,13 +103,15 @@ public class SevenZFileTest extends Abst
(byte) 0xAF, 0x27, 0x1D}, 6));
}
- private void test7zUnarchive(File f, byte[] password) throws Exception {
+ private void test7zUnarchive(File f, SevenZMethod m, byte[] password) throws Exception {
SevenZFile sevenZFile = new SevenZFile(f, password);
try {
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
assertEquals("test1.xml", entry.getName());
+ assertEquals(m, entry.getContentMethods().iterator().next().getMethod());
entry = sevenZFile.getNextEntry();
assertEquals("test2.xml", entry.getName());
+ assertEquals(m, entry.getContentMethods().iterator().next().getMethod());
byte[] contents = new byte[(int)entry.getSize()];
int off = 0;
while ((off < contents.length)) {
Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java
URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java?rev=1572043&r1=1572042&r2=1572043&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java (original)
+++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFileTest.java Wed Feb 26 13:16:50 2014
@@ -19,10 +19,12 @@ package org.apache.commons.compress.arch
import java.io.File;
import java.io.IOException;
+import java.util.Arrays;
+import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
-import java.util.ArrayList;
+import java.util.Iterator;
import org.apache.commons.compress.AbstractTestCase;
import org.tukaani.xz.LZMA2Options;
@@ -257,19 +259,40 @@ public class SevenZOutputFileTest extend
}
public void testLzma2WithIntConfiguration() throws Exception {
- output = new File(dir, "bzip2-options.7z");
+ output = new File(dir, "lzma2-options.7z");
// 1 MB dictionary
createAndReadBack(output, Collections
.singletonList(new SevenZMethodConfiguration(SevenZMethod.LZMA2, 1 << 20)));
}
public void testLzma2WithOptionsConfiguration() throws Exception {
- output = new File(dir, "bzip2-options2.7z");
+ output = new File(dir, "lzma2-options2.7z");
LZMA2Options opts = new LZMA2Options(1);
createAndReadBack(output, Collections
.singletonList(new SevenZMethodConfiguration(SevenZMethod.LZMA2, opts)));
}
+ public void testArchiveWithMixedMethods() throws Exception {
+ output = new File(dir, "mixed-methods.7z");
+ SevenZOutputFile outArchive = new SevenZOutputFile(output);
+ try {
+ addFile(outArchive, 0, true);
+ addFile(outArchive, 1, true, Arrays.asList(new SevenZMethodConfiguration(SevenZMethod.BZIP2)));
+ } finally {
+ outArchive.close();
+ }
+
+ SevenZFile archive = new SevenZFile(output);
+ try {
+ assertEquals(Boolean.TRUE,
+ verifyFile(archive, 0, Arrays.asList(new SevenZMethodConfiguration(SevenZMethod.LZMA2))));
+ assertEquals(Boolean.TRUE,
+ verifyFile(archive, 1, Arrays.asList(new SevenZMethodConfiguration(SevenZMethod.BZIP2))));
+ } finally {
+ archive.close();
+ }
+ }
+
private void testCompress252(int numberOfFiles, int numberOfNonEmptyFiles)
throws Exception {
int nonEmptyModulus = numberOfNonEmptyFiles != 0
@@ -326,14 +349,25 @@ public class SevenZOutputFileTest extend
private void addFile(SevenZOutputFile archive, int index, boolean nonEmpty)
throws Exception {
+ addFile(archive, index, nonEmpty, null);
+ }
+
+ private void addFile(SevenZOutputFile archive, int index, boolean nonEmpty, Iterable<SevenZMethodConfiguration> methods)
+ throws Exception {
SevenZArchiveEntry entry = new SevenZArchiveEntry();
entry.setName("foo/" + index + ".txt");
+ entry.setContentMethods(methods);
archive.putArchiveEntry(entry);
archive.write(nonEmpty ? new byte[] { 'A' } : new byte[0]);
archive.closeArchiveEntry();
}
private Boolean verifyFile(SevenZFile archive, int index) throws Exception {
+ return verifyFile(archive, index, null);
+ }
+
+ private Boolean verifyFile(SevenZFile archive, int index,
+ Iterable<SevenZMethodConfiguration> methods) throws Exception {
SevenZArchiveEntry entry = archive.getNextEntry();
if (entry == null) {
return null;
@@ -346,6 +380,9 @@ public class SevenZOutputFileTest extend
assertEquals(1, entry.getSize());
assertEquals('A', archive.read());
assertEquals(-1, archive.read());
+ if (methods != null) {
+ assertContentMethodsEquals(methods, entry.getContentMethods());
+ }
return Boolean.TRUE;
}
@@ -367,9 +404,23 @@ public class SevenZOutputFileTest extend
SevenZFile archive = new SevenZFile(output);
try {
- assertEquals(Boolean.TRUE, verifyFile(archive, 0));
+ assertEquals(Boolean.TRUE, verifyFile(archive, 0, methods));
} finally {
archive.close();
}
}
+
+ private static void assertContentMethodsEquals(Iterable<? extends SevenZMethodConfiguration> expected,
+ Iterable<? extends SevenZMethodConfiguration> actual) {
+ assertNotNull(actual);
+ Iterator<? extends SevenZMethodConfiguration> expectedIter = expected.iterator();
+ Iterator<? extends SevenZMethodConfiguration> actualIter = actual.iterator();
+ while (expectedIter.hasNext()) {
+ assertTrue(actualIter.hasNext());
+ SevenZMethodConfiguration expConfig = expectedIter.next();
+ SevenZMethodConfiguration actConfig = actualIter.next();
+ assertEquals(expConfig.getMethod(), actConfig.getMethod());
+ }
+ assertFalse(actualIter.hasNext());
+ }
}