Permalink
Browse files

Resolve issue #229 by conditionally using ZipFile

  • Loading branch information...
ndw committed Apr 14, 2017
1 parent 9f50de5 commit 3ea910c8e56af2e60c999536e5c81b3ba3df79f1
Showing with 68 additions and 40 deletions.
  1. +68 −40 src/main/java/com/xmlcalabash/extensions/Zip.java
@@ -29,6 +29,7 @@
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
@@ -41,6 +42,7 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.LinkedHashMap;
@@ -49,6 +51,7 @@
import java.util.zip.CRC32;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
@@ -186,9 +189,23 @@ public void load(URI id, String media, InputStream content, long len)
final DatatypeFactory dfactory = DatatypeFactory.newInstance();
DataStore store = runtime.getDataStore();
store.readEntry(zipFn, zipFn, "application/zip, */*", null, new DataReader() {
public void load(URI id, String media, InputStream stream,
long len) throws IOException {
read(id, stream, dfactory);
public void load(URI id, String media, InputStream stream, long len) throws IOException {
TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(step.getNode().getBaseURI());
tree.addStartElement(c_zipfile);
tree.addAttribute(_href, id.toASCIIString());
tree.startContent();
if (zipFn.startsWith("file:/")) {
readFile(tree, id, zipFn, dfactory);
} else {
readStream(tree, id, stream, dfactory);
}
tree.addEndElement();
tree.endDocument();
result.write(tree.getResult());
}
});
} catch (MalformedURLException mue) {
@@ -200,7 +217,7 @@ public void load(URI id, String media, InputStream stream,
}
}
void update(ZipInputStream inZip, final ZipOutputStream outZip) {
private void update(ZipInputStream inZip, final ZipOutputStream outZip) {
String command = getOption(_command).getString();
if ("create".equals(command)) {
@@ -225,52 +242,63 @@ void update(ZipInputStream inZip, final ZipOutputStream outZip) {
}
}
void read(URI id, InputStream stream, final DatatypeFactory dfactory)
private void readFile(TreeWriter tree, URI id, String zipFn, final DatatypeFactory dfactory)
throws IOException {
TreeWriter tree = new TreeWriter(runtime);
tree.startDocument(step.getNode().getBaseURI());
tree.addStartElement(c_zipfile);
tree.addAttribute(_href, id.toASCIIString());
tree.startContent();
ZipInputStream zipStream = new ZipInputStream(stream);
ZipFile zipFile = null;
try {
GregorianCalendar cal = new GregorianCalendar();
File uriFile = new File(new URI(zipFn));
zipFile = new ZipFile(uriFile);
ZipEntry entry = zipStream.getNextEntry();
while (entry != null) {
cal.setTimeInMillis(entry.getTime());
XMLGregorianCalendar xmlCal = dfactory.newXMLGregorianCalendar(cal);
Enumeration<? extends ZipEntry> zipEntryEnum = zipFile.entries();
while (zipEntryEnum.hasMoreElements()) {
ZipEntry entry = zipEntryEnum.nextElement();
processEntry(tree, entry, dfactory);
}
} catch (URISyntaxException e) {
throw new XProcException(e);
} finally {
if (zipFile != null) {
zipFile.close();
}
}
}
if (entry.isDirectory()) {
tree.addStartElement(c_directory);
} else {
tree.addStartElement(c_file);
private void readStream(TreeWriter tree, URI id, InputStream stream, final DatatypeFactory dfactory)
throws IOException {
ZipInputStream zipStream = new ZipInputStream(stream);
tree.addAttribute(_compressed_size, ""+entry.getCompressedSize());
tree.addAttribute(_size, ""+entry.getSize());
}
try {
ZipEntry entry = zipStream.getNextEntry();
while (entry != null) {
processEntry(tree, entry, dfactory);
}
} finally {
zipStream.close();
}
}
if (entry.getComment() != null) {
tree.addAttribute(_comment, entry.getComment());
}
private void processEntry(TreeWriter tree, ZipEntry entry, DatatypeFactory dfactory) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTimeInMillis(entry.getTime());
XMLGregorianCalendar xmlCal = dfactory.newXMLGregorianCalendar(cal);
tree.addAttribute(_name, ""+entry.getName());
tree.addAttribute(_date, xmlCal.toXMLFormat());
tree.startContent();
tree.addEndElement();
entry = zipStream.getNextEntry();
}
if (entry.isDirectory()) {
tree.addStartElement(c_directory);
} else {
tree.addStartElement(c_file);
tree.addEndElement();
tree.endDocument();
result.write(tree.getResult());
tree.addAttribute(_compressed_size, ""+entry.getCompressedSize());
tree.addAttribute(_size, ""+entry.getSize());
}
} finally {
zipStream.close();
if (entry.getComment() != null) {
tree.addAttribute(_comment, entry.getComment());
}
tree.addAttribute(_name, ""+entry.getName());
tree.addAttribute(_date, xmlCal.toXMLFormat());
tree.startContent();
tree.addEndElement();
}
private void parseManifest(XdmNode man) {

0 comments on commit 3ea910c

Please sign in to comment.