ZipPackage Classe

Definizione

Implementa una sottoclasse derivata della classe di base Package astratta. La classe ZipPackage usa un archivio ZIP come archivio del contenitore. La classe non può essere ereditata.

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
Ereditarietà
ZipPackage

Esempio

In questo esempio viene illustrato come creare un oggetto di base ZipPackage.

Nell'esempio viene creato un pacchetto contenente una singola parte del documento definita come elemento radice del pacchetto da un oggetto a livello PackageRelationshipdi pacchetto.

Il pacchetto contiene anche una parte dell'immagine e un secondo PackageRelationship che definisce un'associazione tra la parte del documento di origine e la parte dell'immagine di destinazione. L'immagine è una risorsa usata con il documento.

//  -------------------------- 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

Per l'esempio completo, vedere Scrittura di un esempio di pacchetto.

Commenti

Pacchetto.Open per impostazione predefinita, il metodo usa ZipPackage i contenitori.

Proprietà

FileOpenAccess

Ottiene l'impostazione di accesso ai file per il pacchetto.

(Ereditato da Package)
PackageProperties

Ottiene le proprietà principali del pacchetto.

(Ereditato da Package)

Metodi

Close()

Salva e chiude il pacchetto più tutti i flussi della parte sottostanti.

(Ereditato da Package)
CreatePart(Uri, String)

Crea una parte non compressa nuova con un URI specificato e un tipo di contenuto.

(Ereditato da Package)
CreatePart(Uri, String, CompressionOption)

Crea una parte nuova con un URI specificato, un tipo di contenuto e un'opzione di compressione.

(Ereditato da Package)
CreatePartCore(Uri, String, CompressionOption)

Quando è sottoposto a override in una classe derivata, crea una parte nuova nel pacchetto.

(Ereditato da Package)
CreateRelationship(Uri, TargetMode, String)

Crea una relazione a livello di pacchetto con una parte con un URI specificato, un modalità di destinazione e un tipo di relazione.

(Ereditato da Package)
CreateRelationship(Uri, TargetMode, String, String)

Crea una relazione a livello di pacchetto a una parte con un determinato URI, modalità di destinazione, tipo di relazione e identificatore (ID).

(Ereditato da Package)
DeletePart(Uri)

Elimina una parte con un URI specificato dal pacchetto.

(Ereditato da Package)
DeletePartCore(Uri)

Quando è sottoposto a override in una classe derivata, elimina una parte con un URI specificato.

(Ereditato da Package)
DeleteRelationship(String)

Elimina una relazione a livello di pacchetto.

(Ereditato da Package)
Dispose(Boolean)

Svuota e salva il contenuto di tutte le parti e le relazioni, chiude il pacchetto e rilascia tutte le risorse.

(Ereditato da Package)
Equals(Object)

Determina se l'oggetto specificato è uguale all'oggetto corrente.

(Ereditato da Object)
Flush()

Salva il contenuto di tutte le parti e le relazioni contenute nel pacchetto.

(Ereditato da Package)
FlushCore()

Quando è sottoposto a override in una classe derivata, salva il contenuto di tutte le parti e le relazioni nell'archivio della classe derivata.

(Ereditato da Package)
GetHashCode()

Funge da funzione hash predefinita.

(Ereditato da Object)
GetPart(Uri)

Restituisce la parte con un URI specificato.

(Ereditato da Package)
GetPartCore(Uri)

Quando è sottoposto a override in una classe derivata, restituisce la parte alla quale fa riferimento un URI specificato.

(Ereditato da Package)
GetParts()

Restituisce un insieme di tutte le parti nel pacchetto.

(Ereditato da Package)
GetPartsCore()

Quando è sottoposto a override in una classe derivata, restituisce una matrice di tutte le parti nel pacchetto.

(Ereditato da Package)
GetRelationship(String)

Restituisce la relazione a livello di pacchetto con un identificatore specificato.

(Ereditato da Package)
GetRelationships()

Restituisce un insieme di tutte le relazioni a livello di pacchetto.

(Ereditato da Package)
GetRelationshipsByType(String)

Restituisce un insieme di tutte le relazioni a livello di pacchetto che corrispondono a RelationshipType specificato.

(Ereditato da Package)
GetType()

Ottiene l'oggetto Type dell'istanza corrente.

(Ereditato da Object)
MemberwiseClone()

Crea una copia superficiale dell'oggetto Object corrente.

(Ereditato da Object)
PartExists(Uri)

Indica se una parte con un URI specificato è nel pacchetto.

(Ereditato da Package)
RelationshipExists(String)

Indica se una relazione a livello di pacchetto con un ID specificato è contenuta nel pacchetto.

(Ereditato da Package)
ToString()

Restituisce una stringa che rappresenta l'oggetto corrente.

(Ereditato da Object)

Implementazioni dell'interfaccia esplicita

IDisposable.Dispose()

Questo membro supporta l'infrastruttura Windows Presentation Foundation (WPF) e non è destinato all'uso dell'applicazione. Usare invece il metodo Dispose(Boolean) indipendente dai tipi.

(Ereditato da Package)

Si applica a

Vedi anche