Thursday 12 December 2019

C#.NET - How to Create a Zip File using Compression Classes

The following code snippet takes the specified file (sourceFilePath) and creates Zip file.

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

public class FilesProcess
{
    public bool ConvertFileToZip(string sourceFilePath, string destinationZipFilePath)
    {
        bool isFileZipped = false;

        using (ZipArchive archive = ZipFile.Open(destinationZipFilePath, ZipArchiveMode.Create))
        {
            archive.CreateEntryFromFile(sourceFilePath, Path.GetFileName(sourceFilePath));
        }

        if (File.Exists(destinationZipFilePath))
        {
            isFileZipped = true;
        }
        return isFileZipped;
    }
}

No comments: