Archive

Archive constructor (1 of 4)

Initializes a new instance of the Archive class with optional settings for its entries.

public Archive(ArchiveEntrySettings newEntrySettings = null)
ParameterTypeDescription
newEntrySettingsArchiveEntrySettingsCompression and encryption settings used for newly added ArchiveEntry items. If not specified, most common Deflate compression without encryption would be used.

Examples

The following example shows how to compress a single file with default settings.

using (FileStream zipFile = File.Open("archive.zip", FileMode.Create))
{
    using (var archive = new Archive())
    {
        archive.CreateEntry("data.bin", "file.dat");
        archive.Save(zipFile);
    }
}

See Also


Archive constructor (2 of 4)

Initializes a new instance of the Archive class and composes entries list can be extracted from the archive.

public Archive(Stream sourceStream, ArchiveLoadOptions loadOptions = null, 
    ArchiveEntrySettings newEntrySettings = null)
ParameterTypeDescription
sourceStreamStreamThe source of the archive.
loadOptionsArchiveLoadOptionsOptions to load existing archive with.
newEntrySettingsArchiveEntrySettingsCompression and encryption settings used for newly added ArchiveEntry items. If not specified, most common Deflate compression without encryption would be used.

Exceptions

exceptioncondition
ArgumentExceptionsourceStream is not seekable.
InvalidDataExceptionEncryption header for AES contradicts WinZip compression method.

Remarks

This constructor does not decompress any entry. See Open method for decompressing.

Examples

The following example extract an encrypted archive, then decompress first entry to a MemoryStream.

var fs = File.OpenRead("encrypted.zip");
var extracted = new MemoryStream();
using (Archive archive = new Archive(fs, new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }))
{
    using (var decompressed = archive.Entries[0].Open())
    {
        byte[] b = new byte[8192];
        int bytesRead;
        while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
            extracted.Write(b, 0, bytesRead);
    }
}

See Also


Archive constructor (3 of 4)

Initializes a new instance of the Archive class and composes entries list can be extracted from the archive.

public Archive(string path, ArchiveLoadOptions loadOptions = null, 
    ArchiveEntrySettings newEntrySettings = null)
ParameterTypeDescription
pathStringThe fully qualified or the relative path to the archive file.
loadOptionsArchiveLoadOptionsOptions to load existing archive with.
newEntrySettingsArchiveEntrySettingsCompression and encryption settings used for newly added ArchiveEntry items. If not specified, most common Deflate compression without encryption would be used.

Exceptions

exceptioncondition
ArgumentNullExceptionpath is null.
SecurityExceptionThe caller does not have the required permission to access.
ArgumentExceptionThe path is empty, contains only white spaces, or contains invalid characters.
UnauthorizedAccessExceptionAccess to file path is denied.
PathTooLongExceptionThe specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
NotSupportedExceptionFile at path contains a colon (:) in the middle of the string.

Remarks

This constructor does not decompress any entry. See Open method for decompressing.

Examples

The following example extract an encrypted archive, then decompress first entry to a MemoryStream.

var extracted = new MemoryStream();
using (Archive archive = new Archive("encrypted.zip", new ArchiveLoadOptions() { DecryptionPassword = "p@s$" }))
{
    using (var decompressed = archive.Entries[0].Open())
    {
        byte[] b = new byte[8192];
        int bytesRead;
        while (0 < (bytesRead = decompressed.Read(b, 0, b.Length)))
            extracted.Write(b, 0, bytesRead);
    }
}

See Also


Archive constructor (4 of 4)

Initializes a new instance of the Archive class from multi-volume zip archive and composes entries list can be extracted from the archive.

public Archive(string mainSegment, string[] segmentsInOrder, ArchiveLoadOptions loadOptions = null)
ParameterTypeDescription
mainSegmentStringPath to the last segment of multi-volume archive with the central directory.
segmentsInOrderString[]Paths to each segment but the last of multi-volume zip archive respecting order.
loadOptionsArchiveLoadOptionsOptions to load existing archive with.

Examples

This sample extract to a directory an archive of three segments.

using (Archive a = new Archive("archive.zip", new string[] { "archive.z01", "archive.z02" }))
{
    a.ExtractToDirectory("destination");
}

See Also