Class ZipOutputStream
This is a DeflaterOutputStream that writes the files into a zip archive one after another. It has a special method to start a new zip entry. The zip entries contains information about the file name size, compressed size, CRC, etc.
It includes support for Stored and Deflated entries.
This class is not thread safe.
Author of the original java version : Jochen Hoenicke
Inheritance
Implements
Inherited Members
Namespace: ICSharpCode.SharpZipLib.Zip
Assembly: ICSharpCode.SharpZipLib.dll
Syntax
public class ZipOutputStream : DeflaterOutputStream, IDisposable
Examples
This sample shows how to create a zip file
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
class MainClass
{
public static void Main(string[] args)
{
string[] filenames = Directory.GetFiles(args[0]);
byte[] buffer = new byte[4096];
using ( ZipOutputStream s = new ZipOutputStream(File.Create(args[1])) ) {
s.SetLevel(9); // 0 - store only to 9 - means best compression
foreach (string file in filenames) {
ZipEntry entry = new ZipEntry(file);
s.PutNextEntry(entry);
using (FileStream fs = File.OpenRead(file)) {
StreamUtils.Copy(fs, s, buffer);
}
}
}
}
}
Constructors
| Improve this Doc View SourceZipOutputStream(Stream)
Creates a new Zip output stream, writing a zip archive.
Declaration
public ZipOutputStream(Stream baseOutputStream)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.Stream | baseOutputStream | The output stream to which the archive contents are written. |
ZipOutputStream(Stream, StringCodec)
Creates a new Zip output stream, writing a zip archive.
Declaration
public ZipOutputStream(Stream baseOutputStream, StringCodec stringCodec)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.Stream | baseOutputStream | The output stream to which the archive contents are written. |
| StringCodec | stringCodec |
ZipOutputStream(Stream, Int32)
Creates a new Zip output stream, writing a zip archive.
Declaration
public ZipOutputStream(Stream baseOutputStream, int bufferSize)
Parameters
| Type | Name | Description |
|---|---|---|
| System.IO.Stream | baseOutputStream | The output stream to which the archive contents are written. |
| System.Int32 | bufferSize | Size of the buffer to use. |
Properties
| Improve this Doc View SourceIsFinished
Gets a flag value of true if the central header has been added for this archive; false if it has not been added.
Declaration
public bool IsFinished { get; }
Property Value
| Type | Description |
|---|---|
| System.Boolean |
Remarks
No further entries can be added once this has been done.
NameTransform
Used for transforming the names of entries added by PutNextEntry(ZipEntry). Defaults to PathTransformer, set to null to disable transforms and use names as supplied.
Declaration
public INameTransform NameTransform { get; set; }
Property Value
| Type | Description |
|---|---|
| INameTransform |
Password
Get/set the password used for encryption.
Declaration
public string Password { get; set; }
Property Value
| Type | Description |
|---|---|
| System.String |
Remarks
When set to null or if the password is empty no encryption is performed
UseZip64
Get / set a value indicating how Zip64 Extension usage is determined when adding entries.
Declaration
public UseZip64 UseZip64 { get; set; }
Property Value
| Type | Description |
|---|---|
| UseZip64 |
Remarks
Older archivers may not understand Zip64 extensions. If backwards compatability is an issue be careful when adding entries to an archive. Setting this property to off is workable but less desirable as in those circumstances adding a file larger then 4GB will fail.
Methods
| Improve this Doc View SourceCloseEntry()
Closes the current entry, updating header and footer information as required
Declaration
public void CloseEntry()
Exceptions
| Type | Condition |
|---|---|
| ZipException | Invalid entry field values. |
| System.IO.IOException | An I/O error occurs. |
| System.InvalidOperationException | No entry is active. |
CloseEntryAsync(CancellationToken)
Closes the current entry, updating header and footer information as required
Declaration
public async Task CloseEntryAsync(CancellationToken ct)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Threading.CancellationToken | ct |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task |
Exceptions
| Type | Condition |
|---|---|
| ZipException | Invalid entry field values. |
| System.IO.IOException | An I/O error occurs. |
| System.InvalidOperationException | No entry is active. |
Finish()
Finishes the stream. This will write the central directory at the end of the zip file and flush the stream.
Declaration
public override void Finish()
Overrides
Remarks
This is automatically called when the stream is closed.
Exceptions
| Type | Condition |
|---|---|
| System.IO.IOException | An I/O error occurs. |
| ZipException | Comment exceeds the maximum length |
FinishAsync(CancellationToken)
Finishes the stream. This will write the central directory at the end of the zip file and flush the stream.
Declaration
public override async Task FinishAsync(CancellationToken ct)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Threading.CancellationToken | ct |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task |
Overrides
Remarks
This is automatically called when the stream is closed.
Exceptions
| Type | Condition |
|---|---|
| System.IO.IOException | An I/O error occurs. |
| ZipException | Comment exceeds the maximum length |
Flush()
Flushes the stream by calling Flush on the deflater stream unless the current compression method is Stored. Then it flushes the underlying output stream.
Declaration
public override void Flush()
Overrides
| Improve this Doc View SourceGetLevel()
Get the current deflater compression level
Declaration
public int GetLevel()
Returns
| Type | Description |
|---|---|
| System.Int32 | The current compression level |
InitializeAESPassword(ZipEntry, String)
Initializes encryption keys based on given password.
Declaration
protected byte[] InitializeAESPassword(ZipEntry entry, string rawPassword)
Parameters
| Type | Name | Description |
|---|---|---|
| ZipEntry | entry | |
| System.String | rawPassword |
Returns
| Type | Description |
|---|---|
| System.Byte[] |
PutNextEntry(ZipEntry)
Starts a new Zip entry. It automatically closes the previous entry if present. All entry elements bar name are optional, but must be correct if present. If the compression method is stored and the output is not patchable the compression for that entry is automatically changed to deflate level 0
Declaration
public void PutNextEntry(ZipEntry entry)
Parameters
| Type | Name | Description |
|---|---|---|
| ZipEntry | entry | the entry. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | if entry passed is null. |
| System.IO.IOException | if an I/O error occurred. |
| System.InvalidOperationException | if stream was finished |
| ZipException | Too many entries in the Zip file |
| System.NotImplementedException | The Compression method specified for the entry is unsupported. |
PutNextEntryAsync(ZipEntry, CancellationToken)
Starts a new Zip entry. It automatically closes the previous entry if present. All entry elements bar name are optional, but must be correct if present. If the compression method is stored and the output is not patchable the compression for that entry is automatically changed to deflate level 0
Declaration
public async Task PutNextEntryAsync(ZipEntry entry, CancellationToken ct = default(CancellationToken))
Parameters
| Type | Name | Description |
|---|---|---|
| ZipEntry | entry | the entry. |
| System.Threading.CancellationToken | ct | The System.Threading.CancellationToken that can be used to cancel the operation. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | if entry passed is null. |
| System.IO.IOException | if an I/O error occured. |
| System.InvalidOperationException | if stream was finished |
| ZipException | Too many entries in the Zip file |
| System.NotImplementedException | The Compression method specified for the entry is unsupported. |
PutNextPassthroughEntry(ZipEntry)
Starts a new passthrough Zip entry. It automatically closes the previous entry if present. Passthrough entry is an entry that is created from compressed data. It is useful to avoid recompression to save CPU resources if compressed data is already disposable. All entry elements bar name, crc, size and compressed size are optional, but must be correct if present. Compression should be set to Deflated.
Declaration
public void PutNextPassthroughEntry(ZipEntry entry)
Parameters
| Type | Name | Description |
|---|---|---|
| ZipEntry | entry | the entry. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentNullException | if entry passed is null. |
| System.IO.IOException | if an I/O error occurred. |
| System.InvalidOperationException | if stream was finished. |
| ZipException | Crc is not set |
| System.NotImplementedException | The Compression method specified for the entry is unsupported |
SetComment(String)
Set the zip file comment.
Declaration
public void SetComment(string comment)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | comment | The comment text for the entire archive. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException | The converted comment is longer than 0xffff bytes. |
SetLevel(Int32)
Sets the compression level. The new level will be activated immediately.
Declaration
public void SetLevel(int level)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | level | The new compression level (1 to 9). |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException | Level specified is not supported. |
Write(Byte[], Int32, Int32)
Writes the given buffer to the current entry.
Declaration
public override void Write(byte[] buffer, int offset, int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | buffer | The buffer containing data to write. |
| System.Int32 | offset | The offset of the first byte to write. |
| System.Int32 | count | The number of bytes to write. |
Overrides
Exceptions
| Type | Condition |
|---|---|
| ZipException | Archive size is invalid |
| System.InvalidOperationException | No entry is active. |
WriteAsync(Byte[], Int32, Int32, CancellationToken)
Asynchronously writes a sequence of bytes to the current stream, advances the current position within this stream by the number of bytes written, and monitors cancellation requests.
Declaration
public override async Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken ct)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Byte[] | buffer | The byte array |
| System.Int32 | offset | The offset into the byte array where to start. |
| System.Int32 | count | The number of bytes to write. |
| System.Threading.CancellationToken | ct | The token to monitor for cancellation requests. The default value is System.Threading.CancellationToken.None. |
Returns
| Type | Description |
|---|---|
| System.Threading.Tasks.Task |