Show / Hide Table of Contents

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
System.Object
System.MarshalByRefObject
System.IO.Stream
DeflaterOutputStream
ZipOutputStream
Implements
System.IDisposable
Inherited Members
DeflaterOutputStream.IsStreamOwner
DeflaterOutputStream.CanPatchEntries
DeflaterOutputStream.cryptoTransform_
DeflaterOutputStream.AESAuthCode
DeflaterOutputStream.ZipCryptoEncoding
DeflaterOutputStream.EncryptBlock(Byte[], Int32, Int32)
DeflaterOutputStream.Deflate()
DeflaterOutputStream.CanRead
DeflaterOutputStream.CanSeek
DeflaterOutputStream.CanWrite
DeflaterOutputStream.Length
DeflaterOutputStream.Position
DeflaterOutputStream.Seek(Int64, SeekOrigin)
DeflaterOutputStream.SetLength(Int64)
DeflaterOutputStream.ReadByte()
DeflaterOutputStream.Read(Byte[], Int32, Int32)
DeflaterOutputStream.FlushAsync(CancellationToken)
DeflaterOutputStream.Dispose(Boolean)
DeflaterOutputStream.GetAuthCodeIfAES()
DeflaterOutputStream.WriteByte(Byte)
DeflaterOutputStream.deflater_
DeflaterOutputStream.baseOutputStream_
DeflaterOutputStream._stringCodec
System.IO.Stream.Null
System.IO.Stream.BeginRead(System.Byte[], System.Int32, System.Int32, System.AsyncCallback, System.Object)
System.IO.Stream.BeginWrite(System.Byte[], System.Int32, System.Int32, System.AsyncCallback, System.Object)
System.IO.Stream.Close()
System.IO.Stream.CopyTo(System.IO.Stream)
System.IO.Stream.CopyTo(System.IO.Stream, System.Int32)
System.IO.Stream.CopyToAsync(System.IO.Stream)
System.IO.Stream.CopyToAsync(System.IO.Stream, System.Int32)
System.IO.Stream.CopyToAsync(System.IO.Stream, System.Int32, System.Threading.CancellationToken)
System.IO.Stream.CreateWaitHandle()
System.IO.Stream.Dispose()
System.IO.Stream.EndRead(System.IAsyncResult)
System.IO.Stream.EndWrite(System.IAsyncResult)
System.IO.Stream.FlushAsync()
System.IO.Stream.ObjectInvariant()
System.IO.Stream.ReadAsync(System.Byte[], System.Int32, System.Int32)
System.IO.Stream.ReadAsync(System.Byte[], System.Int32, System.Int32, System.Threading.CancellationToken)
System.IO.Stream.Synchronized(System.IO.Stream)
System.IO.Stream.WriteAsync(System.Byte[], System.Int32, System.Int32)
System.IO.Stream.CanTimeout
System.IO.Stream.ReadTimeout
System.IO.Stream.WriteTimeout
System.MarshalByRefObject.GetLifetimeService()
System.MarshalByRefObject.InitializeLifetimeService()
System.MarshalByRefObject.MemberwiseClone(System.Boolean)
System.Object.Equals(System.Object)
System.Object.Equals(System.Object, System.Object)
System.Object.GetHashCode()
System.Object.GetType()
System.Object.MemberwiseClone()
System.Object.ReferenceEquals(System.Object, System.Object)
System.Object.ToString()
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 Source

ZipOutputStream(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.

| Improve this Doc View Source

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
| Improve this Doc View Source

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 Source

IsFinished

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.

| Improve this Doc View Source

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
| Improve this Doc View Source

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

| Improve this Doc View Source

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 Source

CloseEntry()

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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
DeflaterOutputStream.Finish()
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
Entry name exceeds the maximum length

| Improve this Doc View Source

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
DeflaterOutputStream.FinishAsync(CancellationToken)
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
Entry name exceeds the maximum length

| Improve this Doc View Source

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
DeflaterOutputStream.Flush()
| Improve this Doc View Source

GetLevel()

Get the current deflater compression level

Declaration
public int GetLevel()
Returns
Type Description
System.Int32

The current compression level

| Improve this Doc View Source

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[]
| Improve this Doc View Source

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
Entry name is too long
Finish has already been called

System.NotImplementedException

The Compression method specified for the entry is unsupported.

| Improve this Doc View Source

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
Entry name is too long
Finish has already been called

System.NotImplementedException

The Compression method specified for the entry is unsupported.

| Improve this Doc View Source

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
Size is not set
CompressedSize is not set
CompressionMethod is not Deflate
Too many entries in the Zip file
Entry name is too long
Finish has already been called

System.NotImplementedException

The Compression method specified for the entry is unsupported
Entry is encrypted

| Improve this Doc View Source

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.

| Improve this Doc View Source

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.

| Improve this Doc View Source

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
DeflaterOutputStream.Write(Byte[], Int32, Int32)
Exceptions
Type Condition
ZipException

Archive size is invalid

System.InvalidOperationException

No entry is active.

| Improve this Doc View Source

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
Overrides
DeflaterOutputStream.WriteAsync(Byte[], Int32, Int32, CancellationToken)

Implements

System.IDisposable
  • Improve this Doc
  • View Source
In This Article
Back to top Copyright © 2000-2022 SharpZipLib Contributors