Contents

Compress files and folders using sevenzipsharp and 7-Zip in C-Sharp

Contents

Compressing files using 7-Zip and seven zip sharp (http://sevenzipsharp.codeplex.com/) is really easy. To get started you would need the 7-Zip DLL http://www.7-zip.org/download.html

namespace Cupofdev
{
    using System;
    using System.IO;
    using SevenZip;

    class Cupofdev
    {
        static void Main(string[] args)
        {  // Set source and target folders
            string targetFolder = @"E:\CodeDumps";
            string sourceCodeFolder = @"C:\Dev\Clients\cupofdev";
            if (System.IO.Directory.Exists(targetFolder))
            {
                // Specify where 7z.dll DLL is located 
                SevenZipCompressor.SetLibraryPath(@"C:\Program Files\7 - Zip\7z.dll");
                SevenZipCompressor sevenZipCompressor = new SevenZipCompressor();
                sevenZipCompressor.CompressionLevel = SevenZip.CompressionLevel.Ultra;
                sevenZipCompressor.CompressionMethod = CompressionMethod.Lzma;
                // Compress the directory and save the file in a yyyyMMdd_project-files.7z format (eg. 20141024_project-files.7z 
                sevenZipCompressor.CompressDirectory(sourceCodeFolder, Path.Combine(targetFolder, string.Concat(DateTime.Now.ToString("yyyyMMdd"), "_project-files.7z")));
            }
        }
    }
}