c#

C# Zip压缩文件目录如何操作

小樊
162
2024-07-13 14:26:26
栏目: 编程语言

在C#中,可以使用System.IO.Compression命名空间中的ZipFile类来压缩文件目录。以下是一个示例代码,演示了如何压缩一个文件夹:

using System;
using System.IO;
using System.IO.Compression;

class Program
{
    static void Main()
    {
        string startPath = @"C:\example\folderToCompress";
        string zipPath = @"C:\example\result.zip";

        ZipFile.CreateFromDirectory(startPath, zipPath);
    }
}

在上面的示例中,我们首先指定要压缩的文件夹路径startPath,然后指定压缩后的Zip文件路径zipPath。然后使用ZipFile.CreateFromDirectory方法将文件夹压缩为一个Zip文件。

需要注意的是,ZipFile类是.NET Framework 4.5及以上版本中的新功能,因此在更早的版本中可能无法使用。如果需要在较早的版本中进行压缩操作,可以考虑使用第三方库,如SharpZipLib。

0
看了该问题的人还看了