Create ZIP Archives with .Net C# Without 3rd Party Libraries Packages

Published August 7, 2022 by .

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.

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 to it. Note that 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 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

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 integrety.

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

Create ZIP Archives with .Net C# Without 3rd Party Libraries Packages
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 fucntions 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.

Comments

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?

We respect your privacy, and will not make your email public. Hashed email address may be checked against Gravatar service to retrieve avatars. This site uses Akismet to reduce spam. Learn how your comment data is processed.