xxxxxxxxxx
/// <summary>
/// Create a zip file
/// </summary>
/// <param name="zipPath">Path of the files</param>
/// <param name="destinationPath">Distination Path</param>
/// <param name="zipFileName">Filename of the zip files</param>
public static void ZipFiles(string zipPath, string destinationPath, string zipFileName)
{
try
{
System.IO.Compression.ZipFile.CreateFromDirectory(zipPath, destinationPath + zipFileName);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
xxxxxxxxxx
using System.IO.Compression;
void CompressToZip(string inputPath, string outputPath, string password = null)
{
ZipFile.CreateFromDirectory(inputPath, outputPath, CompressionLevel.Optimal, false, null, password);
}
string inputPath = @"C:\MyFolder"; // replace with the path to the file or folder you want to compress
string outputPath = @"C:\MyArchive.zip"; // replace with the desired path and name for the output ZIP archive
string password = "mypassword"; // optional password to protect the archive
CompressToZip(inputPath, outputPath, password);
Console.WriteLine("File compressed successfully.");