压缩在 C# 中创建的文件夹

CBr*_*eze 2 c# zip dotnetzip

我正在 C# 中创建一个文件夹,我希望在创建后立即将其压缩。我环顾四周(如何压缩文件夹),(http://dotnetzip.codeplex.com/)但到目前为止还没有运气。我有点担心使用 dotnetzip,因为它的最后一个版本是 5 年前。

dotnetzip 在 Visual Studio 2015 中是否仍然相关,或者是否有更现代的方式在 C# 中压缩文件夹而不使用包?

这就是我复制文件夹的方式;

    private static void CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
    {

        SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
        DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";

        if (Directory.Exists(SourcePath))
        {
            if (Directory.Exists(DestinationPath) == false)
                Directory.CreateDirectory(DestinationPath);

            foreach (string fls in Directory.GetFiles(SourcePath))
            {
                FileInfo flinfo = new FileInfo(fls);
                flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
            }
            foreach (string drs in Directory.GetDirectories(SourcePath))
            {
                DirectoryInfo drinfo = new DirectoryInfo(drs);
                CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我希望在此之后压缩创建的文件夹。

stu*_*rtd 6

要压缩文件夹,.Net 4.5 框架包含ZipFile.CreateFromDirectory

string startPath = @"c:\example\start";
string zipPath = @"c:\example\result.zip";

ZipFile.CreateFromDirectory(startPath, zipPath);
Run Code Online (Sandbox Code Playgroud)