How to compress multiple files in a zip file

I am trying to compress two text files into a zip file. This is what my public method looks like:

public ActionResult Index() { byte[] file1 = System.IO.File.ReadAllBytes(@"C:\file1.txt"); byte[] file2 = System.IO.File.ReadAllBytes(@"C:\file2.txt"); Dictionary<string, byte[]> fileList = new Dictionary<string, byte[]>(); fileList.Add("file1.txt", file1); fileList.Add("file2.txt", file2); CompressToZip("zip.zip", fileList); return View(); } 

Here's what my compression method looks like:

 private void CompressToZip(string fileName, Dictionary<string, byte[]> fileList) { using (var memoryStream = new MemoryStream()) { foreach (var file in fileList) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { var demoFile = archive.CreateEntry(file.Key); using (var entryStream = demoFile.Open()) using (var b = new BinaryWriter(entryStream)) { b.Write(file.Value); } } } using (var fileStream = new FileStream(fileName, FileMode.Create)) { memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.CopyTo(fileStream); } } } 

In this approach, the zip folder is perfectly created. However, the problem is that I get only one file inside the zip folder (only the second file will be created inside the zip folder). No errors were found.

Question: How to compress both text files to a zip folder?

Thank you for the advanced!

+5
source share
2 answers

Your code actually saves two separate zip archives into a zip.zip file (a new ZipArchive is created for each compressed file). The first zip archive contains only file1.txt , the second only file2.txt . When you open zip.zip in Windows Explorer, it only shows the contents of the second zip archive.

To create one ZIP archive containing both files, simply move the ZipArchive creation outside of your fileList loop:

 using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { foreach (var file in fileList) { var demoFile = archive.CreateEntry(file.Key); using (var entryStream = demoFile.Open()) using (var b = new BinaryWriter(entryStream)) { b.Write(file.Value); } } } 
+8
source

At first glance, I would suggest that your foreach around using var (archive = new ZipArchive...) is the wrong way round.

This way you create a new ZipArchive every time you repeat the foreach .

Do you really want to create a ZipArchive and execute a loop through foreach inside this?

Like this:

 private void CompressToZip(string fileName, Dictionary<string, byte[]> fileList) { using (var memoryStream = new MemoryStream()) { using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) { foreach (var file in fileList) { var demoFile = archive.CreateEntry(file.Key); using (var entryStream = demoFile.Open()) using (var b = new BinaryWriter(entryStream)) { b.Write(file.Value); } } } using (var fileStream = new FileStream(fileName, FileMode.Create)) { memoryStream.Seek(0, SeekOrigin.Begin); memoryStream.CopyTo(fileStream); } } } 

Hope this helps!

+5
source

All Articles