Class ZstandardArchive

Class ZstandardArchive

Namespace: Aspose.Zip.Zstandard
Assembly: Aspose.Zip.dll (25.1.0)

此类表示 Zstandard 压缩文件。使用它来组合 Zstandard 压缩文件。

public class ZstandardArchive : IArchive, IDisposable, IArchiveFileEntry

继承

objectZstandardArchive

实现

IArchive, IDisposable, IArchiveFileEntry

继承成员

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

构造函数

ZstandardArchive()

初始化 Aspose.Zip.Zstandard.ZstandardArchive 类的新实例,以便进行压缩。

public ZstandardArchive()

示例

以下示例演示如何压缩文件。

using (ZstandardArchive archive = new ZstandardArchive()) 
{
    archive.SetSource("data.bin");
    archive.Save("archive.zst");
}

ZstandardArchive(Stream, ZstandardLoadOptions)

初始化 Aspose.Zip.Zstandard.ZstandardArchive 类的新实例,以便进行解压缩。

public ZstandardArchive(Stream sourceStream, ZstandardLoadOptions options = null)

参数

sourceStream Stream

压缩文件的来源。

options ZstandardLoadOptions

加载压缩文件的选项。

示例

从流中打开压缩文件并提取到 MemoryStream

var ms = new MemoryStream();
using (GzipArchive archive = new ZstandardArchive(File.OpenRead("archive.zst")))
  archive.Open().CopyTo(ms);

备注

此构造函数不进行解压缩。有关解压缩的详细信息,请参见 Aspose.Zip.Zstandard.ZstandardArchive.Open 方法。

ZstandardArchive(string, ZstandardLoadOptions)

初始化 Aspose.Zip.Zstandard.ZstandardArchive 类的新实例。

public ZstandardArchive(string path, ZstandardLoadOptions options = null)

参数

path string

压缩文件的路径。

options ZstandardLoadOptions

加载压缩文件的选项。

示例

通过路径从文件中打开压缩文件并提取到 MemoryStream

var ms = new MemoryStream();
using (ZstandardArchive archive = new ZstandardArchive("archive.zst"))
  archive.Open().CopyTo(ms);

备注

此构造函数不进行解压缩。有关解压缩的详细信息,请参见 Aspose.Zip.Zstandard.ZstandardArchive.Open 方法。

异常

ArgumentNullException

path 为 null。

SecurityException

调用者没有访问所需权限。

ArgumentException

path 为空,仅包含空格或包含无效字符。

UnauthorizedAccessException

访问文件 path 被拒绝。

PathTooLongException

指定的 path、文件名或两者超过系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须少于 248 个字符,文件名必须少于 260 个字符。

NotSupportedException

文件 path 在字符串中间包含冒号 (:)。

方法

Dispose()

执行与释放、释放或重置非托管资源相关的应用程序定义任务。

public void Dispose()

Dispose(bool)

执行与释放、释放或重置非托管资源相关的应用程序定义任务。

protected virtual void Dispose(bool disposing)

参数

disposing bool

是否应释放托管资源。

Extract(Stream)

将压缩文件提取到提供的流中。

public void Extract(Stream destination)

参数

destination Stream

目标流。必须可写。

示例

using (var archive = new GzipArchive("archive.zst"))
{
     archive.Extract(httpResponseStream);
}

异常

ArgumentException

destination 不支持写入。

Extract(string)

将压缩文件提取到指定路径的文件中。

public FileInfo Extract(string path)

参数

path string

目标文件的路径。如果文件已存在,将被覆盖。

返回

FileInfo

提取文件的信息。

异常

ArgumentNullException

path 为 null。

SecurityException

调用者没有访问所需权限。

ArgumentException

path 为空,仅包含空格或包含无效字符。

UnauthorizedAccessException

访问文件 path 被拒绝。

PathTooLongException

指定的 path、文件名或两者超过系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须少于 248 个字符,文件名必须少于 260 个字符。

NotSupportedException

文件 path 在字符串中间包含冒号 (:)。

ExtractToDirectory(string)

将压缩文件的内容提取到提供的目录中。

public void ExtractToDirectory(string destinationDirectory)

参数

destinationDirectory string

提取文件的目标目录的路径。

备注

如果目录不存在,将会创建。

异常

ArgumentNullException

destinationDirectory 为 null。

PathTooLongException

指定的路径、文件名或两者超过系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须少于 248 个字符,文件名必须少于 260 个字符。

SecurityException

调用者没有访问现有目录的所需权限。

NotSupportedException

如果目录不存在,路径中包含不是驱动器标签 (“C:") 的冒号字符 (:)。

ArgumentException

destinationDirectory 是零长度字符串,仅包含空格,或包含一个或多个无效字符。您可以使用 System.IO.Path.GetInvalidPathChars 方法查询无效字符。 -或- 路径以冒号字符 (:) 为前缀或仅包含冒号字符。

IOException

指定的路径是文件。-或- 网络名称未知。

Open()

打开压缩文件以进行提取,并提供一个包含压缩内容的流。

public Stream Open()

返回

Stream

表示压缩文件内容的流。

示例

提取压缩文件并将提取的内容复制到文件流。

using (var archive = new ZstandardArchive("archive.zst"))
{
    using (var extracted = File.Create("data.bin"))
    {
        var unpacked = archive.Open();
        byte[] b = new byte[8192];
        int bytesRead;
        while (0 < (bytesRead = unpacked.Read(b, 0, b.Length)))
            extracted.Write(b, 0, bytesRead);
    }            
}

您可以使用 .NET 4.0 及更高版本的 Stream.CopyTo 方法: `unpacked.CopyTo(extracted);`

备注

从流中读取以获取文件的原始内容。有关示例,请参见示例部分。

Save(Stream, ZstandardSaveOptions)

将压缩文件保存到提供的流中。

public void Save(Stream outputStream, ZstandardSaveOptions settings = null)

参数

outputStream Stream

目标流。

settings ZstandardSaveOptions

压缩文件的可选设置。

示例

将压缩数据写入 http 响应流。

using (var archive = new ZstandardArchive()) 
{
    archive.SetSource(new FileInfo("data.bin"));
    archive.Save(httpResponse.OutputStream);
}

备注

outputStream 必须可写。

异常

ArgumentException

outputStream 不可写。

InvalidOperationException

未提供源。

Save(string, ZstandardSaveOptions)

将压缩文件保存到提供的目标文件中。

public void Save(string destinationFileName, ZstandardSaveOptions settings = null)

参数

destinationFileName string

要创建的压缩文件的路径。如果指定的文件名指向现有文件,将被覆盖。

settings ZstandardSaveOptions

压缩文件的可选设置。

示例

using (var archive = new ZstandardArchive()) 
{
    archive.SetSource(new FileInfo("data.bin"));
    archive.Save("result.zst");
}

异常

ArgumentNullException

destinationFileName 为 null。

SecurityException

调用者没有访问所需权限。

ArgumentException

destinationFileName 为空,仅包含空格或包含无效字符。

UnauthorizedAccessException

访问文件 destinationFileName 被拒绝。

PathTooLongException

指定的 destinationFileName、文件名或两者超过系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须少于 248 个字符,文件名必须少于 260 个字符。

NotSupportedException

文件 destinationFileName 在字符串中间包含冒号 (:)。

Save(FileInfo, ZstandardSaveOptions)

将压缩文件保存到提供的目标文件中。

public void Save(FileInfo destination, ZstandardSaveOptions settings = null)

参数

destination FileInfo

将被打开为目标流的 FileInfo。

settings ZstandardSaveOptions

压缩文件的可选设置。

示例

using (var archive = new ZstandardArchive()) 
{
    archive.SetSource(new FileInfo("data.bin"));
    archive.Save(new FileInfo("archive.zst"));
}

异常

SecurityException

调用者没有打开 destination 所需的权限。

ArgumentException

文件路径为空或仅包含空格。

FileNotFoundException

未找到文件。

UnauthorizedAccessException

文件路径为只读或是目录。

ArgumentNullException

destination 为 null。

DirectoryNotFoundException

指定的路径无效,例如位于未映射的驱动器上。

IOException

文件已打开。

SetSource(Stream)

设置要在压缩文件中压缩的内容。

public void SetSource(Stream source)

参数

source Stream

压缩文件的输入流。

示例

using (var archive = new ZstandardArchive())
{
    archive.SetSource(new MemoryStream(new byte[] { 0x00, 0xFF }));
    archive.Save("archive.zst");
}

SetSource(FileInfo)

设置要在压缩文件中压缩的内容。

public void SetSource(FileInfo fileInfo)

参数

fileInfo FileInfo

要压缩的文件的引用。

示例

using (var archive = new ZstandardArchive()) 
{
    archive.SetSource(new FileInfo("data.bin"));
    archive.Save("archive.zst");
}

SetSource(string)

设置要在压缩文件中压缩的内容。

public void SetSource(string path)

参数

path string

要压缩的文件路径。

示例

using (var archive = new ZstandardArchive()) 
{
    archive.SetSource("data.bin");
    archive.Save("archive.zst");
}

异常

ArgumentNullException

path 为 null。

SecurityException

调用者没有访问所需权限。

ArgumentException

path 为空,仅包含空格或包含无效字符。

UnauthorizedAccessException

访问文件 path 被拒绝。

PathTooLongException

指定的 path、文件名或两者超过系统定义的最大长度。例如,在基于 Windows 的平台上,路径必须少于 248 个字符,文件名必须少于 260 个字符。

NotSupportedException

文件 path 在字符串中间包含冒号 (:)。

 中文