<changes>
<release version="3.9-beta1" date="2012-??-??">
+ <action dev="poi-developers" type="add">Add File based constructor to OPCPackage, alongside existing String one (which constructed a File from the string internally)</action>
<action dev="poi-developers" type="fix">53389 - Handle formatting General and @ formats even if a locale is prefixed to them</action>
<action dev="poi-developers" type="fix">53271 - Removed unconditional asserts in SXSSF</action>
<action dev="poi-developers" type="add">53025 - Updatad documentation and example on using Data Validations </action>
return open(path, defaultPackageAccess);
}
+ /**
+ * Open a package with read/write permission.
+ *
+ * @param file
+ * The file to open.
+ * @return A Package object, else <b>null</b>.
+ * @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.
*
return pack;
}
+ /**
+ * Open a package.
+ *
+ * @param file
+ * The file to open.
+ * @param access
+ * PackageBase access.
+ * @return A PackageBase object, else <b>null</b>.
+ * @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.
*
);
}
- /**
- * 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
* Retrieve the Zip entry of the content types part.
*/
public static ZipEntry getContentTypeZipEntry(ZipPackage pkg) {
- Enumeration entries = pkg.getZipArchive().getEntries();
+ Enumeration<? extends ZipEntry> 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;
}
}
+ /**
+ * 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.
*
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);
}
}