Class ArchiveEntry

Class ArchiveEntry

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

表示归档中的单个文件。

public abstract class ArchiveEntry : IArchiveFileEntry

继承

objectArchiveEntry

派生

ArchiveEntryEncrypted, ArchiveEntryPlain

实现

IArchiveFileEntry

继承成员

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

备注

将 Aspose.Zip.ArchiveEntry 实例转换为 Aspose.Zip.ArchiveEntryEncrypted 以确定条目是否被加密。

构造函数

ArchiveEntry(string, CompressionSettings, Func<stream>, FileAttributes)

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

protected ArchiveEntry(string name, CompressionSettings compressionSettings, Func<stream> sourceProvider, FileAttributes fileAttributes)

参数

name string

条目名称。

compressionSettings CompressionSettings

压缩或解压缩的设置。

sourceProvider Func<Stream&gt;

返回包含条目数据的流的方法,供压缩使用。

fileAttributes FileAttributes

来自文件系统的属性。

ArchiveEntry(string, CompressionSettings, Stream, FileAttributes, FileSystemInfo)

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

protected ArchiveEntry(string name, CompressionSettings compressionSettings, Stream source, FileAttributes fileAttributes, FileSystemInfo fileInfo = null)

参数

name string

条目名称。

compressionSettings CompressionSettings

压缩或解压缩的设置。

source Stream

包含条目数据的流,供压缩或解压缩使用。

fileAttributes FileAttributes

来自文件系统的属性。

fileInfo FileSystemInfo

条目所基于的文件或目录信息。

属性

Comment

获取归档中条目的注释。

public string Comment { get; protected set; }

属性值

string

CompressedSize

获取压缩文件的大小。

public ulong CompressedSize { get; }

属性值

ulong

CompressionSettings

获取压缩或解压缩的设置。

public CompressionSettings CompressionSettings { get; }

属性值

CompressionSettings

DataSource

如果条目已添加到归档中而不是提取,则获取条目的源。

public Stream DataSource { get; }

属性值

Stream

备注

在赋值之前,源为 null。此源可能在某些情况下通过 Archive.Save 方法分配。

FileAttributes

获取主机系统中的文件属性。

protected FileAttributes FileAttributes { get; }

属性值

FileAttributes

IsDirectory

获取一个值,指示条目是否表示目录。

public bool IsDirectory { get; }

属性值

bool

ModificationTime

获取或设置最后修改的日期和时间。

public DateTime ModificationTime { get; set; }

属性值

DateTime

Name

获取归档中条目的名称。

public string Name { get; protected set; }

属性值

string

UncompressedSize

获取原始文件的大小。

public ulong UncompressedSize { get; }

属性值

ulong

方法

Extract(string, string)

通过提供的路径将条目提取到文件系统中。

public FileInfo Extract(string path, string password = null)

参数

path string

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

password string

可选的解密密码。

返回

FileInfo

组合文件的信息。

示例

提取 zip 归档的两个条目,每个条目都有自己的密码

using (FileStream zipFile = File.Open("archive.zip", FileMode.Open))
{
    using (Archive archive = new Archive(zipFile))
    {
        archive.Entries[0].Extract("first.bin", "first_pass");
        archive.Entries[1].Extract("second.bin", "second_pass");
    }
}

异常

ArgumentNullException

path 为 null。

SecurityException

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

ArgumentException

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

UnauthorizedAccessException

访问文件 path 被拒绝。

PathTooLongException

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

NotSupportedException

path 中间包含冒号 (:)。

FileNotFoundException

文件未找到。

DirectoryNotFoundException

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

IOException

文件已打开。

InvalidDataException

数据已损坏。-或- CRC 或 MAC 验证失败。

Extract(Stream, string)

将条目提取到提供的流中。

public void Extract(Stream destination, string password = null)

参数

destination Stream

目标流。必须可写。

password string

可选的解密密码。

示例

使用密码提取 zip 归档的一个条目。

using (FileStream zipFile = File.Open("archive.zip", FileMode.Open))
{
    using (Archive archive = new Archive(zipFile))
    {
        archive.Entries[0].Extract(httpResponseStream, "p@s$");
    }
}

异常

InvalidDataException

数据已损坏。-或- CRC 或 MAC 验证失败。

IOException

源已损坏或不可读。

ArgumentException

destination 不支持写入。

Open(string)

打开条目以进行提取,并提供包含解压缩条目内容的流。

public Stream Open(string password = null)

参数

password string

可选的解密密码。

返回

Stream

表示条目内容的流。

示例

用法: Stream decompressed = entry.Open();

.NET 4.0 及更高版本 - 使用 Stream.CopyTo 方法: decompressed.CopyTo(httpResponse.OutputStream)

.NET 3.5 及之前版本 - 手动复制字节:

byte[] buffer = new byte[8192];
int bytesRead;
while (0 &lt; (bytesRead = decompressed.Read(buffer, 0, buffer.Length)))
 fileStream.Write(buffer, 0, bytesRead);
```</p>

#### 备注

<p>从流中读取以获取文件的原始内容。请参阅示例部分。</p>

#### 异常

[InvalidOperationException](https://learn.microsoft.com/dotnet/api/system.invalidoperationexception)

归档处于不正确的状态。

### <a id="Aspose_Zip_ArchiveEntry_CompressionProgressed"></a> CompressionProgressed

在压缩原始流的一部分时引发。

```csharp
public event EventHandler<progresseventargs> CompressionProgressed

事件类型

EventHandler<ProgressEventArgs&gt;

示例

archive.Entries[0].CompressionProgressed += (s, e) =&gt; { int percent = (int)((100 * (long)e.ProceededBytes) / entrySourceStream.Length); };

备注

事件发送者是 Aspose.Zip.ArchiveEntry 实例。

ExtractionProgressed

在提取原始流的一部分时引发。

public event EventHandler<progresseventargs> ExtractionProgressed

事件类型

EventHandler<ProgressEventArgs&gt;

示例

archive.Entries[0].ExtractionProgressed += (s, e) =&gt; { int percent = (int)((100 * e.ProceededBytes) / ((ArchiveEntry)s).UncompressedSize); };

备注

事件发送者是 Aspose.Zip.ArchiveEntry 实例。

 中文