Class OPCPackage

java.lang.Object
org.apache.poi.openxml4j.opc.OPCPackage
All Implemented Interfaces:
Closeable, AutoCloseable, RelationshipSource
Direct Known Subclasses:
ZipPackage

public abstract class OPCPackage extends Object implements RelationshipSource, Closeable
Represents a container that can store multiple data objects.
  • Method Details

    • open

      public static OPCPackage open(String path) throws InvalidFormatException
      Open a package with read/write permission.
      Parameters:
      path - The document path.
      Returns:
      A Package object, else null.
      Throws:
      InvalidFormatException - If the specified file doesn't exist, and a parsing error occur.
    • open

      public static OPCPackage open(File file) throws InvalidFormatException
      Open a package with read/write permission.
      Parameters:
      file - The file to open.
      Returns:
      A Package object, else null.
      Throws:
      InvalidFormatException - If the specified file doesn't exist, and a parsing error occur.
    • open

      public static OPCPackage open(ZipEntrySource zipEntry) throws InvalidFormatException
      Open an user provided ZipEntrySource with read-only permission. This method can be used to stream data into POI. Opposed to other open variants, the data is read as-is, e.g. there aren't any zip-bomb protection put in place.
      Parameters:
      zipEntry - the custom source
      Returns:
      A Package object
      Throws:
      InvalidFormatException - if a parsing error occur.
    • open

      Open a package.
      Parameters:
      path - The document path.
      access - PackageBase access.
      Returns:
      A PackageBase object, else null.
      Throws:
      InvalidFormatException - If the specified file doesn't exist, and a parsing error occur.
      InvalidOperationException - If the zip file cannot be opened.
      InvalidFormatException - if the package is not valid.
    • open

      public static OPCPackage open(File file, PackageAccess access) throws InvalidFormatException
      Open a package.
      Parameters:
      file - The file to open.
      access - PackageBase access.
      Returns:
      A PackageBase object, else null.
      Throws:
      InvalidFormatException - If the specified file doesn't exist, and a parsing error occur.
    • open

      public static OPCPackage open(InputStream in) throws InvalidFormatException, IOException
      Open a package. Note - uses quite a bit more memory than open(String), which doesn't need to hold the whole zip file in memory, and can take advantage of native methods
      Parameters:
      in - The InputStream to read the package from
      Returns:
      A PackageBase object
      Throws:
      InvalidFormatException - Throws if the specified file exist and is not valid.
      IOException - If reading the stream fails
    • openOrCreate

      public static OPCPackage openOrCreate(File file) throws InvalidFormatException
      Opens a package if it exists, else it creates one.
      Parameters:
      file - The file to open or to create.
      Returns:
      A newly created package if the specified file does not exist, else the package extract from the file.
      Throws:
      InvalidFormatException - Throws if the specified file exist and is not valid.
    • create

      public static OPCPackage create(String path)
      Creates a new package.
      Parameters:
      path - Path of the document.
      Returns:
      A newly created PackageBase ready to use.
    • create

      public static OPCPackage create(File file)
      Creates a new package.
      Parameters:
      file - Path of the document.
      Returns:
      A newly created PackageBase ready to use.
    • create

      public static OPCPackage create(OutputStream output)
    • flush

      public void flush()
      Flush the package : save all.
      See Also:
    • close

      public void close() throws IOException
      Close the open, writable package and save its content. If your package is open read only, then you should call revert() when finished with the package. This method is not thread-safe.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException - If an IO exception occur during the saving process.
    • revert

      public void revert()
      Close the package WITHOUT saving its content. Reinitialize this package and cancel all changes done to it.
    • addThumbnail

      public void addThumbnail(String path) throws IOException
      Add a thumbnail to the package. This method is provided to make easier the addition of a thumbnail in a package. You can do the same work by using the traditionnal relationship and part mechanism.
      Parameters:
      path - The full path to the image file.
      Throws:
      IOException
    • addThumbnail

      public void addThumbnail(String filename, InputStream data) throws IOException
      Add a thumbnail to the package. This method is provided to make easier the addition of a thumbnail in a package. You can do the same work by using the traditionnal relationship and part mechanism.
      Parameters:
      filename - The full path to the image file.
      data - the image data
      Throws:
      IOException
    • getPackageProperties

      public PackageProperties getPackageProperties() throws InvalidFormatException
      Retrieves or creates if none exists, core package property part.
      Returns:
      The PackageProperties part of this package.
      Throws:
      InvalidFormatException
    • getPart

      public PackagePart getPart(PackagePartName partName)
      Retrieve a part identified by its name.
      Parameters:
      partName - Part name of the part to retrieve.
      Returns:
      The part with the specified name, else null.
    • getPartsByContentType

      public ArrayList<PackagePart> getPartsByContentType(String contentType)
      Retrieve parts by content type.
      Parameters:
      contentType - The content type criteria.
      Returns:
      All part associated to the specified content type.
    • getPartsByRelationshipType

      public ArrayList<PackagePart> getPartsByRelationshipType(String relationshipType)
      Retrieve parts by relationship type.
      Parameters:
      relationshipType - Relationship type. Must not be null.
      Returns:
      All parts which are the target of a relationship with the specified type. If no such parts are found, the list is empty.
      Throws:
      InvalidOperationException - If called on a write-only package.
      IllegalArgumentException - if relationshipType input param is null.
    • getPartsByName

      public List<PackagePart> getPartsByName(Pattern namePattern)
      Retrieve parts by name
      Parameters:
      namePattern - The pattern for matching the names
      Returns:
      All parts associated to the specified content type, sorted in alphanumerically by the part-name
    • getPart

      public PackagePart getPart(PackageRelationship partRel)
      Get the target part from the specified relationship.
      Parameters:
      partRel - The part relationship uses to retrieve the part.
    • getParts

      public ArrayList<PackagePart> getParts() throws InvalidFormatException
      Load the parts of the archive if it has not been done yet. The relationships of each part are not loaded. Note - Rule M4.1 states that there may only ever be one Core Properties Part, but Office produced files will sometimes have multiple! As Office ignores all but the first, we relax Compliance with Rule M4.1, and ignore all others silently too.
      Returns:
      All this package's parts.
      Throws:
      InvalidFormatException - if the package is not valid.
    • createPart

      public PackagePart createPart(PackagePartName partName, String contentType)
      Create and add a part, with the specified name and content type, to the package.
      Parameters:
      partName - Part name.
      contentType - Part content type.
      Returns:
      The newly created part.
      Throws:
      PartAlreadyExistsException - If rule M1.12 is not verified : Packages shall not contain equivalent part names and package implementers shall neither create nor recognize packages with equivalent part names.
      See Also:
      • createPartImpl(PackagePartName, String, boolean)
    • createPart

      public PackagePart createPart(PackagePartName partName, String contentType, ByteArrayOutputStream content)
      Add a part to the package.
      Parameters:
      partName - Part name of the part to create.
      contentType - type associated with the file
      content - the contents to add. In order to have faster operation in document merge, the data are stored in memory not on a hard disk
      Returns:
      The new part.
      See Also:
    • removePart

      public void removePart(PackagePart part)
      Remove the specified part in this package. If this part is relationship part, then delete all relationships in the source part.
      Parameters:
      part - The part to remove. If null, skip the action.
      See Also:
    • removePart

      public void removePart(PackagePartName partName)
      Remove a part in this package. If this part is relationship part, then delete all relationships in the source part.
      Parameters:
      partName - The part name of the part to remove.
    • removePartRecursive

      public void removePartRecursive(PackagePartName partName) throws InvalidFormatException
      Remove a part from this package as well as its relationship part, if one exists, and all parts listed in the relationship part. Be aware that this do not delete relationships which target the specified part.
      Parameters:
      partName - The name of the part to delete.
      Throws:
      InvalidFormatException - Throws if the associated relationship part of the specified part is not valid.
    • deletePart

      public void deletePart(PackagePartName partName)
      Delete the part with the specified name and its associated relationships part if one exists. Prefer the use of this method to delete a part in the package, compare to the remove() methods that don't remove associated relationships part.
      Parameters:
      partName - Name of the part to delete
    • deletePartRecursive

      public void deletePartRecursive(PackagePartName partName)
      Delete the part with the specified name and all part listed in its associated relationships part if one exists. This process is recursively apply to all parts in the relationships part of the specified part. Prefer the use of this method to delete a part in the package, compare to the remove() methods that don't remove associated relationships part.
      Parameters:
      partName - Name of the part to delete
    • containPart

      public boolean containPart(PackagePartName partName)
      Check if a part already exists in this package from its name.
      Parameters:
      partName - Part name to check.
      Returns:
      true if the part is logically added to this package, else false.
    • addRelationship

      public PackageRelationship addRelationship(PackagePartName targetPartName, TargetMode targetMode, String relationshipType, String relID)
      Add a relationship to the package (except relationships part). Check rule M4.1 : The format designer shall specify and the format producer shall create at most one core properties relationship for a package. A format consumer shall consider more than one core properties relationship for a package to be an error. If present, the relationship shall target the Core Properties part. Check rule M1.25: The Relationships part shall not have relationships to any other part. Package implementers shall enforce this requirement upon the attempt to create such a relationship and shall treat any such relationship as invalid.
      Specified by:
      addRelationship in interface RelationshipSource
      Parameters:
      targetPartName - Target part name.
      targetMode - Target mode, either Internal or External.
      relationshipType - Relationship type.
      relID - ID of the relationship.
      Returns:
      The newly created and added relationship
      See Also:
    • addRelationship

      public PackageRelationship addRelationship(PackagePartName targetPartName, TargetMode targetMode, String relationshipType)
      Add a package relationship.
      Specified by:
      addRelationship in interface RelationshipSource
      Parameters:
      targetPartName - Target part name.
      targetMode - Target mode, either Internal or External.
      relationshipType - Relationship type.
      Returns:
      The newly created and added relationship
      See Also:
    • addExternalRelationship

      public PackageRelationship addExternalRelationship(String target, String relationshipType)
      Adds an external relationship to a part (except relationships part). The targets of external relationships are not subject to the same validity checks that internal ones are, as the contents is potentially any file, URL or similar.
      Specified by:
      addExternalRelationship in interface RelationshipSource
      Parameters:
      target - External target of the relationship
      relationshipType - Type of relationship.
      Returns:
      The newly created and added relationship
    • addExternalRelationship

      public PackageRelationship addExternalRelationship(String target, String relationshipType, String id)
      Adds an external relationship to a part (except relationships part). The targets of external relationships are not subject to the same validity checks that internal ones are, as the contents is potentially any file, URL or similar.
      Specified by:
      addExternalRelationship in interface RelationshipSource
      Parameters:
      target - External target of the relationship
      relationshipType - Type of relationship.
      id - Relationship unique id.
      Returns:
      The newly created and added relationship
      See Also:
    • removeRelationship

      public void removeRelationship(String id)
      Delete a relationship from this package.
      Specified by:
      removeRelationship in interface RelationshipSource
      Parameters:
      id - Id of the relationship to delete.
    • getRelationships

      public PackageRelationshipCollection getRelationships()
      Retrieves all package relationships.
      Specified by:
      getRelationships in interface RelationshipSource
      Returns:
      All package relationships of this package.
      Throws:
      InvalidOperationException - if a read operation is done on a write only package.
      See Also:
      • getRelationshipsHelper(String)
    • getRelationshipsByType

      public PackageRelationshipCollection getRelationshipsByType(String relationshipType)
      Retrieves all relationships with the specified type.
      Specified by:
      getRelationshipsByType in interface RelationshipSource
      Parameters:
      relationshipType - The filter specifying the relationship type.
      Returns:
      All relationships with the specified relationship type.
    • clearRelationships

      public void clearRelationships()
      Clear package relationships.
      Specified by:
      clearRelationships in interface RelationshipSource
    • ensureRelationships

      public void ensureRelationships()
      Ensure that the relationships collection is not null.
    • getRelationship

      public PackageRelationship getRelationship(String id)
      Description copied from interface: RelationshipSource
      Retrieves a package relationship from its id.
      Specified by:
      getRelationship in interface RelationshipSource
      Parameters:
      id - ID of the package relationship to retrieve.
      Returns:
      The package relationship
    • hasRelationships

      public boolean hasRelationships()
      Description copied from interface: RelationshipSource
      Knows if the part have any relationships.
      Specified by:
      hasRelationships in interface RelationshipSource
      Returns:
      true if the part have at least one relationship else false.
    • isRelationshipExists

      public boolean isRelationshipExists(PackageRelationship rel)
      Description copied from interface: RelationshipSource
      Checks if the specified relationship is part of this package part.
      Specified by:
      isRelationshipExists in interface RelationshipSource
      Parameters:
      rel - The relationship to check.
      Returns:
      true if the specified relationship exists in this part, else returns false
    • addMarshaller

      public void addMarshaller(String contentType, PartMarshaller marshaller)
      Add a marshaller.
      Parameters:
      contentType - The content type to bind to the specified marshaller.
      marshaller - The marshaller to register with the specified content type.
    • addUnmarshaller

      public void addUnmarshaller(String contentType, PartUnmarshaller unmarshaller)
      Add an unmarshaller.
      Parameters:
      contentType - The content type to bind to the specified unmarshaller.
      unmarshaller - The unmarshaller to register with the specified content type.
    • removeMarshaller

      public void removeMarshaller(String contentType)
      Remove a marshaller by its content type.
      Parameters:
      contentType - The content type associated with the marshaller to remove.
    • removeUnmarshaller

      public void removeUnmarshaller(String contentType)
      Remove an unmarshaller by its content type.
      Parameters:
      contentType - The content type associated with the unmarshaller to remove.
    • getPackageAccess

      public PackageAccess getPackageAccess()
      Get the package access mode.
      Returns:
      the packageAccess The current package access.
    • validatePackage

      @NotImplemented public boolean validatePackage(OPCPackage pkg) throws InvalidFormatException
      Validates the package compliance with the OPC specifications.
      Returns:
      true if the package is valid else false
      Throws:
      InvalidFormatException
    • save

      public void save(File targetFile) throws IOException
      Save the document in the specified file.
      Parameters:
      targetFile - Destination file.
      Throws:
      IOException - Throws if an IO exception occur.
      See Also:
    • save

      public void save(OutputStream outputStream) throws IOException
      Save the document in the specified output stream.
      Parameters:
      outputStream - The stream to save the package.
      Throws:
      IOException
      See Also:
      • saveImpl(OutputStream)
    • replaceContentType

      public boolean replaceContentType(String oldContentType, String newContentType)
      Replace a content type in this package.

      A typical scneario to call this method is to rename a template file to the main format, e.g.

      • ".dotx" to ".docx"
      • ".dotm" to ".docm"
      • ".xltx" to ".xlsx"
      • ".xltm" to ".xlsm"
      • ".potx" to ".pptx"
      • ".potm" to ".pptm"
      For example, a code converting a .xlsm macro workbook to .xlsx would look as follows:
      
      
        OPCPackage pkg = OPCPackage.open(new FileInputStream("macro-workbook.xlsm"));
        pkg.replaceContentType(
            "application/vnd.ms-excel.sheet.macroEnabled.main+xml",
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
      
        try (FileOutputStream out = new FileOutputStream("workbook.xlsx")) {
          pkg.save(out);
        }
      
       
      Parameters:
      oldContentType - the content type to be replaced
      newContentType - the replacement
      Returns:
      whether replacement was successful
      Since:
      POI-3.8
    • registerPartAndContentType

      public void registerPartAndContentType(PackagePart part)
      Add the specified part, and register its content type with the content type manager.
      Parameters:
      part - The part to add.
    • unregisterPartAndContentType

      public void unregisterPartAndContentType(PackagePartName partName)
      Remove the specified part, and clear its content type from the content type manager.
      Parameters:
      partName - The part name of the part to remove.
    • getUnusedPartIndex

      public int getUnusedPartIndex(String nameTemplate) throws InvalidFormatException
      Get an unused part index based on the namePattern, which doesn't exist yet and has the lowest positive index
      Parameters:
      nameTemplate - The template for new part names containing a '#' for the index, e.g. "/ppt/slides/slide#.xml"
      Returns:
      the next available part name index
      Throws:
      InvalidFormatException - if the nameTemplate is null or doesn't contain the index char (#) or results in an invalid part name
    • isStrictOoxmlFormat

      public boolean isStrictOoxmlFormat()
      Returns:
      true if the package is in Strict OOXML format
      Since:
      POI 5.1.0
    • isClosed

      public abstract boolean isClosed()
      Has close been called already?
    • toString

      public String toString()
      Overrides:
      toString in class Object