In previous article, I have mentioned to Convert List to Dictionary in C# (Various ways), now in this article, I have mentioned how we can zip-unzip files using C# in .NET Framework 4.5 or .NET Core using Console application examples.
Zip-Unzip using C# (.NET 4.5+ Framework )
Let's take a look at .NET Framework example to zip-unzip files in C#.
We will be using System.IO.Compression;
namespace with ZipFile.CreateFromDirectory
method to create Zip from directory
using System;
using System.IO.Compression;
using System.IO;
using System.Text.RegularExpressions;
namespace ZipUnzipNETOld
{
internal class Program
{
static void Main(string[] args)
{
var directoryPath = new DirectoryInfo(@"D:\SampleFiles\");
Compress(directoryPath);
Decompress(directoryPath);
}
public static void Compress(DirectoryInfo directoryPath)
{
//this will loop thrhough all folders of path and create zip of each folder
foreach (DirectoryInfo directory in directoryPath.GetDirectories())
{
var path = directoryPath.FullName;
var newArchiveName = Regex.Replace(directory.Name, "[0-9]{8}", "20130913");
newArchiveName = Regex.Replace(newArchiveName, "[_]+", "_");
string startPath = path + directory.Name;
string zipPath = path + "" + newArchiveName + ".zip";
//if path exists, delete it to generate new zip files
if (File.Exists(zipPath))
File.Delete(zipPath);
ZipFile.CreateFromDirectory(startPath, zipPath);
}
}
public static void Decompress(DirectoryInfo directoryPath)
{
//get all files
foreach (FileInfo file in directoryPath.GetFiles())
{
var path = directoryPath.FullName;
//get zip file, if exists
string zipPath = path + file.Name;
string extractPath = Regex.Replace(path + file.Name, ".zip", "");
//if path exists, delete it to generate new extracted files
if (Directory.Exists(extractPath))
Directory.Delete(extractPath, true);
//if zip exists extract it
if (File.Exists(zipPath))
ZipFile.ExtractToDirectory(zipPath, extractPath);
}
}
}
}
Output:
Zip unzip using C# (.NET Core)
Now let's take a look on .NET Core based solution, I am using .NET 5 in this example to zip-unzip files.
We are simply using ZipFile method of System.IO.Compression, to simply Zip and unzip files in C#
using System;
using System.IO;
using System.IO.Compression;
namespace ZipUnZipFile
{
internal class Program
{
static void Main(string[] args)
{
ZipFiles();
UnZipFiles();
Console.WriteLine("Done");
}
private static void UnZipFiles()
{
ZipFile.ExtractToDirectory(
@"D:\\SampleFilesZipped.zip",
@"D:\\SampleFilesZipped",
overwriteFiles: true
);
Console.WriteLine("Files Uncompressed");
}
public static void ZipFiles()
{
//get all files, you can filter it, if you want
var allFiles = Directory.GetFiles(@"D:\SampleFiles");
//delete zip if already exists
if (File.Exists("D:\\SampleFilesZipped.zip"))
File.Delete("D:\\SampleFilesZipped.zip");
//create zip file
using var archive =
ZipFile
.Open(@"D:\SampleFilesZipped.zip", ZipArchiveMode.Create);
//loop through all files in directory
foreach (var file in allFiles)
{
//add file in zip archive
var entry =
archive.CreateEntryFromFile(
file,
Path.GetFileName(file),
CompressionLevel.Optimal
);
Console.WriteLine($"{entry.FullName} was compressed.");
}
}
}
}
If you will see above method, of ZipFiles() we are simply getting all files from folder "D:\SampleFiles" and then looping them to add it in Zip file "D:\SampleFilesZipped.zip"
And in UnZipFiles() method, we are simply using "ExtractToDirectory" method and giving source zip file name and destination folder.
Here is the console output, with folder output, which you can see in the image.
As you can see in the above image, initially, a zip file is created and then unzipped.
You may also like to read:
How to get month name from month number in C#?
Creating C# Custom Exception (With Console application example)
Convert List to Dictionary in C# (Various ways)