Push all the zip opening logic into ZipHelper

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1735064 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2016-03-15 12:03:30 +00:00
parent 0fab07ccfc
commit 700fe05e34
2 changed files with 23 additions and 13 deletions

View File

@ -24,7 +24,6 @@ import java.io.OutputStream;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@ -42,7 +41,6 @@ import org.apache.poi.openxml4j.opc.internal.marshallers.ZipPartMarshaller;
import org.apache.poi.openxml4j.util.ZipEntrySource; import org.apache.poi.openxml4j.util.ZipEntrySource;
import org.apache.poi.openxml4j.util.ZipFileZipEntrySource; import org.apache.poi.openxml4j.util.ZipFileZipEntrySource;
import org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource; import org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource;
import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.openxml4j.util.ZipSecureFile.ThresholdInputStream; import org.apache.poi.openxml4j.util.ZipSecureFile.ThresholdInputStream;
import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger; import org.apache.poi.util.POILogger;
@ -89,9 +87,8 @@ public final class ZipPackage extends Package {
*/ */
ZipPackage(InputStream in, PackageAccess access) throws IOException { ZipPackage(InputStream in, PackageAccess access) throws IOException {
super(access); super(access);
InputStream zis = new ZipInputStream(in); ThresholdInputStream zis = ZipHelper.openZipStream(in);
ThresholdInputStream tis = ZipSecureFile.addThreshold(zis); this.zipArchive = new ZipInputStreamZipEntrySource(zis);
this.zipArchive = new ZipInputStreamZipEntrySource(tis);
} }
/** /**

View File

@ -19,17 +19,20 @@ package org.apache.poi.openxml4j.opc.internal;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile; import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.PackageRelationship; import org.apache.poi.openxml4j.opc.PackageRelationship;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
import org.apache.poi.openxml4j.opc.ZipPackage; import org.apache.poi.openxml4j.opc.ZipPackage;
import org.apache.poi.openxml4j.util.ZipSecureFile; import org.apache.poi.openxml4j.util.ZipSecureFile;
import org.apache.poi.openxml4j.util.ZipSecureFile.ThresholdInputStream;
public final class ZipHelper { public final class ZipHelper {
/** /**
@ -143,7 +146,21 @@ public final class ZipHelper {
} }
/** /**
* Opens the specified file as a zip, or returns null if no such file exists * Opens the specified stream as a secure zip
*
* @param stream
* The stream to open.
* @return The zip stream freshly open.
*/
public static ThresholdInputStream openZipStream(InputStream stream) throws IOException {
InputStream zis = new ZipInputStream(stream);
ThresholdInputStream tis = ZipSecureFile.addThreshold(zis);
return tis;
}
/**
* Opens the specified file as a secure zip, or returns null if no
* such file exists
* *
* @param file * @param file
* The file to open. * The file to open.
@ -158,7 +175,7 @@ public final class ZipHelper {
} }
/** /**
* Retrieve and open a zip file with the specified path. * Retrieve and open as a secure zip file with the specified path.
* *
* @param path * @param path
* The file path. * The file path.
@ -167,10 +184,6 @@ public final class ZipHelper {
public static ZipFile openZipFile(String path) throws IOException { public static ZipFile openZipFile(String path) throws IOException {
File f = new File(path); File f = new File(path);
if (!f.exists()) { return openZipFile(f);
return null;
}
return new ZipSecureFile(f);
} }
} }