How to Create Zip Archives in C# Without 3rd Party Libraries

How to create ZIP archives with .Net C# without relying on any 3rd party libraries or nuget packages, using 100% native code.

By Tim TrottC# ASP.Net MVC • August 7, 2022
How to Create Zip Archives in C# Without 3rd Party Libraries

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 all the folder contents to a ZIP archive.

C#
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.

C#
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. The only difference from the above code is the FileStream FileMode.Open and the ZipArchiveMode.Update.

C#
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 and send it to the specified destination folder.

C#
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

This function can test whether a ZIP file is valid or invalid. For example, this can be useful for verifying a backup but should not be relied on to verify zip file contents or integrity.

C#
public bool IsValidZipFile(string filename)
{
    try
    {
        using (var zip = ZipFile.OpenRead(filename))
        {
            return zip != null;
        }
    }
    catch (Exception)
    {
        return false;
    }
}

ZIP Archive Passwords and Encryption

A stack of messy files and paperwork
Create ZIP Archives with .Net C# Without 3rd Party Libraries Packages

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. Microsoft will release an update with built-in passwords and encryption.

About the Author

Tim Trott is a senior software engineer with over 20 years of experience in designing, building, and maintaining software systems across a range of industries. Passionate about clean code, scalable architecture, and continuous learning, he specialises in creating robust solutions that solve real-world problems. He is currently based in Edinburgh, where he develops innovative software and collaborates with teams around the globe.

Related ArticlesThese articles may also be of interest to you

CommentsShare your thoughts in the comments below

My website and its content are free to use without the clutter of adverts, popups, marketing messages or anything else like that. If you enjoyed reading this article, or it helped you in some way, all I ask in return is you leave a comment below or share this page with your friends. Thank you.

There are no comments yet. Why not get the discussion started?

New comments for this post are currently closed.