12

I know that the likes of the DotNetZip or SharpZipLib libraries are usually recommended for creating ZIP files in a .net language (C# in my case), but it's not impossible to use System.IO.Packaging to generate a ZIP file. I thought it might be nice to try and develop a routine in C# which could do it, without the need to download any external libraries. Does anyone have a good example of a method or methods that will use System.IO.Packaging to generate a ZIP file?

Jez
  • 27,951
  • 32
  • 136
  • 233
  • OK, after reading around a bit, it looks like the awkwardness of using System.IO.Packaging isn't the only reason people avoid it like the plague; it also generates a silly [Content_Types].xml file in every zip in generates, and there are serious question marks as to its compatibility with other zip file clients. So... I guess I'll be using DotNetZip. :-) – Jez Jun 17 '11 at 14:05

2 Answers2

34

let me google this for you -> system.io.packaging+generate+zip

first link http://weblogs.asp.net/jongalloway//creating-zip-archives-in-net-without-an-external-library-like-sharpziplib

using System;
using System.IO;
using System.IO.Packaging;

namespace ZipSample
{
    class Program
    {
        static void Main(string[] args)
        {
            AddFileToZip("Output.zip", @"C:\Windows\Notepad.exe");
            AddFileToZip("Output.zip", @"C:\Windows\System32\Calc.exe");
        }

        private static void AddFileToZip(string zipFilename, string fileToAdd, CompressionOption compression = CompressionOption.Normal)
        {
            using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate))
            {
                string destFilename = ".\\" + Path.GetFileName(fileToAdd);
                Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
                if (zip.PartExists(uri))
                {
                    zip.DeletePart(uri);
                }
                PackagePart part = zip.CreatePart(uri, "", compression);
                using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read))
                {
                    using (Stream dest = part.GetStream())
                    {
                        fileStream.CopyTo(dest);
                    }
                }
            }
        }              
    }
}
Tomas Kubes
  • 23,880
  • 18
  • 111
  • 148
RamonBoza
  • 8,898
  • 6
  • 36
  • 48
  • Sure, but this doesn't take a path, and add all its directories and files into the ZIP; one must specify the individual files manually. – Jez Jun 17 '11 at 13:27
  • 9
    Really you cannot implement it yourself ? :S – RamonBoza Jun 17 '11 at 13:31
  • Look - Listing files in a directory in C# http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=356 – RamonBoza Jun 17 '11 at 13:32
  • you dont, all the code is in my reply and my comment above, just use it and for each file from DirectoryInfo use AddFileToZip ... :P – RamonBoza Jun 17 '11 at 13:37
  • OK, after reading around a bit, it looks like the awkwardness of using `System.IO.Packaging` isn't the only reason people avoid it like the plague; it also generates a silly `[Content_Types].xml` file in every zip in generates, and there are serious question marks as to its compatibility with other zip file clients. So... I guess I'll be using DotNetZip. :-) – Jez Jun 17 '11 at 14:05
  • 5
    Nice, thanks for this.Just want to mention that to use this code you must add a reference to WindowsBase dll to be able to use System.IO.Packaging – Denys Wessels Jun 28 '12 at 06:46
  • 9
    ironically, i googled exactly "create zip System.IO.Packaging" and it showed this thread first. you broke it. – user381624 Mar 21 '13 at 23:44
  • 1
    me thinks that last line should read: bytesWritten += bytesRead; – erict Nov 09 '13 at 19:34
  • @erict - or: both lines containing bytesWritten can be omitted – miroxlav Oct 01 '14 at 23:51
  • 5
    *"let me google this for you..."* Wow, could you be more condescending if you tried? The way this answer is phrased is only one or two steps above a [LMGTFY link, which Stack Overflow frowns upon](https://meta.stackoverflow.com/questions/255397/lmgtfy-link-cant-be-added). – Knowledge Cube May 16 '17 at 13:24
  • 2
    "Let me google this for you..." Seriously ? Please read this ! https://meta.stackexchange.com/questions/9953/could-we-please-be-a-bit-nicer-to-new-users You answer is correct, thanks, but your behavior is horrible -> downvote – Elo Jul 04 '18 at 08:50
11

In .NET Framework 4.5 you can use the new classes in the System.IO.Compression namespace.

g t
  • 7,287
  • 7
  • 50
  • 85
Heiko Scholze
  • 123
  • 1
  • 5