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
This commit is contained in:
Nick Burch 2012-06-19 22:50:14 +00:00
parent 6de1dc574e
commit 7aea48961d
5 changed files with 105 additions and 24 deletions

View File

@ -34,6 +34,7 @@
<changes> <changes>
<release version="3.9-beta1" date="2012-??-??"> <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">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="fix">53271 - Removed unconditional asserts in SXSSF</action>
<action dev="poi-developers" type="add">53025 - Updatad documentation and example on using Data Validations </action> <action dev="poi-developers" type="add">53025 - Updatad documentation and example on using Data Validations </action>

View File

@ -186,6 +186,20 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
return open(path, defaultPackageAccess); 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. * Open a package.
* *
@ -212,6 +226,31 @@ public abstract class OPCPackage implements RelationshipSource, Closeable {
return pack; 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. * Open a package.
* *

View File

@ -110,6 +110,31 @@ public final class ZipPackage extends Package {
this.zipArchive = new ZipFileZipEntrySource(zipFile); 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 * Retrieves the parts from this package. We assume that the package has not
* been yet inspect to retrieve all the parts, this method will open the * been yet inspect to retrieve all the parts, this method will open the

View File

@ -72,11 +72,12 @@ public final class ZipHelper {
* Retrieve the Zip entry of the content types part. * Retrieve the Zip entry of the content types part.
*/ */
public static ZipEntry getContentTypeZipEntry(ZipPackage pkg) { 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 // Enumerate through the Zip entries until we find the one named
// '[Content_Types].xml'. // '[Content_Types].xml'.
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement(); ZipEntry entry = entries.nextElement();
if (entry.getName().equals( if (entry.getName().equals(
ContentTypeManager.CONTENT_TYPES_PART_NAME)) ContentTypeManager.CONTENT_TYPES_PART_NAME))
return entry; 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. * Retrieve and open a zip file with the specified path.
* *

View File

@ -87,7 +87,7 @@ public class WorkbookFactory {
NPOIFSFileSystem fs = new NPOIFSFileSystem(file); NPOIFSFileSystem fs = new NPOIFSFileSystem(file);
return new HSSFWorkbook(fs.getRoot(), true); return new HSSFWorkbook(fs.getRoot(), true);
} catch(OfficeXmlFileException e) { } catch(OfficeXmlFileException e) {
OPCPackage pkg = OPCPackage.openOrCreate(file); OPCPackage pkg = OPCPackage.open(file);
return new XSSFWorkbook(pkg); return new XSSFWorkbook(pkg);
} }
} }