Chuyển tới nội dung
Trang chủ » C# Create Zip File From Multiple Files Top 18 Latest Posts

C# Create Zip File From Multiple Files Top 18 Latest Posts

  • bởi

You are looking for information on the topic “c# create zip file from multiple files”. will gather information to answer your questions in this article. Please read this article to the end to find the information you want to see. You can also check out some other related articles here: .

View information about the c# create zip file from multiple files

To create a zip file from multiple files in C#, you can use the System.IO.Compression namespace, specifically the ZipFile class. Here is some sample code that demonstrates how to create a zip file from multiple files:

In this code, the CreateZipFile method takes two parameters: the path of the zip file to be created (zipFilePath) and an array of source file paths (sourceFilePaths) to be added to the zip file. The method creates a new ZipArchive object using the ZipFile.Open method, with the ZipArchiveMode.Create mode specified to create a new zip archive. It then loops through each source file path in the sourceFilePaths array, creates a new ZipArchiveEntry for the file using the zipArchive.CreateEntry method, and copies the contents of the source file to the zip archive entry using the CopyTo method of the source file stream.

Finally, the ZipArchive object is closed by calling its Dispose method in a finally block, to ensure that the zip file is properly closed even if an exception occurs during the creation process.

Information about this question

  • Question: c# create zip file from multiple files
  • Time: Updated 13 days ago
  • Modified: Asked 86 minute ago
  • View: 10384


We use .zip files all of the time and Windows users have come to expect this functionality to be present in many applications. There are numerous third-party products available that we can use to create .zip archives and these include WinZip, PkZip, WinRar and others. It is generally understood that we can create a .zip archive from numerous files or directories on a hard drive and that the resulting zip  archive is a single “file” with a .zip extension that contains the files and folders that we include. Zip files are good for storing and/or transferring files/folders for the following reasons:

  1. Since multiple files or folders can be “zipped” into a single file, we can simplify things by storing or transferring a single file and not many, many files. This is great for e-mail attachments, web-based file uploads/downloads and just simpler for both the packager and the recipient.
  2. Because zip files implement varying levels of compression, the size of the single zip file is less than the cumulative sizes of the files included in the zip file.

Creating a Zip File (Archive) via Windows 7

The Windows operating system provides .zip file functionality within My Computer. This is easily accomplished by selecting a folder or files, right-clicking, and selecting SendTo and Compressed (zipped) folder. See the illustration below.

I’m sure that we’ve all done this at one time or another, right? Sure we have. Now, let’s take look at how we can create and work with the built-in Windows zip file/archive functionality provided by Windows via C# and the .NET Framework.

Creating and Working with Zip Files in C#

In .NET 4.5, there are classes in the System.IO.Compression namespace that allow developers to quickly and easily create and work with zip files and archives. These classes are listed below:

  • ZipFile – provides static methods for creating, extracting, and opening zip files.
  • ZipArchive – represents a package of compressed files in a zip format. Allows us to work with a collection of compressed files in a robust manner.

So, let’s just get going with some sample code!

Creating a Zip File – System.IO.Compression.ZipFile Class

The ZipFile class provides static methods that allow developers to programmatically work with zip archives. To use these methods, we have to first reference the System.IO.Compression.FileSystem assembly. The following little walkthrough will allow us to use the methods of the ZipFile class to work with a simple zip archive. Let’s create a new C# Console Application and call it ZipFileConsoleApplication. Once we’ve done that, let’s Add a Reference to the System.IO.Compression.FileSystem assembly. See the Add Reference dialog below.

Works like a champ and even better, it’s really simple to implement.

The ZipFile.CreateFromDirectory() method has a few overloads that are worth mentioning. The first one that we looked at was the simplest of the three available. The second overload allows us to also specify a CompressionLevel value. This enum has three members and they are listed below:

  • NoCompression – this option specifies that no compression should be applied to the file.
  • Fastest – this option results in an operation that takes the least amount of time, but the total level of compression is less.
  • Optimal – this option results in an operation that provides the greatest level of compression, but in cases where there is a large number of files or subfolders, the compression operation will take more time.

This warrants some discussion. First, let’s recognize that when talking about compression, we are constantly forced to balance between the level of compression and the time required to produce the output. This is a tradeoff that is just inherent to the process. The CompressionLevel that you specify must be carefully chosen based on your scenario. An application that provides zip archive functionality could either allow the user to specify the compression level or it could dynamically specify it based on rules that you implement within the application. For example, your logic could assess the number of files, the disparity of file sizes (i.e. how many and how large), evaluate available disk space, and many other variables. Based on these “rules” that you would write, you could determine on the fly what compression strategy to use.

It is important to note here that the CompressionLevel enum does not give us control over how our files are compressed. There are extension methods in the ZipFileExtensions classes that allow us to do this if we so desire. I won’t go into too much detail about that here, but we do need to remember that our scenario should dictate which CompressionLevel we implement. Okay?

Now, armed with our knowledge of the additional overloads to the CreateFromDirectory() method and the existence of the CompressionLevel enum, let’s do three quick tests.

First, modify the last line of code (see sample code above) to utilize the second overload of the CreateFromDirectory() method, and let’s pick NoCompression for our CompressionLevel value. See below.

c# create zip file from multiple files

To create a zip file from multiple files in C#, you can use the System.IO.Compression namespace. Here’s an example code snippet:

using System.IO;
using System.IO.Compression;

string[] filesToZip = { "file1.txt", "file2.jpg", "file3.docx" };
string zipFilePath = "";

// Create the zip archive
using (ZipArchive zip = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
// Add each file to the zip archive
foreach (string file in filesToZip)
zip.CreateEntryFromFile(file, Path.GetFileName(file));

In this example, we first define an array filesToZip which contains the paths of the files we want to add to the zip archive. We also define a zipFilePath which is the path where we want to save the zip archive.

We then create a new zip archive using ZipFile.Open method, passing in the zipFilePath and ZipArchiveMode.Create. This creates a new zip file or overwrites an existing one.

Next, we loop through each file in filesToZip, and add it to the zip archive using CreateEntryFromFile method. This method takes two arguments – the path of the file to add, and the name to use for the file inside the zip archive (we use Path.GetFileName to get just the file name without the directory path).

Finally, we close the zip archive using a using block, which ensures that the archive is properly closed and any resources are released.

Watch videos related to “c# create zip file from multiple files”

മനുഷ്യൻ ‍ധാ‍ർമ്മിക ജീവിയോ? | Islam Vs Atheism Debate | Ravichandran C | T Muhammad Velam | Live

Images related to c# create zip file from multiple files

Found 25 c# create zip file from multiple files related images.

You can see some more information related to c# create zip file from multiple files here


There are a total of 399 comments on this question.

  • 735 comments are great
  • 548 great comments
  • 136 normal comments
  • 74 bad comments
  • 18 very bad comments

So you have finished reading the article on the topic c# create zip file from multiple files. If you found this article useful, please share it with others. Thank you very much.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *