For a more comprehensive and detailed explanation, you may also want to visit: https://www.code4it.dev/blog/working-with-zip-files/, written by Code4IT.
Official Documentation: https://learn.microsoft.com/en-us/dotnet/api/system.io.compression.zipfile
If you are doing this in .NET Framework 4.8, you need to add the assembly reference.
At the solution explore > right click your “project name” > “Add” > “Project Reference” > select both “System.IO.Compression
” and “System.IO.Compression.FileSystem
“.

If you are doing .NET or .NET Core, add the Nuget Package of “System.IO.Compression.ZipFile
“:
Nuget: https://www.nuget.org/packages/System.IO.Compression.ZipFile
The following class can do the following tasks:
- Zip a folder
- Zip a single file
- Zip a list of files
- Zip a file from bytes (byte array)
- Zip a file from memory stream
- Unzip everything to a folder
using System.Collections.Generic;
using System.IO.Compression;
using System.IO;
namespace System
{
public class Zip
{
// Zip a folder
public static void ZipFolder(string folderPath, string zipFilePath)
{
ZipFile.CreateFromDirectory(folderPath, zipFilePath);
}
// Zip a single file
public static void ZipSingleFile(string filePathath, string zipFilePath)
{
List<string> lst = new List<string>();
lst.Add(filePathath);
ZipListFile(lst, zipFilePath);
}
// Zip a list of files
public static void ZipListFile(List<string> lstFiles, string zipFilePath)
{
using (FileStream zipToOpen = new FileStream(zipFilePath, FileMode.Create))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
{
foreach (var file in lstFiles)
{
if (File.Exists(file))
{
ZipArchiveEntry zipEntry = archive.CreateEntry(Path.GetFileName(file));
using (FileStream originalFile = new FileStream(file, FileMode.Open, FileAccess.Read))
using (Stream entryStream = zipEntry.Open())
{
originalFile.CopyTo(entryStream);
}
}
}
}
}
}
// Zip file from byte array (bytes)
public static void ZipFileBytes(string filename, byte[] fileBytes, string zipFilePath)
{
using (FileStream zipToOpen = new FileStream(zipFilePath, FileMode.Create))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
{
ZipArchiveEntry zipEntry = archive.CreateEntry(filename);
using (Stream entryStream = zipEntry.Open())
{
entryStream.Write(fileBytes, 0, fileBytes.Length);
}
}
}
}
// Zip file from memory stream
public static void ZipFileMemoryStream(string filename, MemoryStream ms, string zipFilePath)
{
using (FileStream zipToOpen = new FileStream(zipFilePath, FileMode.Create))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
{
ZipArchiveEntry zipEntry = archive.CreateEntry(filename);
using (Stream entryStream = zipEntry.Open())
{
ms.CopyTo(entryStream);
}
}
}
}
// Unzip the everything in zip to a folder
public static void Upzip(string zipFilePath, string destinationFolder)
{
ZipFile.ExtractToDirectory(zipFilePath, destinationFolder);
}
}
}
Example of usage:
string folder = @"C:\mywebsite\media\";
string zipPath = @"C:\mywebsite\files.zip";
string file = @"C:\mywebsite\content.txt";
string zipPath2 = @"C:\mywebsite\content.zip";
// to zip a folder
Zip.ZipFolder(folder, zipPath);
// to zip a file
Zip.ZipSingleFile(file, zipPath2);
// to unzip (extract files) to a folder
Zip.Unzip(zipPath, @"C:\mywebsite\files\");