From: Nick Burch Date: Tue, 19 Jun 2012 22:50:14 +0000 (+0000) Subject: Add File based constructor to OPCPackage, alongside existing String one (which constr... X-Git-Tag: 3.10-beta1~184 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=ddc3c292e5db37725797c2d0854e0e31e2b8f820;p=poi.git Add File based constructor to OPCPackage, alongside existing String one (which constructed a File from the string internally) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1351894 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index e605f9ee6c..7ef684155f 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Add File based constructor to OPCPackage, alongside existing String one (which constructed a File from the string internally) 53389 - Handle formatting General and @ formats even if a locale is prefixed to them 53271 - Removed unconditional asserts in SXSSF 53025 - Updatad documentation and example on using Data Validations diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java index 7a0da815b7..26c17c5404 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -186,6 +186,20 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { return open(path, defaultPackageAccess); } + /** + * Open a package with read/write permission. + * + * @param file + * The file to open. + * @return A Package object, else null. + * @throws InvalidFormatException + * If the specified file doesn't exist, and a parsing error + * occur. + */ + public static OPCPackage open(File file) throws InvalidFormatException { + return open(file, defaultPackageAccess); + } + /** * Open a package. * @@ -212,6 +226,31 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { return pack; } + /** + * Open a package. + * + * @param file + * The file to open. + * @param access + * PackageBase access. + * @return A PackageBase object, else null. + * @throws InvalidFormatException + * If the specified file doesn't exist, and a parsing error + * occur. + */ + public static OPCPackage open(File file, PackageAccess access) + throws InvalidFormatException { + if (file == null|| (file.exists() && file.isDirectory())) + throw new IllegalArgumentException("file"); + + OPCPackage pack = new ZipPackage(file, access); + if (pack.partList == null && access != PackageAccess.WRITE) { + pack.getParts(); + } + pack.originalPackagePath = file.getAbsolutePath(); + return pack; + } + /** * Open a package. * diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java index 5ac16d3a0b..bf98cdd29b 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java @@ -85,30 +85,55 @@ public final class ZipPackage extends Package { ); } - /** - * Constructor. Opens a Zip based Open XML document. - * - * @param path - * The path of the file to open or create. - * @param access - * The package access mode. - * @throws InvalidFormatException - * If the content type part parsing encounters an error. - */ - ZipPackage(String path, PackageAccess access) { - super(access); - - ZipFile zipFile = null; - - try { - zipFile = ZipHelper.openZipFile(path); - } catch (IOException e) { - throw new InvalidOperationException( - "Can't open the specified file: '" + path + "'", e); - } - - this.zipArchive = new ZipFileZipEntrySource(zipFile); - } + /** + * Constructor. Opens a Zip based Open XML document. + * + * @param path + * The path of the file to open or create. + * @param access + * The package access mode. + * @throws InvalidFormatException + * If the content type part parsing encounters an error. + */ + ZipPackage(String path, PackageAccess access) { + super(access); + + ZipFile zipFile = null; + + try { + zipFile = ZipHelper.openZipFile(path); + } catch (IOException e) { + throw new InvalidOperationException( + "Can't open the specified file: '" + path + "'", e); + } + + this.zipArchive = new ZipFileZipEntrySource(zipFile); + } + + /** + * Constructor. Opens a Zip based Open XML document. + * + * @param file + * The file to open or create. + * @param access + * The package access mode. + * @throws InvalidFormatException + * If the content type part parsing encounters an error. + */ + ZipPackage(File file, PackageAccess access) { + super(access); + + ZipFile zipFile = null; + + try { + zipFile = ZipHelper.openZipFile(file); + } catch (IOException e) { + throw new InvalidOperationException( + "Can't open the specified file: '" + file + "'", e); + } + + this.zipArchive = new ZipFileZipEntrySource(zipFile); + } /** * Retrieves the parts from this package. We assume that the package has not diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java index e808dc61e9..9598b05cbe 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/internal/ZipHelper.java @@ -72,11 +72,12 @@ public final class ZipHelper { * Retrieve the Zip entry of the content types part. */ public static ZipEntry getContentTypeZipEntry(ZipPackage pkg) { - Enumeration entries = pkg.getZipArchive().getEntries(); + Enumeration entries = pkg.getZipArchive().getEntries(); + // Enumerate through the Zip entries until we find the one named // '[Content_Types].xml'. while (entries.hasMoreElements()) { - ZipEntry entry = (ZipEntry) entries.nextElement(); + ZipEntry entry = entries.nextElement(); if (entry.getName().equals( ContentTypeManager.CONTENT_TYPES_PART_NAME)) return entry; @@ -141,6 +142,21 @@ public final class ZipHelper { } } + /** + * Opens the specified file as a zip, or returns null if no such file exists + * + * @param file + * The file to open. + * @return The zip archive freshly open. + */ + public static ZipFile openZipFile(File file) throws IOException { + if (!file.exists()) { + return null; + } + + return new ZipFile(file); + } + /** * Retrieve and open a zip file with the specified path. * diff --git a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java index 41f70d92bb..56a9f89028 100644 --- a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java +++ b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java @@ -87,7 +87,7 @@ public class WorkbookFactory { NPOIFSFileSystem fs = new NPOIFSFileSystem(file); return new HSSFWorkbook(fs.getRoot(), true); } catch(OfficeXmlFileException e) { - OPCPackage pkg = OPCPackage.openOrCreate(file); + OPCPackage pkg = OPCPackage.open(file); return new XSSFWorkbook(pkg); } }