C# 文件压缩与解压(ZIP)基于 .Net Framework

基于 .Net Framework 类库的文件压缩与解压

  • 从 .Net Framework 4.5 开始增加了对文件进行压缩与解压操作的类库

注:需要引用的程序集

using System.IO.Compression;
using System.IO.Compression.FileSystem;
  • 类似于对文件和目录的操作,对于压缩文件也提供了两种方式:ZipArchiveZipFile,分别对应两个新增加的类 ZipArchiveZipFile。这两个类都定义在命名空间 System.IO.Compression 中。

定义路径

  • 为了后面演示方便,我们定义一个表示压缩文件路径的常量。
const string zipFilePath = @"..\..\Sample.zip";

使用 ZipArchive

创建压缩文件

  • 创建一个空的压缩文件,使用 ZipArchiveMode.Create 创建参数。
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Create))
{
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Create))
    {
    }
}

创建并添加文件

  • 通常,在创建的同时,我们就会加入一些文件,下面的例子中,我们将当前的执行程序文件本身加到压缩文件中。
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Create))
{
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Create))
    {
        System.Reflection.Assembly assemble = System.Reflection.Assembly.GetExecutingAssembly();
        string path = assemble.Location;
        string filename = System.IO.Path.GetFileName(path);
        ZipArchiveEntry readMeEntry = archive.CreateEntry(filename);
        using (System.IO.Stream stream = readMeEntry.Open())
        {
            byte[] bytes = System.IO.File.ReadAllBytes(path);
            stream.Write(bytes, 0, bytes.Length);
        }
    }
}

列出压缩文件内容

  • 当然,也可以通过程序检查压缩文件的内容了。使用 Read 方式就可以了。
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
{
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read))
    {
        foreach (var zipArchiveEntry in archive.Entries)
        Console.WriteLine("FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName);
    }
}

提取压缩文件

  • 当然可以从压缩文件中提取被压缩的内容了。
// 读取其中一个文件的内容
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
{
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read))
    {
        // 解压某个文件
        ZipArchiveEntry entry = archive.GetEntry("ZipArchiveSample.exe");
        Console.WriteLine(entry.Name);
        using (System.IO.Stream stream = entry.Open())
        {
            System.IO.Stream output = new FileStream("http://www.cnblogs.com/ZipArchiveSample.exe", FileMode.Create);
            int b = -1;
            while ((b = stream.ReadByte()) != -1)
            {
                output.WriteByte((byte) b);
            }
            output.Close();
        }
    }
}

更新压缩文件

  • 在压缩文件已经创建之后,还可以打开它,继续添加文件,这就称为更新了,使用 Update 模式。
// 向现有的压缩文件中添加文件
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
{
    using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Update))
    {
        // 这里添加当前正在执行的程序文件本身
        System.Reflection.Assembly assemble = System.Reflection.Assembly.GetExecutingAssembly();
        string path = assemble.Location;
        string filename = System.IO.Path.GetFileName( path);
        ZipArchiveEntry readMeEntry = archive.CreateEntry( filename );
        using (System.IO.Stream stream = readMeEntry.Open() )
        {
            byte[] bytes = System.IO.File.ReadAllBytes(path);
            stream.Write(bytes, 0, bytes.Length);
        }
        foreach (var zipArchiveEntry in archive.Entries)
        {
            Console.WriteLine("FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName);
        }
    }
}

使用 ZipFile

  • 除了上边的基本方法之外,还有一些简单的使用方法。这涉及到另外一个类:ZipFile。

创建空压缩文件

// 删除压缩文件
System.IO.File.Delete(zipFilePath);

// 使用 ZipFile 的静态方法创建压缩文件,要保证文件没有存在
using (ZipArchive zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
{
}

创建并添加文件

  • 直接添加一个文件的方法。直接使用 CreateEntryFromFile 就可以了。
System.IO.File.Delete(zipFilePath);
// 使用 CreateEntryFromFile 方法添加文件
// 使用 ZipFile 的静态方法创建压缩文件
using (ZipArchive zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
{
    System.Reflection.Assembly assemble = System.Reflection.Assembly.GetExecutingAssembly();
    string path = assemble.Location;
    string filename = System.IO.Path.GetFileName(path);
    zipArchive.CreateEntryFromFile(path, filename);
}

解压文件

  • 将压缩文件解压到指定的目录中。
// 解压文件
ZipFile.ExtractToDirectory(zipFilePath, "../..");

压缩一个目录

  • 还可以直接将一个目录中所有的文件都压缩到一个压缩文件中。
// 压缩指定目录中所有文件
System.IO.File.Delete(zipFilePath);
ZipFile.CreateFromDirectory(".", zipFilePath);
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值