Using System.IO.Packaging to generate a ZIP file
Asked Answered
E

2

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?

Ere answered 17/6, 2011 at 13:1 Comment(1)
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. :-)Ere
S
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);
                    }
                }
            }
        }              
    }
}
Shilling answered 17/6, 2011 at 13:4 Comment(11)
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.Ere
Really you cannot implement it yourself ? :SShilling
Look - Listing files in a directory in C# csharpfriends.com/Articles/getArticle.aspx?articleID=356Shilling
you dont, all the code is in my reply and my comment above, just use it and for each file from DirectoryInfo use AddFileToZip ... :PShilling
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. :-)Ere
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.PackagingCasuistry
ironically, i googled exactly "create zip System.IO.Packaging" and it showed this thread first. you broke it.Halhalafian
me thinks that last line should read: bytesWritten += bytesRead;Cuckoo
@Cuckoo - or: both lines containing bytesWritten can be omittedFronniah
"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.Hindquarter
"Let me google this for you..." Seriously ? Please read this ! meta.stackexchange.com/questions/9953/… You answer is correct, thanks, but your behavior is horrible -> downvoteUranie
M
11

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

Movement answered 12/6, 2014 at 13:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.