本文整理汇总了VB.NET中System.IO.Packaging.Package.Open方法的典型用法代码示例。如果您正苦于以下问题:VB.NET Package.Open方法的具体用法?VB.NET Package.Open怎么用?VB.NET Package.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.Packaging.Package
的用法示例。
在下文中一共展示了Package.Open方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: CreatePackage
' -------------------------- CreatePackage --------------------------
''' <summary>
''' Creates a package zip file containing specified
''' content and resource files.</summary>
Private Shared Sub CreatePackage()
' Convert system path and file names to Part URIs. In this example
' Dim partUriDocument as Uri /* /Content/Document.xml */ =
' PackUriHelper.CreatePartUri(
' New Uri("Content\Document.xml", UriKind.Relative))
' Dim partUriResource as Uri /* /Resources/Image1.jpg */ =
' PackUriHelper.CreatePartUri(
' New Uri("Resources\Image1.jpg", UriKind.Relative))
Dim partUriDocument As Uri = PackUriHelper.CreatePartUri(New Uri(documentPath, UriKind.Relative))
Dim partUriResource As Uri = PackUriHelper.CreatePartUri(New Uri(resourcePath, UriKind.Relative))
' Create the Package
' (If the package file already exists, FileMode.Create will
' automatically delete it first before creating a new one.
' The 'using' statement insures that 'package' is
' closed and disposed when it goes out of scope.)
Using package As Package = Package.Open(packagePath, FileMode.Create)
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)
' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType)
' Add a Resource Part to the Package
Dim packagePartResource As PackagePart = package.CreatePart(partUriResource, System.Net.Mime.MediaTypeNames.Image.Jpeg)
' Copy the data to the Resource Part
Using fileStream As New FileStream(resourcePath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartResource.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add Relationship from the Document part to the Resource part
packagePartDocument.CreateRelationship(New Uri("../resources/image1.jpg", UriKind.Relative), TargetMode.Internal, ResourceRelationshipType)
End Using ' end:using (Package package) - Close and dispose package.
End Sub
' --------------------------- CopyStream ---------------------------
''' <summary>
''' Copies data from a source stream to a target stream.</summary>
''' <param name="source">
''' The source stream to copy from.</param>
''' <param name="target">
''' The destination stream to copy to.</param>
Private Shared Sub CopyStream(ByVal source As Stream, ByVal target As Stream)
Const bufSize As Integer = &H1000
Dim buf(bufSize - 1) As Byte
Dim bytesRead As Integer = 0
bytesRead = source.Read(buf, 0, bufSize)
Do While bytesRead > 0
target.Write(buf, 0, bytesRead)
bytesRead = source.Read(buf, 0, bufSize)
Loop
End Sub
示例2: Uri
' Open the Package.
' ('using' statement insures that 'package' is
' closed and disposed when it goes out of scope.)
Using package As Package = Package.Open(packagePath, FileMode.Open, FileAccess.Read)
Dim documentPart As PackagePart = Nothing
Dim resourcePart As PackagePart = Nothing
' Get the Package Relationships and look for
' the Document part based on the RelationshipType
Dim uriDocumentTarget As Uri = Nothing
For Each relationship As PackageRelationship In package.GetRelationshipsByType(PackageRelationshipType)
' Resolve the Relationship Target Uri
' so the Document Part can be retrieved.
uriDocumentTarget = PackUriHelper.ResolvePartUri(New Uri("/", UriKind.Relative), relationship.TargetUri)
' Open the Document Part, write the contents to a file.
documentPart = package.GetPart(uriDocumentTarget)
ExtractPart(documentPart, targetDirectory)
Next relationship
' Get the Document part's Relationships,
' and look for required resources.
Dim uriResourceTarget As Uri = Nothing
For Each relationship As PackageRelationship In documentPart.GetRelationshipsByType(ResourceRelationshipType)
' Resolve the Relationship Target Uri
' so the Resource Part can be retrieved.
uriResourceTarget = PackUriHelper.ResolvePartUri(documentPart.Uri, relationship.TargetUri)
' Open the Resource Part and write the contents to a file.
resourcePart = package.GetPart(uriResourceTarget)
ExtractPart(resourcePart, targetDirectory)
Next relationship
End Using ' end:using(Package package) - Close & dispose package.