Archive.Archive

Archive(ArchiveEntrySettings)

初始化一个新的实例Archive类及其条目的可选设置.

public Archive(ArchiveEntrySettings newEntrySettings = null)
范围类型描述
newEntrySettingsArchiveEntrySettings用于新添加的压缩和加密设置ArchiveEntry items. 如果未指定,将使用最常见的没有加密的 Deflate 压缩。

例子

以下示例显示如何使用默认设置压缩单个文件。

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

也可以看看


Archive(Stream, ArchiveLoadOptions, ArchiveEntrySettings)

初始化一个新的实例Archive可以从存档中提取类和组合条目列表。

public Archive(Stream sourceStream, ArchiveLoadOptions loadOptions = null, 
    ArchiveEntrySettings newEntrySettings = null)
范围类型描述
sourceStreamStream存档的来源。
loadOptionsArchiveLoadOptions加载现有存档的选项。
newEntrySettingsArchiveEntrySettings用于新添加的压缩和加密设置ArchiveEntry items. 如果未指定,将使用最常见的没有加密的 Deflate 压缩。

例外

例外(健康)状况
ArgumentExceptionsourceStream不可搜索。
InvalidDataExceptionAES 的加密标头与 WinZip 压缩方法相矛盾。

评论

此构造函数不解压缩任何条目。看Open解压方法.

例子

以下示例提取加密存档,然后将第一个条目解压缩为内存流.

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);
    }
}

也可以看看


Archive(string, ArchiveLoadOptions, ArchiveEntrySettings)

初始化一个新的实例Archive可以从存档中提取类和组合条目列表。

public Archive(string path, ArchiveLoadOptions loadOptions = null, 
    ArchiveEntrySettings newEntrySettings = null)
范围类型描述
pathString存档文件的完全限定路径或相对路径。
loadOptionsArchiveLoadOptions加载现有存档的选项。
newEntrySettingsArchiveEntrySettings用于新添加的压缩和加密设置ArchiveEntry items. 如果未指定,将使用最常见的没有加密的 Deflate 压缩。

例外

例外(健康)状况
ArgumentNullExceptionpath一片空白。
SecurityException调用者没有所需的访问权限。
ArgumentExceptionpath为空、仅包含空格或包含无效字符。
UnauthorizedAccessException访问文件path被拒绝。
PathTooLongException指定的path、文件名或两者都超过了系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须少于 248 个字符,文件名必须少于 260 个字符。
NotSupportedException归档于path在字符串中间包含一个冒号 (:)。

评论

此构造函数不解压缩任何条目。看Open解压方法.

例子

以下示例提取加密存档,然后将第一个条目解压缩为内存流.

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);
    }
}

也可以看看