How to Create Zip Archives in C# Without 3rd Party LibrariesHow to create ZIP archives with .Net C# without relying on any 3rd party libraries or nuget packages, using 100% native code.
This article will show you how to create ZIP archives with .Net C# without relying on any 3rd party libraries or nuget packages, using 100% native code.
This example uses the System.IO.Compression
namespace which provides basic compression and decompression services.
Create ZIP Archives of Entire Folder
This example will send the entire contents of a folder to a ZIP archive.
using System.IO.Compression;
var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var sourceFolder = Path.Combine(desktopFolder, @"My Files");
var targetZip = Path.Combine(desktopFolder, @"MyFiles.zip");
System.IO.Compression.ZipFile.CreateFromDirectory(sourceFolder, targetZip);
Create ZIP Archives From a List of Files
This example will get a list of all the folders in the directory and add them to a new zip file. You can get the list of files from any means you require including a static list, I'm just using the GetFiles as an example.
using System.IO.Compression;
var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var sourceFiles = Path.Combine(desktopFolder, "My Files");
var allfiles = Directory.GetFiles(sourceFiles, "*.*", SearchOption.AllDirectories);
var targetZipFile = Path.Combine(desktopFolder, @"MyFilesBackup.zip");
using (var fileStream = new FileStream(targetZipFile, FileMode.CreateNew))
using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Create, true))
{
foreach (var file in allfiles)
{
var info = Path.GetFileName(file);
archive.CreateEntryFromFile(file, info);
}
}
Add Files to Existing ZIP Archive
You can also open an existing zip archive and add additional files to it. Note that the only difference from the above code is the FileStream FileMode.Open
and the ZipArchiveMode.Update
.
using System.IO.Compression;
var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var sourceFiles = Path.Combine(desktopFolder, "My Additional Files");
var allfiles = Directory.GetFiles(sourceFiles, "*.*", SearchOption.AllDirectories);
var targetZipFile = Path.Combine(desktopFolder, @"MyFilesBackup.zip");
using (var fileStream = new FileStream(targetZipFile, FileMode.Open))
using (var archive = new ZipArchive(fileStream, ZipArchiveMode.Update, true))
{
foreach (var file in allfiles)
{
var info = Path.GetFileName(file);
archive.CreateEntryFromFile(file, info);
}
}
Extracting ZIP Contents
This short code snippet will extract the contents of a ZIP file to the specified destination folder.
using System.IO.Compression;
var desktopFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var zippedFile = Path.Combine(desktopFolder, @"MyFilesBackup.zip");
var destinationFolder = Path.Combine(desktopFolder, @"My Files Unzipped");
System.IO.Compression.ZipFile.ExtractToDirectory(zippedFile, destinationFolder);
Check if a ZIP archive is Valid
You can use this function to test if a ZIP file is valid or invalid. This can be useful for verifying a backup for example, but should not be relied on to verify zip file contents or integrity.
public bool IsValidZipFile(string filename)
{
try
{
using (var zip = ZipFile.OpenRead(filename))
{
return zip != null;
}
}
catch (Exception)
{
return false;
}
}
ZIP Archive Passwords and Encryption Unfortunately at the time of writing Microsoft did not implement zip passwords or encryption into the create zip archives functions of System.IO.Compression tools, for this you will need a third party extension or package. Hopefully, Microsoft will release an update with passwords and encryption built in.
Unfortunately at the time of writing Microsoft did not implement zip passwords or encryption into the create zip archives functions of System.IO.Compression tools, for this you will need a third party extension or package. Hopefully, Microsoft will release an update with passwords and encryption built in.