ZipPackage Klasa

Definicja

Implementuje pochodną podklasę abstrakcyjnej Package klasy bazowej — ZipPackage klasa używa archiwum ZIP jako magazynu kontenerów. Klasa ta nie może być dziedziczona.

public ref class ZipPackage sealed : System::IO::Packaging::Package
public sealed class ZipPackage : System.IO.Packaging.Package
type ZipPackage = class
    inherit Package
Public NotInheritable Class ZipPackage
Inherits Package
Dziedziczenie
ZipPackage

Przykłady

W tym przykładzie pokazano, jak utworzyć podstawowy ZipPackageelement .

W przykładzie zostanie utworzony pakiet zawierający pojedynczą część dokumentu, która jest zdefiniowana jako element główny pakietu przez element na poziomie PackageRelationshippakietu.

Pakiet zawiera również część obrazu i drugą PackageRelationship , która definiuje skojarzenie między częścią dokumentu źródłowego a częścią obrazu docelowego. (Obraz jest zasobem używanym z dokumentem).

//  -------------------------- CreatePackage --------------------------
/// <summary>
///   Creates a package zip file containing specified
///   content and resource files.</summary>
private static void CreatePackage()
{
    // Convert system path and file names to Part URIs. In this example
    // Uri partUriDocument /* /Content/Document.xml */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Content\Document.xml", UriKind.Relative));
    // Uri partUriResource /* /Resources/Image1.jpg */ =
    //     PackUriHelper.CreatePartUri(
    //         new Uri("Resources\Image1.jpg", UriKind.Relative));
    Uri partUriDocument = PackUriHelper.CreatePartUri(
                              new Uri(documentPath, UriKind.Relative));
    Uri partUriResource = 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 package =
        Package.Open(packagePath, FileMode.Create))
    {
        // Add the Document part to the Package
        PackagePart packagePartDocument =
            package.CreatePart(partUriDocument,
                           System.Net.Mime.MediaTypeNames.Text.Xml);

        // Copy the data to the Document Part
        using (FileStream fileStream = new FileStream(
               documentPath, FileMode.Open, FileAccess.Read))
        {
            CopyStream(fileStream, packagePartDocument.GetStream());
        }// 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
        PackagePart packagePartResource =
            package.CreatePart(partUriResource,
                           System.Net.Mime.MediaTypeNames.Image.Jpeg);

        // Copy the data to the Resource Part
        using (FileStream fileStream = new FileStream(
               resourcePath, FileMode.Open, FileAccess.Read))
        {
            CopyStream(fileStream, packagePartResource.GetStream());
        }// 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 (Package package) - Close and dispose package.
}// end:CreatePackage()

//  --------------------------- 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 static void CopyStream(Stream source, Stream target)
{
    const int bufSize = 0x1000;
    byte[] buf = new byte[bufSize];
    int bytesRead = 0;
    while ((bytesRead = source.Read(buf, 0, bufSize)) > 0)
        target.Write(buf, 0, bytesRead);
}// end:CopyStream()
'  -------------------------- 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

Uwagi

Pakiet.Open metoda domyślnie używa ZipPackage kontenerów.

Właściwości

FileOpenAccess

Pobiera ustawienie dostępu do pliku dla pakietu.

(Odziedziczone po Package)
PackageProperties

Pobiera podstawowe właściwości pakietu.

(Odziedziczone po Package)

Metody

Close()

Zapisuje i zamyka pakiet oraz wszystkie bazowe strumienie części.

(Odziedziczone po Package)
CreatePart(Uri, String)

Tworzy nową nieskompresowaną część z danym identyfikatorem URI i typem zawartości.

(Odziedziczone po Package)
CreatePart(Uri, String, CompressionOption)

Tworzy nową część z danym identyfikatorem URI, typem zawartości i opcją kompresji.

(Odziedziczone po Package)
CreatePartCore(Uri, String, CompressionOption)

Po przesłonięciu w klasie pochodnej tworzy nową część pakietu.

(Odziedziczone po Package)
CreateRelationship(Uri, TargetMode, String)

Tworzy relację na poziomie pakietu ze częścią z danym identyfikatorem URI, trybem docelowym i typem relacji.

(Odziedziczone po Package)
CreateRelationship(Uri, TargetMode, String, String)

Tworzy relację na poziomie pakietu z częścią z danym identyfikatorem URI, trybem docelowym, typem relacji i identyfikatorem (ID).

(Odziedziczone po Package)
DeletePart(Uri)

Usuwa część z danego identyfikatora URI z pakietu.

(Odziedziczone po Package)
DeletePartCore(Uri)

Gdy zastąpisz klasę pochodną, usuwa część z danym identyfikatorem URI.

(Odziedziczone po Package)
DeleteRelationship(String)

Usuwa relację na poziomie pakietu.

(Odziedziczone po Package)
Dispose(Boolean)

Opróżnia i zapisuje zawartość wszystkich części i relacji, zamyka pakiet i zwalnia wszystkie zasoby.

(Odziedziczone po Package)
Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
Flush()

Zapisuje zawartość wszystkich części i relacji zawartych w pakiecie.

(Odziedziczone po Package)
FlushCore()

Po zastąpieniu klasy pochodnej zapisuje zawartość wszystkich części i relacji w magazynie klas pochodnych.

(Odziedziczone po Package)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetPart(Uri)

Zwraca część z danym identyfikatorem URI.

(Odziedziczone po Package)
GetPartCore(Uri)

Po przesłonięciu w klasie pochodnej zwraca część adresowaną przez dany identyfikator URI.

(Odziedziczone po Package)
GetParts()

Zwraca kolekcję wszystkich części pakietu.

(Odziedziczone po Package)
GetPartsCore()

Po zastąpieniu klasy pochodnej zwraca tablicę wszystkich części pakietu.

(Odziedziczone po Package)
GetRelationship(String)

Zwraca relację na poziomie pakietu z danym identyfikatorem.

(Odziedziczone po Package)
GetRelationships()

Zwraca kolekcję wszystkich relacji na poziomie pakietu.

(Odziedziczone po Package)
GetRelationshipsByType(String)

Zwraca kolekcję wszystkich relacji na poziomie pakietu, które pasują do danego RelationshipTypeelementu .

(Odziedziczone po Package)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
PartExists(Uri)

Wskazuje, czy część z danym identyfikatorem URI znajduje się w pakiecie.

(Odziedziczone po Package)
RelationshipExists(String)

Wskazuje, czy relacja na poziomie pakietu z danym identyfikatorem jest zawarta w pakiecie.

(Odziedziczone po Package)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

IDisposable.Dispose()

Ten element członkowski obsługuje infrastrukturę Windows Presentation Foundation (WPF) i nie jest przeznaczony do użycia aplikacji. Zamiast tego należy użyć metody bezpiecznej Dispose(Boolean) typu.

(Odziedziczone po Package)

Dotyczy

Zobacz też