Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List encrypted archive entries, if available, in SevenZipParser (#1574) #1575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
Expand All @@ -37,11 +39,13 @@
import iped.parsers.util.Util;
import iped.properties.ExtraProperties;
import iped.utils.IOUtil;
import iped.utils.LocalizedFormat;
import net.sf.sevenzipjbinding.ExtractAskMode;
import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.IArchiveExtractCallback;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.PropID;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
Expand Down Expand Up @@ -141,8 +145,10 @@ public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
ArrayList<Integer> itemsToExtract = new ArrayList<Integer>();
for (int i = 0; i < simpleInArchive.getNumberOfItems(); i++) {
ISimpleInArchiveItem item = simpleInArchive.getArchiveItem(i);
if (item.isEncrypted())
if (item.isEncrypted()) {
listArchiveContent(inArchive, xhtml);
throw new EncryptedDocumentException();
}
if (item.isFolder())
folderMap.put(item.getPath(), i);
else
Expand Down Expand Up @@ -174,6 +180,44 @@ public void parse(InputStream stream, ContentHandler handler, Metadata metadata,
}

}

private void listArchiveContent(IInArchive inArchive, XHTMLContentHandler xhtml) {
int maxNumEntries = 1 << 24;
List<String> entries = new ArrayList<String>();
try {
int numEntries = Math.min(maxNumEntries, inArchive.getNumberOfItems());
DecimalFormat nf = LocalizedFormat.getDecimalInstance("#,##0");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < numEntries; i++) {
sb.append((String) inArchive.getProperty(i, PropID.PATH));
Boolean isFolder = (Boolean) inArchive.getProperty(i, PropID.IS_FOLDER);
if (isFolder != null && isFolder.booleanValue()) {
sb.append(" [FOLDER]");
} else {
Long size = (Long) inArchive.getProperty(i, PropID.SIZE);
if (size != null && size > 0) {
sb.append(" [").append(nf.format(size)).append(" bytes]");
}
}
entries.add(sb.toString());
sb.delete(0, sb.length());
}
} catch (Exception e) {
} finally {
try {
if (!entries.isEmpty()) {
Collections.sort(entries);
xhtml.startElement("pre");
for (String entry : entries) {
xhtml.characters(entry);
xhtml.newline();
}
xhtml.endElement("pre");
}
} catch (Exception e) {
}
}
}

public class MyExtractCallback implements IArchiveExtractCallback {

Expand Down