简体   繁体   中英

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. I've had a look around ( How to zip a folder ), ( http://dotnetzip.codeplex.com/ ) but no luck so far. I'm a bit apprehensive of using dotnetzip as it's last release was 5 years ago.

Is dotnetzip still relevant in Visual Studio 2015 or is there a more modern way of zipping folders in C# without using a package?

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 :

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. No 3-rd party libs required. You simply need to reference the System.IO.Compression.FileSystem

What I wanted to point out is: You can either zip the entire RootFolder including it to the archive

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

or the contents of the RootFolder without the folder itself

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

The fourth parameter (bool includeBaseDirectory) gives us this flexibility . Hope it helps someone.

How do I ZIP a file in C#, using no 3rd-party APIs? 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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