3

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.

Community
  • 1
  • 1
CBreeze
  • 2,925
  • 4
  • 38
  • 93
  • _"no luck so far"_ isn't really descriptive. Show your attempts and their results. – CodeCaster Jan 13 '16 at 11:18
  • 2
    There's [ZipFile.CreateFromDirectory](https://msdn.microsoft.com/en-us/library/hh485707%28v=vs.110%29.aspx) in .Net 4.5 – stuartd Jan 13 '16 at 11:20
  • Look into [Compression](https://msdn.microsoft.com/en-us/library/system.io.compression(v=vs.110).aspx) with the specific topic of [ZipArchive](https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive(v=vs.110).aspx) class. – Christoph K Jan 13 '16 at 11:25
  • @stuartd Thanks for the answer. I've just tried using this but I'm getting an error: `The file already exists`. As far as I can see it needs a file to exist so that it can zip it? – CBreeze Jan 13 '16 at 11:32
  • I believe from the documentation that that error indicates that the **zip** file already exists.. - "If the archive already exists, an IOException exception is thrown ... "`destinationArchiveFileName` already exists"" – stuartd Jan 13 '16 at 11:33
  • @stuartd rather strange because it definitely doesn't. Thanks for the help I'll keep investigating. If you offer your comment as an answer I'll accept. – CBreeze Jan 13 '16 at 11:35
  • @CBreeze done, thanks – stuartd Jan 13 '16 at 11:37

4 Answers4

7

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);
stuartd
  • 70,509
  • 14
  • 132
  • 163
3

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.

Alex X.
  • 453
  • 5
  • 11
0

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.

Community
  • 1
  • 1
christutty
  • 952
  • 5
  • 12
  • Why the down vote? The links to the duplicate question contain the same info as @stuartd provides and that's the accepted answer. – christutty Jan 14 '16 at 02:28
-1

You can use third party dll ICSharpCode.SharpZipLib inside which you can compress directory using below load file to filsstream fs and proceed bytes = fs.Read(buffer, 0, buffer.Length)zipFile.Write(buffer, 0, bytes)