I like to zip multiple files which are being created dynamically in my web application. Those files should be zipped. For this, i dont want to use any third-party tools. just like to use .net api in c#
8 Answers
With the release of the .NET Framework 4.5 this is actually a lot easier now with the updates to System.IO.Compression which adds the ZipFile class. There is a good walk-through on codeguru; however, the basics are in line with the following example:
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.IO.Compression.FileSystem;
namespace ZipFileCreator
{
public static class ZipFileCreator
{
/// <summary>
/// Create a ZIP file of the files provided.
/// </summary>
/// <param name="fileName">The full path and name to store the ZIP file at.</param>
/// <param name="files">The list of files to be added.</param>
public static void CreateZipFile(string fileName, IEnumerable<string> files)
{
// Create and open a new ZIP file
var zip = ZipFile.Open(fileName, ZipArchiveMode.Create);
foreach (var file in files)
{
// Add the entry for each file
zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
}
// Dispose of the object when we are done
zip.Dispose();
}
}
}

- 14,236
- 12
- 79
- 119
-
2How do i do this when getting the files (blob) data from the database? – Seth May 04 '15 at 16:43
-
@Seth you would probably need to do some I/O. Deserialize the blob back to bytes, write the bytes to file, then pass the full file path into this method as one of the files in `IEnumerable
files` – Kris Coleman Sep 21 '17 at 17:53 -
@Seth Put this inside the `foreach` `var entry = zip.CreateEntry(file), CompressionLevel.Optimal); using (Stream dbStream = getStreamFromDB(file)) using (Stream entryStream = entry.Open()) { dbStream.CopyToAsync(entryStream); }` – sirdank Mar 07 '19 at 20:31
-
This will fail if all of the files are not in the same folder. – Mason Wheeler Nov 20 '21 at 18:47
-
@MasonWheeler It's also an example in an answer that's almost seven years old. ¯\_(ツ)_/¯ – rjzii Nov 20 '21 at 20:59
Use System.IO.Packaging in .NET 3.0+.
See this introduction to System.IO.Packaging
If you're able to take a .NET 4.5 dependency, there's a System.IO.Compression.ZipArchive in that universe; see walkthrough article here (via InfoQ news summary article here)

- 1,497
- 2
- 15
- 20

- 59,778
- 26
- 187
- 249
Simple zip file with flat structure:
using System.IO;
using System.IO.Compression;
private static void CreateZipFile(IEnumerable<FileInfo> files, string archiveName)
{
using (var stream = File.OpenWrite(archiveName))
using (ZipArchive archive = new ZipArchive(stream, System.IO.Compression.ZipArchiveMode.Create))
{
foreach (var item in files)
{
archive.CreateEntryFromFile(item.FullName, item.Name, CompressionLevel.Optimal);
}
}
}
You need to add reference to System.IO.Compression and System.IO.Compression.FileSystem

- 23,880
- 18
- 111
- 148
I'm not sure what you mean by not wanting to use thrid party tools, but I assume its that you don't want some nasty interop to programmatically do it through another piece of software.
I recommend using ICSharpCode SharpZipLib
This can be added to your project as a reference DLL and is fairly straightforward for creating ZIP files and reading them.

- 27,303
- 5
- 81
- 107

- 45,739
- 9
- 81
- 112
-
1SharpZipLib is good (+1) but you can do it natively in .Net now - see @Ruben Bartelink's answer – Keith Aug 07 '09 at 10:11
-
-
He didnt exclude .NET 3, and its hardly as big as forcing a move from .NET 1.1 to 2.0 - Though SharpZipLib is a fine library that's heavily used (inc by me) – Ruben Bartelink Aug 07 '09 at 10:17
-
@Ruben - obviously yours is a highly valid answer (+1), but moving from 2.0 to 3.0+ isn't just about the technical changes, there's all the bureaucracy involved with licensing etc. – cjk Aug 07 '09 at 10:35
-
@ck: Not sure what licensing you're referring to, but surely you dont want to be living without System.Core :P (My main point was that the questioner didnt specifically rule out .NET 3.0, but made noises in the direction of wanting to use only MS stuff) – Ruben Bartelink Aug 07 '09 at 10:40
-
@Ruben - for licensing I mean moving to Visual Studio 2008 in all our development environments. – cjk Aug 07 '09 at 11:20
-
@ck - .NET 3.0 is just a framework update, you don't need VS2008 to exploit its goodness. – Kev Aug 07 '09 at 11:31
-
@Kev - my dev machine is patched up to 3.5, how do I get to use the 3.0 changes in VS 2005? – cjk Aug 07 '09 at 14:55
-
@ck - follow Eric Moreau's steps in Ruben's link. All you need to do is add a reference to the WindowsBase assembly. Works just fine in VS2005. – Kev Aug 11 '09 at 11:39
-
Well you can zip the files using following function you have to just pass the file bytes and this function will zip the file bytes passed as parameter and return the zipped file bytes.
public static byte[] PackageDocsAsZip(byte[] fileBytesTobeZipped, string packageFileName)
{
try
{
string parentSourceLoc2Zip = @"C:\UploadedDocs\SG-ACA OCI Packages";
if (Directory.Exists(parentSourceLoc2Zip) == false)
{
Directory.CreateDirectory(parentSourceLoc2Zip);
}
//if destination folder already exists then delete it
string sourceLoc2Zip = string.Format(@"{0}\{1}", parentSourceLoc2Zip, packageFileName);
if (Directory.Exists(sourceLoc2Zip) == true)
{
Directory.Delete(sourceLoc2Zip, true);
}
Directory.CreateDirectory(sourceLoc2Zip);
FilePath = string.Format(@"{0}\{1}",
sourceLoc2Zip,
"filename.extension");//e-g report.xlsx , report.docx according to exported file
File.WriteAllBytes(FilePath, fileBytesTobeZipped);
//if zip already exists then delete it
if (File.Exists(sourceLoc2Zip + ".zip"))
{
File.Delete(sourceLoc2Zip + ".zip");
}
//now zip the source location
ZipFile.CreateFromDirectory(sourceLoc2Zip, sourceLoc2Zip + ".zip", System.IO.Compression.CompressionLevel.Optimal, true);
return File.ReadAllBytes(sourceLoc2Zip + ".zip");
}
catch
{
throw;
}
}
Now if you want to export this zip bytes created for user to download you can call this function using following lines
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=Report.zip");
Response.ContentType = "application/zip";
Response.BinaryWrite(PackageDocsAsZip(fileBytesToBeExported ,"TemporaryFolderName"));
Response.End();

- 18,195
- 11
- 58
- 78

- 410
- 4
- 15
DotNetZip is the way to go (dotnetzip.codeplex.com)... don't try the .NET Packaging library.. too hard to use and the [Content_Types].xml that it puts in there bothers me..

- 125
- 1
- 5
Check out System.IO.Compression.DeflateStream. The linked documentation page also offers an example.
-
4
-
true, but you can still compress multiple files together for sending. Sarathi asked for something inbuilt in .net. And thats one thing that .net offers – Marcom Aug 07 '09 at 10:11
You could always call a third-party executable like 7-zip with an appropriate command line using the System.Diagnostics.Process class. There's no interop that way because you're just asking the OS to launch a binary.

- 2,384
- 2
- 19
- 14