/** * Save this package into the specified stream * * * @param outputStream * The stream use to save this package. * * @see #save(OutputStream) */ protected override void SaveImpl(Stream outputStream) { // Check that the document was open in write mode ThrowExceptionIfReadOnly(); ZipOutputStream zos = null; //try //{ if (!(outputStream is ZipOutputStream)) { zos = new ZipOutputStream(outputStream); } else { zos = (ZipOutputStream)outputStream; } // If the core properties part does not exist in the part list, // we save it as well if (this.GetPartsByRelationshipType( PackageRelationshipTypes.CORE_PROPERTIES).Count == 0) { logger.Log(POILogger.DEBUG, "Save core properties part"); // We have to save the core properties part ... new ZipPackagePropertiesMarshaller().Marshall( this.packageProperties, zos); // ... and to add its relationship ... this.relationships.AddRelationship(this.packageProperties .PartName.URI, TargetMode.Internal, PackageRelationshipTypes.CORE_PROPERTIES, null); // ... and the content if it has not been added yet. if (!this.contentTypeManager .IsContentTypeRegister(ContentTypes.CORE_PROPERTIES_PART)) { this.contentTypeManager.AddContentType( this.packageProperties.PartName, ContentTypes.CORE_PROPERTIES_PART); } } // Save package relationships part. logger.Log(POILogger.DEBUG, "Save package relationships"); ZipPartMarshaller.MarshallRelationshipPart(this.Relationships, PackagingUriHelper.PACKAGE_RELATIONSHIPS_ROOT_PART_NAME, zos); // Save content type part. logger.Log(POILogger.DEBUG, "Save content types part"); this.contentTypeManager.Save(zos); // Save parts. foreach (PackagePart part in GetParts()) { // If the part is a relationship part, we don't save it, it's // the source part that will do the job. if (part.IsRelationshipPart) { continue; } logger.Log(POILogger.DEBUG, "Save part '" + ZipHelper.GetZipItemNameFromOPCName(part .PartName.Name) + "'"); if (partMarshallers.ContainsKey(part.contentType)) { PartMarshaller marshaller = partMarshallers[part.contentType]; if (!marshaller.Marshall(part, zos)) { throw new OpenXml4NetException( "The part " + part.PartName.URI + " fail to be saved in the stream with marshaller " + marshaller); } } else { if (!defaultPartMarshaller.Marshall(part, zos)) { throw new OpenXml4NetException( "The part " + part.PartName.URI + " fail to be saved in the stream with marshaller " + defaultPartMarshaller); } } } zos.Close(); //} //catch (Exception e) //{ // logger // .Log(POILogger.ERROR,"Fail to save: an error occurs while saving the package : " // + e.Message); //} }
/** * Add a marshaller. * * @param contentType * The content type to bind to the specified marshaller. * @param marshaller * The marshaller to register with the specified content type. */ public void AddMarshaller(String contentType, PartMarshaller marshaller) { try { partMarshallers[new ContentType(contentType)] = marshaller; } catch (InvalidFormatException e) { logger.Log(POILogger.WARN, "The specified content type is not valid: '" + e.Message + "'. The marshaller will not be Added !"); } }
/** * Save this package into the specified stream * * * @param outputStream * The stream use to save this package. * * @see #save(OutputStream) */ protected override void SaveImpl(Stream outputStream) { // Check that the document was open in write mode ThrowExceptionIfReadOnly(); ZipOutputStream zos = null; try { if (!(outputStream is ZipOutputStream)) { zos = new ZipOutputStream(outputStream); } else { zos = (ZipOutputStream)outputStream; } zos.UseZip64 = UseZip64.Off; // If the core properties part does not exist in the part list, // we save it as well if (this.GetPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).Count == 0 && this.GetPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES_ECMA376).Count == 0) { logger.Log(POILogger.DEBUG, "Save core properties part"); // Ensure that core properties are added if missing GetPackageProperties(); // Add core properties to part list ... AddPackagePart(this.packageProperties); // ... and to add its relationship ... this.relationships.AddRelationship(this.packageProperties .PartName.URI, TargetMode.Internal, PackageRelationshipTypes.CORE_PROPERTIES, null); // ... and the content if it has not been added yet. if (!this.contentTypeManager .IsContentTypeRegister(ContentTypes.CORE_PROPERTIES_PART)) { this.contentTypeManager.AddContentType( this.packageProperties.PartName, ContentTypes.CORE_PROPERTIES_PART); } } // Save package relationships part. logger.Log(POILogger.DEBUG, "Save package relationships"); ZipPartMarshaller.MarshallRelationshipPart(this.Relationships, PackagingUriHelper.PACKAGE_RELATIONSHIPS_ROOT_PART_NAME, zos); // Save content type part. logger.Log(POILogger.DEBUG, "Save content types part"); this.contentTypeManager.Save(zos); // Save parts. foreach (PackagePart part in GetParts()) { // If the part is a relationship part, we don't save it, it's // the source part that will do the job. if (part.IsRelationshipPart) { continue; } logger.Log(POILogger.DEBUG, "Save part '" + ZipHelper.GetZipItemNameFromOPCName(part .PartName.Name) + "'"); if (partMarshallers.ContainsKey(part._contentType)) { PartMarshaller marshaller = partMarshallers[part._contentType]; if (!marshaller.Marshall(part, zos)) { throw new OpenXml4NetException( "The part " + part.PartName.URI + " fail to be saved in the stream with marshaller " + marshaller); } } else { if (!defaultPartMarshaller.Marshall(part, zos)) { throw new OpenXml4NetException( "The part " + part.PartName.URI + " fail to be saved in the stream with marshaller " + defaultPartMarshaller); } } } //Finishes writing the contents of the ZIP output stream without closing the underlying stream. if (isStream) { zos.Finish(); //instead of use zos.Close, it will close the stream } else { zos.Close(); } } catch (OpenXML4NetRuntimeException e) { // no need to wrap this type of Exception throw e; } catch (Exception e) { logger.Log(POILogger.ERROR, "fail to save: an error occurs while saving the package : " + e.Message); } }
/** * Initialize the package instance. */ private void Init() { this.partMarshallers = new SortedList<ContentType, PartMarshaller>(5); this.partUnmarshallers = new SortedList<ContentType, PartUnmarshaller>(2); try { // Add 'default' unmarshaller this.partUnmarshallers.Add(new ContentType( ContentTypes.CORE_PROPERTIES_PART), new PackagePropertiesUnmarshaller()); // Add default marshaller this.defaultPartMarshaller = new DefaultMarshaller(); // TODO Delocalize specialized marshallers this.partMarshallers.Add(new ContentType( ContentTypes.CORE_PROPERTIES_PART), new ZipPackagePropertiesMarshaller()); } catch (InvalidFormatException) { // Should never happen throw new OpenXml4NetException( "Package.init() : this exception should never happen, if you read this message please send a mail to the developers team."); } }