/** * 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); //} }
/** * 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); } }