ZipArchiveWriter
OfficeIMO.Archives
Writes ZIP archives (PKWARE APPNOTE 6.3.10) with stored or Deflate members, UTF-8 names and ZIP64 when sizes or offsets require it. With the default options the output depends only on the entries written: timestamps are fixed, attributes are constant, no varying extra fields are written and Deflate output is the same on every platform.
Inheritance
- object
- ZipArchiveWriter
Remarks
Each member is compressed into a staging buffer (memory, then a private temporary file above 8 MiB) so its local header carries exact sizes and checksums. The destination needs to be writable but not seekable. Member names pass the same path checks as extraction.
Constructors
public ZipArchiveWriter(Stream destination, ZipArchiveWriterOptions? options = null, bool leaveOpen = false) #Starts an archive at the destination's current position.
Parameters
- destination System.IO.Stream
- options OfficeIMO.Archives.ZipArchiveWriterOptions? = null
- leaveOpen bool = false
Methods
public void Add(ArchiveWriteEntry entry, CancellationToken cancellationToken = default) #Adds one described entry.
Parameters
- entry OfficeIMO.Archives.ArchiveWriteEntry
- cancellationToken System.Threading.CancellationToken = default
public void AddDirectory(string path, DateTimeOffset? lastWriteTime = null) #Adds an explicit directory member, written with a trailing '/'.
Parameters
- path string
- lastWriteTime System.DateTimeOffset? = null
public void AddFile(string path, Stream content, DateTimeOffset? lastWriteTime = null, ZipCompressionMethod? compression = null, CancellationToken cancellationToken = default) #Adds one file member from the stream's current position to its end. The stream is left open.
Parameters
- path string
- content System.IO.Stream
- lastWriteTime System.DateTimeOffset? = null
- compression OfficeIMO.Archives.ZipCompressionMethod? = null
- cancellationToken System.Threading.CancellationToken = default
public void AddFile(string path, byte[] content, DateTimeOffset? lastWriteTime = null, ZipCompressionMethod? compression = null) #Adds one file member from memory.
Parameters
- path string
- content byte[]
- lastWriteTime System.DateTimeOffset? = null
- compression OfficeIMO.Archives.ZipCompressionMethod? = null
public static void Create(Stream destination, IEnumerable<ArchiveWriteEntry> entries, ZipArchiveWriterOptions? options = null, CancellationToken cancellationToken = default) #Writes the entries, in the given order, to a new ZIP stream.
Parameters
- destination System.IO.Stream
- entries System.Collections.Generic.IEnumerable<OfficeIMO.Archives.ArchiveWriteEntry>
- options OfficeIMO.Archives.ZipArchiveWriterOptions? = null
- cancellationToken System.Threading.CancellationToken = default
public static void Create(string destinationPath, IEnumerable<ArchiveWriteEntry> entries, ZipArchiveWriterOptions? options = null, CancellationToken cancellationToken = default) #Writes the entries, in the given order, to a new ZIP file. An existing file is not overwritten.
Parameters
- destinationPath string
- entries System.Collections.Generic.IEnumerable<OfficeIMO.Archives.ArchiveWriteEntry>
- options OfficeIMO.Archives.ZipArchiveWriterOptions? = null
- cancellationToken System.Threading.CancellationToken = default
public Stream CreateEntry(string path, DateTimeOffset? lastWriteTime = null, ZipCompressionMethod? compression = null) #Opens a write stream for one file member. Dispose it to complete the member before adding another.
Parameters
- path string
- lastWriteTime System.DateTimeOffset? = null
- compression OfficeIMO.Archives.ZipCompressionMethod? = null
Returns
Streampublic static void CreateFromDirectory(string sourceDirectory, Stream destination, ZipArchiveWriterOptions? options = null, CancellationToken cancellationToken = default) #Archives a folder's files and subfolders in ordinal path order. Symbolic links and reparse points are rejected.
Parameters
- sourceDirectory string
- destination System.IO.Stream
- options OfficeIMO.Archives.ZipArchiveWriterOptions? = null
- cancellationToken System.Threading.CancellationToken = default
public static void CreateFromDirectory(string sourceDirectory, string destinationPath, ZipArchiveWriterOptions? options = null, CancellationToken cancellationToken = default) #Archives a folder into a new ZIP file. An existing file is not overwritten.
Parameters
- sourceDirectory string
- destinationPath string
- options OfficeIMO.Archives.ZipArchiveWriterOptions? = null
- cancellationToken System.Threading.CancellationToken = default
public void Dispose() #Completes any open member, finishes the archive and releases the destination unless it was left open.
public void Finish() #Writes the central directory and end records. Further additions are rejected. Dispose also finishes.