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?
Using System.IO.Packaging to generate a ZIP file
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
let me google this for you -> system.io.packaging+generate+zip
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);
}
}
}
}
}
}
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 ? :S –
Shilling
Look - Listing files in a directory in C# csharpfriends.com/Articles/getArticle.aspx?articleID=356 –
Shilling
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 –
Shilling
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.Packaging –
Casuistry
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 omitted –
Fronniah
"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 -> downvote –
Uranie
In .NET Framework 4.5 you can use the new classes in the System.IO.Compression
namespace.
© 2022 - 2024 — McMap. All rights reserved.