Class ZipFile
This class represents a Zip archive. You can ask for the contained entries, or get an input stream for a file entry. The entry is automatically decompressed.
You can also update the archive adding or deleting entries.
This class is thread safe for input: You can open input streams for arbitrary
entries in different threads.
Author of the original java version : Jochen Hoenicke
Inheritance
Implements
Inherited Members
Namespace: ICSharpCode.SharpZipLib.Zip
Assembly: ICSharpCode.SharpZipLib.dll
Syntax
public class ZipFile : IEnumerable, IDisposable
Examples
using System;
using System.Text;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
class MainClass
{
static public void Main(string[] args)
{
using (ZipFile zFile = new ZipFile(args[0])) {
Console.WriteLine("Listing of : " + zFile.Name);
Console.WriteLine("");
Console.WriteLine("Raw Size Size Date Time Name");
Console.WriteLine("-------- -------- -------- ------ ---------");
foreach (ZipEntry e in zFile) {
if ( e.IsFile ) {
DateTime d = e.DateTime;
Console.WriteLine("{0, -10}{1, -10}{2} {3} {4}", e.Size, e.CompressedSize,
d.ToString("dd-MM-yy"), d.ToString("HH:mm"),
e.Name);
}
}
}
}
}
Constructors
| Improve this Doc View SourceZipFile(FileStream)
Opens a Zip file reading the given System.IO.FileStream.
Declaration
public ZipFile(FileStream file)
Parameters
Type | Name | Description |
---|---|---|
System.IO.FileStream | file | The System.IO.FileStream to read archive data from. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The supplied argument is null. |
System.IO.IOException | An i/o error occurs. |
ZipException | The file doesn't contain a valid zip archive. |
ZipFile(FileStream, Boolean)
Opens a Zip file reading the given System.IO.FileStream.
Declaration
public ZipFile(FileStream file, bool leaveOpen)
Parameters
Type | Name | Description |
---|---|---|
System.IO.FileStream | file | The System.IO.FileStream to read archive data from. |
System.Boolean | leaveOpen | true to leave the System.IO.FileStream open when the ZipFile is disposed, false to dispose of it |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The supplied argument is null. |
System.IO.IOException | An i/o error occurs. |
ZipException | The file doesn't contain a valid zip archive. |
ZipFile(Stream)
Opens a Zip file reading the given System.IO.Stream.
Declaration
public ZipFile(Stream stream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream | The System.IO.Stream to read archive data from. |
Exceptions
Type | Condition |
---|---|
System.IO.IOException | An i/o error occurs |
ZipException | The stream doesn't contain a valid zip archive. |
System.ArgumentException | The System.IO.Stream doesnt support seeking. |
System.ArgumentNullException | The System.IO.Stream argument is null. |
ZipFile(Stream, Boolean)
Opens a Zip file reading the given System.IO.Stream.
Declaration
public ZipFile(Stream stream, bool leaveOpen)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | stream | The System.IO.Stream to read archive data from. |
System.Boolean | leaveOpen | true to leave the System.IO.Stream open when the ZipFile is disposed, false to dispose of it |
Exceptions
Type | Condition |
---|---|
System.IO.IOException | An i/o error occurs |
ZipException | The stream doesn't contain a valid zip archive. |
System.ArgumentException | The System.IO.Stream doesnt support seeking. |
System.ArgumentNullException | The System.IO.Stream argument is null. |
ZipFile(String, StringCodec)
Opens a Zip file with the given name for reading.
Declaration
public ZipFile(string name, StringCodec stringCodec = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name of the file to open. |
StringCodec | stringCodec |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | The argument supplied is null. |
System.IO.IOException | An i/o error occurs |
ZipException | The file doesn't contain a valid zip archive. |
Fields
| Improve this Doc View SourceKeysRequired
Event handler for handling encryption keys.
Declaration
public ZipFile.KeysRequiredEventHandler KeysRequired
Field Value
Type | Description |
---|---|
ZipFile.KeysRequiredEventHandler |
Properties
| Improve this Doc View SourceBufferSize
Get /set the buffer size to be used when updating this zip file.
Declaration
public int BufferSize { get; set; }
Property Value
Type | Description |
---|---|
System.Int32 |
Count
Get the number of entries contained in this ZipFile.
Declaration
public long Count { get; }
Property Value
Type | Description |
---|---|
System.Int64 |
EntryByIndex[Int32]
Indexer property for ZipEntries
Declaration
public ZipEntry this[int index] { get; }
Parameters
Type | Name | Description |
---|---|---|
System.Int32 | index |
Property Value
Type | Description |
---|---|
ZipEntry |
EntryFactory
Get/set the IEntryFactory used to generate ZipEntry values during updates.
Declaration
public IEntryFactory EntryFactory { get; set; }
Property Value
Type | Description |
---|---|
IEntryFactory |
IsEmbeddedArchive
Get a value indicating whether this archive is embedded in another file or not.
Declaration
public bool IsEmbeddedArchive { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsNewArchive
Get a value indicating that this archive is a new one.
Declaration
public bool IsNewArchive { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
IsStreamOwner
Get/set a flag indicating if the underlying stream is owned by the ZipFile instance. If the flag is true then the stream will be closed when Close is called.
Declaration
public bool IsStreamOwner { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
Remarks
The default value is true in all cases.
IsUpdating
Get a value indicating an update has been started.
Declaration
public bool IsUpdating { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Name
Gets the name of this zip file.
Declaration
public string Name { get; }
Property Value
Type | Description |
---|---|
System.String |
NameTransform
Get / set the INameTransform to apply to names when updating.
Declaration
public INameTransform NameTransform { get; set; }
Property Value
Type | Description |
---|---|
INameTransform |
Password
Password to be used for encrypting/decrypting files.
Declaration
public string Password { set; }
Property Value
Type | Description |
---|---|
System.String |
Remarks
Set to null if no password is required.
Size
Gets the number of entries in this zip file.
Declaration
[Obsolete("Use the Count property instead")]
public int Size { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Exceptions
Type | Condition |
---|---|
System.InvalidOperationException | The Zip file has been closed. |
SkipLocalEntryTestsOnLocate
Skip the verification of the local header when reading an archive entry. Set this to attempt to read the entries even if the headers should indicate that doing so would fail or produce an unexpected output.
Declaration
public bool SkipLocalEntryTestsOnLocate { get; set; }
Property Value
Type | Description |
---|---|
System.Boolean |
StringCodec
Utility class for resolving the encoding used for reading and writing strings
Declaration
public StringCodec StringCodec { get; set; }
Property Value
Type | Description |
---|---|
StringCodec |
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 |
ZipCryptoEncoding
The encoding used for the ZipCrypto passwords. Defaults to DefaultZipCryptoEncoding.
Declaration
public Encoding ZipCryptoEncoding { get; set; }
Property Value
Type | Description |
---|---|
System.Text.Encoding |
ZipFileComment
Gets the comment for the zip file.
Declaration
public string ZipFileComment { get; }
Property Value
Type | Description |
---|---|
System.String |
Methods
| Improve this Doc View SourceAbortUpdate()
Abort updating leaving the archive unchanged.
Declaration
public void AbortUpdate()
See Also
| Improve this Doc View SourceAdd(IStaticDataSource, ZipEntry)
Add a ZipEntry with data.
Declaration
public void Add(IStaticDataSource dataSource, ZipEntry entry)
Parameters
Type | Name | Description |
---|---|---|
IStaticDataSource | dataSource | The source of the data for this entry. |
ZipEntry | entry | The entry to add. |
Remarks
This can be used to add file entries with a custom data source.
Exceptions
Type | Condition |
---|---|
System.NotSupportedException | The encryption method specified in |
System.NotImplementedException | Compression method is not supported for creating entries. |
Add(IStaticDataSource, String)
Add a file entry with data.
Declaration
public void Add(IStaticDataSource dataSource, string entryName)
Parameters
Type | Name | Description |
---|---|---|
IStaticDataSource | dataSource | The source of the data for this entry. |
System.String | entryName | The name to give to the entry. |
Add(IStaticDataSource, String, CompressionMethod)
Add a file entry with data.
Declaration
public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod)
Parameters
Type | Name | Description |
---|---|---|
IStaticDataSource | dataSource | The source of the data for this entry. |
System.String | entryName | The name to give to the entry. |
CompressionMethod | compressionMethod | The compression method to use. |
Exceptions
Type | Condition |
---|---|
System.NotImplementedException | Compression method is not supported for creating entries. |
Add(IStaticDataSource, String, CompressionMethod, Boolean)
Add a file entry with data.
Declaration
public void Add(IStaticDataSource dataSource, string entryName, CompressionMethod compressionMethod, bool useUnicodeText)
Parameters
Type | Name | Description |
---|---|---|
IStaticDataSource | dataSource | The source of the data for this entry. |
System.String | entryName | The name to give to the entry. |
CompressionMethod | compressionMethod | The compression method to use. |
System.Boolean | useUnicodeText | Ensure Unicode text is used for name and comments for this entry. |
Exceptions
Type | Condition |
---|---|
System.NotImplementedException | Compression method is not supported for creating entries. |
Add(ZipEntry)
Add a ZipEntry that contains no data.
Declaration
public void Add(ZipEntry entry)
Parameters
Type | Name | Description |
---|---|---|
ZipEntry | entry | The entry to add. |
Remarks
This can be used to add directories, volume labels, or empty file entries.
Add(String)
Add a file to the archive.
Declaration
public void Add(string fileName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fileName | The name of the file to add. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Argument supplied is null. |
Add(String, CompressionMethod)
Add a new entry to the archive.
Declaration
public void Add(string fileName, CompressionMethod compressionMethod)
Parameters
Type | Name | Description |
---|---|---|
System.String | fileName | The name of the file to add. |
CompressionMethod | compressionMethod | The compression method to use. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | ZipFile has been closed. |
System.NotImplementedException | Compression method is not supported for creating entries. |
Add(String, CompressionMethod, Boolean)
Add a new entry to the archive.
Declaration
public void Add(string fileName, CompressionMethod compressionMethod, bool useUnicodeText)
Parameters
Type | Name | Description |
---|---|---|
System.String | fileName | The name of the file to add. |
CompressionMethod | compressionMethod | The compression method to use. |
System.Boolean | useUnicodeText | Ensure Unicode text is used for name and comment for this entry. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Argument supplied is null. |
System.ObjectDisposedException | ZipFile has been closed. |
System.NotImplementedException | Compression method is not supported for creating entries. |
Add(String, String)
Add a file to the archive.
Declaration
public void Add(string fileName, string entryName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fileName | The name of the file to add. |
System.String | entryName | The name to use for the ZipEntry on the Zip file created. |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException | Argument supplied is null. |
AddDirectory(String)
Add a directory entry to the archive.
Declaration
public void AddDirectory(string directoryName)
Parameters
Type | Name | Description |
---|---|---|
System.String | directoryName | The directory to add. |
BeginUpdate()
Begin updating this ZipFile archive.
Declaration
public void BeginUpdate()
See Also
| Improve this Doc View SourceBeginUpdate(IArchiveStorage)
Begin updating to this ZipFile archive.
Declaration
public void BeginUpdate(IArchiveStorage archiveStorage)
Parameters
Type | Name | Description |
---|---|---|
IArchiveStorage | archiveStorage | The storage to use during the update. |
BeginUpdate(IArchiveStorage, IDynamicDataSource)
Begin updating this ZipFile archive.
Declaration
public void BeginUpdate(IArchiveStorage archiveStorage, IDynamicDataSource dataSource)
Parameters
Type | Name | Description |
---|---|---|
IArchiveStorage | archiveStorage | The archive storage for use during the update. |
IDynamicDataSource | dataSource | The data source to utilise during updating. |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | ZipFile has been closed. |
System.ArgumentNullException | One of the arguments provided is null |
System.ObjectDisposedException | ZipFile has been closed. |
Close()
Closes the ZipFile. If the stream is owned then this also closes the underlying input stream. Once closed, no further instance methods should be called.
Declaration
public void Close()
Exceptions
Type | Condition |
---|---|
System.IO.IOException | An i/o error occurs. |
CommitUpdate()
Commit current updates, updating this archive.
Declaration
public void CommitUpdate()
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | ZipFile has been closed. |
See Also
| Improve this Doc View SourceCreate(Stream)
Create a new ZipFile whose data will be stored on a stream.
Declaration
public static ZipFile Create(Stream outStream)
Parameters
Type | Name | Description |
---|---|---|
System.IO.Stream | outStream | The stream providing data storage. |
Returns
Type | Description |
---|---|
ZipFile | Returns the newly created ZipFile |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
System.ArgumentException |
|
Create(String)
Create a new ZipFile whose data will be stored in a file.
Declaration
public static ZipFile Create(string fileName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fileName | The name of the archive to create. |
Returns
Type | Description |
---|---|
ZipFile | Returns the newly created ZipFile |
Exceptions
Type | Condition |
---|---|
System.ArgumentNullException |
|
Delete(ZipEntry)
Delete a ZipEntry from the archive.
Declaration
public void Delete(ZipEntry entry)
Parameters
Type | Name | Description |
---|---|---|
ZipEntry | entry | The entry to delete. |
Delete(String)
Delete an entry by name
Declaration
public bool Delete(string fileName)
Parameters
Type | Name | Description |
---|---|---|
System.String | fileName | The filename to delete |
Returns
Type | Description |
---|---|
System.Boolean | True if the entry was found and deleted; false otherwise. |
Dispose(Boolean)
Releases the unmanaged resources used by the this instance and optionally releases the managed resources.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | disposing | true to release both managed and unmanaged resources; false to release only unmanaged resources. |
Finalize()
Finalize this instance.
Declaration
protected void Finalize()
FindEntry(String, Boolean)
Return the index of the entry with a matching name
Declaration
public int FindEntry(string name, bool ignoreCase)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | Entry name to find |
System.Boolean | ignoreCase | If true the comparison is case insensitive |
Returns
Type | Description |
---|---|
System.Int32 | The index position of the matching entry or -1 if not found |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | The Zip file has been closed. |
GetEntry(String)
Searches for a zip entry in this archive with the given name. String comparisons are case insensitive
Declaration
public ZipEntry GetEntry(string name)
Parameters
Type | Name | Description |
---|---|---|
System.String | name | The name to find. May contain directory components separated by slashes ('/'). |
Returns
Type | Description |
---|---|
ZipEntry | A clone of the zip entry, or null if no entry with that name exists. |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | The Zip file has been closed. |
GetEnumerator()
Gets an enumerator for the Zip entries in this Zip file.
Declaration
public IEnumerator GetEnumerator()
Returns
Type | Description |
---|---|
System.Collections.IEnumerator | Returns an System.Collections.IEnumerator for this archive. |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | The Zip file has been closed. |
GetInputStream(ZipEntry)
Gets an input stream for reading the given zip entry data in an uncompressed form. Normally the ZipEntry should be an entry returned by GetEntry().
Declaration
public Stream GetInputStream(ZipEntry entry)
Parameters
Type | Name | Description |
---|---|---|
ZipEntry | entry | The ZipEntry to obtain a data System.IO.Stream for |
Returns
Type | Description |
---|---|
System.IO.Stream | An input System.IO.Stream containing data for this ZipEntry |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | The ZipFile has already been closed |
ZipException | The compression method for the entry is unknown |
System.IndexOutOfRangeException | The entry is not found in the ZipFile |
GetInputStream(Int64)
Creates an input stream reading a zip entry
Declaration
public Stream GetInputStream(long entryIndex)
Parameters
Type | Name | Description |
---|---|---|
System.Int64 | entryIndex | The index of the entry to obtain an input stream for. |
Returns
Type | Description |
---|---|
System.IO.Stream | An input System.IO.Stream containing data for this |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | The ZipFile has already been closed |
ZipException | The compression method for the entry is unknown |
System.IndexOutOfRangeException | The entry is not found in the ZipFile |
SetComment(String)
Set the file comment to be recorded when the current update is commited.
Declaration
public void SetComment(string comment)
Parameters
Type | Name | Description |
---|---|---|
System.String | comment | The comment to record. |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | ZipFile has been closed. |
TestArchive(Boolean)
Test an archive for integrity/validity
Declaration
public bool TestArchive(bool testData)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | testData | Perform low level data Crc check |
Returns
Type | Description |
---|---|
System.Boolean | true if all tests pass, false otherwise |
Remarks
Testing will terminate on the first error found.
TestArchive(Boolean, TestStrategy, ZipTestResultHandler)
Test an archive for integrity/validity
Declaration
public bool TestArchive(bool testData, TestStrategy strategy, ZipTestResultHandler resultHandler)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | testData | Perform low level data Crc check |
TestStrategy | strategy | The TestStrategy to apply. |
ZipTestResultHandler | resultHandler | The ZipTestResultHandler handler to call during testing. |
Returns
Type | Description |
---|---|
System.Boolean | true if all tests pass, false otherwise |
Exceptions
Type | Condition |
---|---|
System.ObjectDisposedException | The object has already been closed. |
Explicit Interface Implementations
| Improve this Doc View SourceIDisposable.Dispose()
Declaration
void IDisposable.Dispose()