简体   繁体   English

压缩用C#创建的文件夹

[英]Zip a Folder Created in C#

I'm creating a folder in C# and I'm hoping to zip it up as soon as I've created it. 我正在C#中创建一个文件夹,希望在创建后立即将其压缩。 I've had a look around ( How to zip a folder ), ( http://dotnetzip.codeplex.com/ ) but no luck so far. 我环顾了一下( 如何压缩文件夹 ),( http://dotnetzip.codeplex.com/ ),但到目前为止还没有运气。 I'm a bit apprehensive of using dotnetzip as it's last release was 5 years ago. 我有点担心使用dotnetzip,因为它的最新发行是在5年前。

Is dotnetzip still relevant in Visual Studio 2015 or is there a more modern way of zipping folders in C# without using a package? dotnetzip在Visual Studio 2015中是否仍然有用,或者是否有更现代的方式在C#中不使用软件包来压缩文件夹?

This is how I'm copying the folders; 这就是我复制文件夹的方式;

    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);
            }
        }
    }

I'm looking to zip the created folder after this. 我想在此之后压缩创建的文件夹。

To zip a folder, the .Net 4.5 framework contains ZipFile.CreateFromDirectory : 要压缩文件夹,.Net 4.5框架包含ZipFile.CreateFromDirectory

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

ZipFile.CreateFromDirectory(startPath, zipPath);

Just wanted to add my two cents to the post that you already accepted as the answer. 只是想在您已经接受作为答案的帖子中加上我的两分钱。 Let's say you have a folder structure: 假设您有一个文件夹结构:

C:\RootFolder
C:\RootFolder\Folder1
C:\RootFolder\Folder1\Folder1a
C:\RootFolder\Folder1\Folder1b
C:\RootFolder\file1.txt
C:\RootFolder\file2.txt

ZipFile.CreateFromDirectory is definitely the way to go. ZipFile.CreateFromDirectory绝对是必经之路。 No 3-rd party libs required. 无需第三方库。 You simply need to reference the System.IO.Compression.FileSystem 您只需要引用System.IO.Compression.FileSystem

What I wanted to point out is: You can either zip the entire RootFolder including it to the archive 我想指出的是:您可以将包括它在内的整个RootFolder压缩到存档中

ZipFile.CreateFromDirectory(@"C:\RootFolder", @"C:\RootFolder.zip", 
                            CompressionLevel.Optimal, true);

or the contents of the RootFolder without the folder itself 或没有文件夹本身的RootFolder的内容

ZipFile.CreateFromDirectory(@"C:\RootFolder", @"C:\RootFolder.zip", 
                            CompressionLevel.Optimal, false);

The fourth parameter (bool includeBaseDirectory) gives us this flexibility . 第四个参数(bool includeBaseDirectory)为我们提供了这种灵活性 Hope it helps someone. 希望它可以帮助某人。

How do I ZIP a file in C#, using no 3rd-party APIs? 如何不使用任何第三方API在C#中压缩文件? covers the same ground and has solutions for both .Net 3.5 ( https://stackoverflow.com/a/940621/2381157 ) and .Net 4.5 ( https://stackoverflow.com/a/20200025/2381157 ) so I'm surprised that @kap states there is no .NET class for doing this. 涵盖了相同的领域,并为.Net 3.5( https://stackoverflow.com/a/940621/2381157 )和.Net 4.5( https://stackoverflow.com/a/20200025/2381157 )提供了解决方案@kap表示没有用于执行此操作的.NET类感到惊讶。

您可以使用第三方dll ICSharpCode.SharpZipLib,在其中可以使用以下加载文件将目录压缩到filsstream fs并继续字节= fs.Read(buffer,0,buffer.Length)zipFile.Write(buffer,0,bytes)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM