ZipArchiveEntry

如果在项目中引用 System.IO.Compression.FileSystem 程序集,则可以访问 ZipArchiveEntry 类的两个扩展方法。 这些方法是 ExtractToFile(ZipArchiveEntry, String) 和 ExtractToFile(ZipArchiveEntry, String, Boolean)的,它们使你能够将项的内容解压缩到文件中。System.IO.Compression.FileSystem 程序集在 Windows 8中不可用。 在 Windows 8.x 应用商店 应用中,可以通过使用 DeflateStream 或 GZipStream解压缩存档的内容,也可以使用 Windows 运行时 类型压缩程序和解压缩程序压缩和解压缩文件。
属性
Archive 获取该项所属的 zip 存档。
CompressedLength 获取项在 zip 存档中的已压缩大小。
Crc32 32 位循环冗余检查。
ExternalAttributes 操作系统和应用程序特定的文件属性。
FullName 获取 zip 存档中的项的相对路径。
LastWriteTime 获取或设置最近一次更改 zip 存档中的项的时间。
Length 获取 zip 存档中的项的未压缩大小。
Name 获取在 zip 存档中的项的文件名。
方法
Delete() 删除 zip 存档中的项。
Equals(Object) 确定指定的对象是否等于当前对象。
(继承自 Object)
GetHashCode() 作为默认哈希函数。
(继承自 Object)
GetType() 获取当前实例的 Type。
(继承自 Object)
MemberwiseClone() 创建当前 Object 的浅表副本。
(继承自 Object)
Open() 打开 zip 存档中的项。
ToString() 在 zip 存档中检索项的相对路径。
使用 ExtractToFile(ZipArchiveEntry, String) 扩展方法。 若要执行代码,必须在项目中引用 System.IO.Compression.FileSystem 程序集。
class Program
{
static void Main(string[] args)
{
string zipPath = @".\result.zip";

    Console.WriteLine("Provide path where to extract the zip file:");
    string extractPath = Console.ReadLine();

    // Normalizes the path.
    extractPath = Path.GetFullPath(extractPath);

    // Ensures that the last character on the extraction path
    // is the directory separator char.
    // Without this, a malicious zip file could try to traverse outside of the expected
    // extraction path.
    if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal))
        extractPath += Path.DirectorySeparatorChar;

    using (ZipArchive archive = ZipFile.OpenRead(zipPath))
    {
        foreach (ZipArchiveEntry entry in archive.Entries)
        {
            if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
            {
                // Gets the full path to ensure that relative segments are removed.
                string destinationPath = Path.GetFullPath(Path.Combine(extractPath, entry.FullName));

                // Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
                // are case-insensitive.
                if (destinationPath.StartsWith(extractPath, StringComparison.Ordinal))
                    entry.ExtractToFile(destinationPath);
            }  }  }  }  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值