using System;
using System.IO;
using System.IO.Compression;
class Program
{
    static void Main(string[] args)
    {
        string startPath = @"c:\example\start";
        string zipPath = @"c:\example\result.zip";
        string extractPath = @"c:\example\extract";
        // 创建一个压缩文件
        ZipFile.CreateFromDirectory(startPath, zipPath);
        Console.WriteLine("压缩完成。");
        // 解压文件到指定目录
        ZipFile.ExtractToDirectory(zipPath, extractPath);
        Console.WriteLine("解压完成。");
    }
}命名空间导入:
System.IO 和 System.IO.Compression 提供了对文件系统和压缩文件操作的支持。路径定义:
startPath: 指定要压缩的文件夹路径。zipPath: 指定生成的 ZIP 文件路径。extractPath: 指定解压后的文件存放路径。压缩文件:
ZipFile.CreateFromDirectory 方法将指定文件夹中的所有文件和子文件夹压缩成一个 ZIP 文件。解压文件:
ZipFile.ExtractToDirectory 方法将 ZIP 文件解压到指定的文件夹中。控制台输出:
通过这段代码,你可以轻松实现文件夹的压缩和解压操作。
上一篇:c# partial class
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站