New to Telerik Document Processing? Download free 30-day trial

Create archive from a list of files

Product Version Product Author
2020.1.219 RadZipLibrary Dimitar Karamfilov

Description

You need to create an archive from a list of files.

Solution

Use RadZipLibrary to create and export the archive.

List<string> fileNames = new List<string>(); 
 
fileNames.Add("text.txt"); 
fileNames.Add("text1.txt"); 
fileNames.Add("text2.txt"); 
 
string zipFileName = "Result.zip"; 
 
using (Stream stream = File.Open(zipFileName, FileMode.Create)) 
{ 
    using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, leaveOpen: true, entryNameEncoding: null)) 
    { 
        foreach (var file in fileNames) 
        { 
            using (ZipArchiveEntry entry = archive.CreateEntry(file)) 
            { 
                var entryStream = entry.Open(); 
                FileStream fs = new FileStream(file, FileMode.Open); 
                fs.CopyTo(entryStream); 
                entryStream.Flush(); 
 
                fs.Close(); 
                fs.Dispose(); 
            } 
        } 
    } 
} 
In this article