如何压缩文件夹和文件?

How to zip a folder and a file?

我想压缩一个包含 1 个文件和 1 个子文件夹(里面有一些文件)的文件夹。

zip.AddDirectory(pathNotesFile, string.Empty);
zip.AddFile(pathTempFiles + csvFileName, string.Empty);

我使用过 DotNetZip 库, 但是在使用上面这些代码之后,我得到的压缩文件没有子文件夹。

有什么方法可以创建一个包含子文件夹的 zip 文件吗???

在 DotNetZip 中,您无需调用 .AddDirectory() 方法即可完成此操作:

zip.AddFile(pathTempFiles + csvFileName, directoryPathInArchive: "Attachments");

我用ICSharpCode.SharpZipLib

public static void ZipPath(string zipFilePath, string sourceDir, string pattern, bool withSubdirs, string password)
{
    FastZip fz = new FastZip();
    if (password != null)
        fz.Password = password;

    fz.CreateZip(zipFilePath, sourceDir, withSubdirs, pattern);
}

希望这对您有所帮助