Create archive from a directory

Product Version Product Author
2020.2.504 RadZipLibrary Tanya Dimitrova

Description

You need to create an archive from the files in a folder.

Solution

Use RadZipLibrary to create and export the archive.

class Program 
{ 
    static void Main(string[] args) 
    { 
        CreateZip(); 
    } 
 
    protected static void CreateZip() 
    { 
        using (FileStream stream = File.OpenWrite("example.zip")) 
        { 
            using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create, true, null)) 
            { 
                foreach (FileObject dataItem in GetData()) 
                { 
                    using (ZipArchiveEntry entry = archive.CreateEntry(dataItem.Name)) 
                    { 
                        BinaryWriter writer = new BinaryWriter(entry.Open()); 
                        writer.Write(dataItem.Data); 
                        writer.Flush(); 
                    } 
                                                                        } 
            } 
        } 
    } 
 
    private static List<FileObject> _files; 
    public static IList<FileObject> GetData() 
    { 
        _files = new List<FileObject>(); 
        foreach (var file in Directory.GetFiles("../../../TestFiles")) 
        { 
            var fileObj = new FileObject { Name = Path.GetFileName(file) }; 
 
            using (MemoryStream ms = new MemoryStream()) 
            using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read)) 
            { 
                byte[] bytes = new byte[fileStream.Length]; 
                fileStream.Read(bytes, 0, (int)fileStream.Length); 
                ms.Write(bytes, 0, (int)fileStream.Length); 
                fileObj.Data = ms.ToArray(); 
            } 
 
            _files.Add(fileObj); 
        } 
        return _files; 
    } 
} 
 
public class FileObject 
{ 
    public string Name { get; set; } 
    public byte[] Data { get; set; } 
}    
In this article
Not finding the help you need? Improve this article