gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
c# 多个文件夹压缩成一个压缩包
- User
c# 多个文件夹压缩成一个压缩包
- Assistant
要将多个文件夹压缩成一个压缩包,您可以使用 System.IO.Compression 命名空间中的 ZipArchive 类来实现。下面是一个示例代码,它将多个文件夹及其内容压缩成一个 zip 文件:
using System; using System.IO; using System.IO.Compression; public class Program { public static void Main(string[] args) { // 定义要压缩的文件夹路径 string[] foldersToCompress = new string[] { @"C:\path\to\folder1", @"C:\path\to\folder2", @"C:\path\to\folder3" }; // 定义输出的 zip 文件路径 string outputZipFile = @"C:\path\to\output.zip"; // 创建一个新的 zip 文件 using (FileStream zipStream = new FileStream(outputZipFile, FileMode.Create)) { using (ZipArchive zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create)) { // 遍历要压缩的文件夹 foreach (string folderPath in foldersToCompress) { // 获取文件夹的文件和子文件夹 string[] files = Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories); // 遍历文件夹中的文件 foreach (string filePath in files) { // 获取文件在 zip 中的相对路径 string entryPath = filePath.Replace(folderPath, "").TrimStart('\\'); // 创建一个新的 zip 条目 ZipArchiveEntry entry = zipArchive.CreateEntry(entryPath); // 将文件内容添加到 zip 条目中 using (Stream entryStream = entry.Open()) { using (FileStream fileStream = new FileStream(filePath, FileMode.Open)) { fileStream.CopyTo(entryStream); } } } } } } Console.WriteLine("压缩完成!"); } }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.