Create zip file from all files in folder

Referencing System.IO.Compression and System.IO.Compression.FileSystem in your Project

using System.IO.Compression;

string startPath = @"c:\example\start";//folder to add
string zipPath = @"c:\example\result.zip";//URL for your ZIP file
ZipFile.CreateFromDirectory(startPath, zipPath, CompressionLevel.Fastest, true);
string extractPath = @"c:\example\extract";//path to extract
ZipFile.ExtractToDirectory(zipPath, extractPath);

To use files only, use:

//Creates a new, blank zip file to work with - the file will be
//finalized when the using statement completes
using (ZipArchive newFile = ZipFile.Open(zipName, ZipArchiveMode.Create))
{
    foreach (string file in Directory.GetFiles(myPath))
    {
        newFile.CreateEntryFromFile(file, System.IO.Path.GetFileName(file));
    }              
}

Referencing System.IO.Compression and System.IO.Compression.FileSystem in your Project, your code can be something like:

string startPath = @"some path";
string zipPath = @"some other path";
var files = Directory.GetFiles(startPath);

using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Open))
{
    using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create))
    {
        foreach (var file in files)
        {
            archive.CreateEntryFromFile(file, file);
        }
    }
}

In some folders though you may have problems with permissions.

Tags:

C#

Zip

Zipfile