Skip to main content
 首页 » 编程设计

c#之如何使用 ICSharplib 压缩带加密密码的文件夹

2024年02月20日13sky-heaven

如何使用 ICSharplib 压缩文件夹。 有什么办法可以在压缩时添加加密密码吗? 没有我可以使用任何其他 dll 的选项。必须仅使用 ICSharplib。

目前我正在使用这个代码块

private static void CompressFiles(string folderPath) { 
    string zipOutput = @"C:\temp\myoutput.zip"; 
    try { 
        using (ZipOutputStream zs = new ZipOutputStream(File.Create(zipOutput))) { 
            zs.SetLevel(9); // 0-9 (9 being best compression) 
            foreach (string file in Directory.GetFiles(folderPath)) { 
                ZipEntry entry = new ZipEntry(Path.GetFileName(file)); 
                entry.DateTime = DateTime.Now; 
                using (FileStream fs = File.OpenRead(file)) { 
                    byte[] buffer = new byte[fs.Length]; 
                    fs.Read(buffer, 0, buffer.Length); 
                    entry.Size = buffer.Length; // This is very important 
                    zs.PutNextEntry(entry); 
                    zs.Write(buffer, 0, buffer.Length); 
                } 
            } 
            zs.Finish(); 
            zs.Close(); 
        } 
    } 
    catch { throw; } 
} 

它可以压缩文件夹中的所有文件。

但我想要的是压缩整个文件夹。 就像该文件夹中的文件夹一样,该文件夹也包含在 zip 文件中。

提前致谢

请您参考如下方法:

使用FastZip 对象。

ICSharpCode.SharpZipLib.Zip.FastZip z = new ICSharpCode.SharpZipLib.Zip.FastZip(); 
z.CreateEmptyDirectories = true; 
z.CreateZip("F:\\ZipTest.zip", "F:\\ZipTest\\", true, "");  
 
if (File.Exists("F:\\ZipTest.zip")) 
    Console.WriteLine("Done"); 
else 
    Console.WriteLine("Failed");