Create ZIP

ZIP is a file compression and archiving format used to reduce file sizes for efficient storage and transmission. It combines multiple files and directories into a single archive, often named with a '.zip' extension. ZIP archives are widely used for data backup, software distribution, and file sharing.

To create a ZIP file in C#, use the using statement to instantiate a ZipArchive object from the System.IO.Compression namespace. Inside the using block, invoke the CreateEntryFromFile method to add files to the ZIP file. The file path can specify a directory or file that you want to add to the ZIP archive. Finally, output the ZIP file using the ZipArchive functionality.

using System;
using System.IO;
using System.IO.Compression;

class ZipFileExample
{
    static void Main()
    {
        // Specify the directory or file paths you want to compress
        string directoryPath = @"C:\example\directory";
        string zipPath = @"C:\example\output.zip";

        // Create and open a ZIP file to archive the specified directory
        using (FileStream zipFileStream = new FileStream(zipPath, FileMode.Create))
        {
            using (ZipArchive archive = new ZipArchive(zipFileStream, ZipArchiveMode.Create))
            {
                // Add each file in the directory to the ZIP archive
                foreach (string filePath in Directory.GetFiles(directoryPath))
                {
                    // Get the file name from the full file path
                    string fileName = Path.GetFileName(filePath);

                    // Create an entry for each file
                    archive.CreateEntryFromFile(filePath, fileName);
                }
            }
        }

        Console.WriteLine("Files have been successfully compressed into a ZIP archive.");
    }
}
using System;
using System.IO;
using System.IO.Compression;

class ZipFileExample
{
    static void Main()
    {
        // Specify the directory or file paths you want to compress
        string directoryPath = @"C:\example\directory";
        string zipPath = @"C:\example\output.zip";

        // Create and open a ZIP file to archive the specified directory
        using (FileStream zipFileStream = new FileStream(zipPath, FileMode.Create))
        {
            using (ZipArchive archive = new ZipArchive(zipFileStream, ZipArchiveMode.Create))
            {
                // Add each file in the directory to the ZIP archive
                foreach (string filePath in Directory.GetFiles(directoryPath))
                {
                    // Get the file name from the full file path
                    string fileName = Path.GetFileName(filePath);

                    // Create an entry for each file
                    archive.CreateEntryFromFile(filePath, fileName);
                }
            }
        }

        Console.WriteLine("Files have been successfully compressed into a ZIP archive.");
    }
}
$vbLabelText   $csharpLabel

Code Explanation:

  • Namespace Usage: The code uses System.IO for handling file and directory paths, and System.IO.Compression to create zip archives.
  • Initialization: Define the directory path containing files to be archived and the zip file path.
  • Creating ZIP Archive:
    • A FileStream is opened for the zip file in Create mode ensuring a new file is created.
    • A ZipArchive is created using this file stream in Create mode.
  • Adding Files:
    • Loop through each file in the specified directory using Directory.GetFiles().
    • For each file, extract its name using Path.GetFileName().
    • Use archive.CreateEntryFromFile() to add the file to the archive, providing the file path and its name.
  • Completion Message: Outputs a message upon successful creation of the ZIP file.

Ensure you have proper permissions to access the directories/files specified, and remember that any existing file at zipPath will be overwritten due to the FileMode.Create option.