How to: Archive Selected Files
- 2 minutes to read
To archive selected files create a ZipArchive class instance and call its AddFile method for each selected file.
To store a file without its path, call the ZipArchive.AddFile method with the “/“ parameter. The file is placed in the root node of the archive. Call the ZipArchive.Save method. The archive is created and saved to a specified location.
This code snippet creates a new archive and add files with .txt and .docx extensions to the archive root. The ZipArchive.Save method compresses data and saves the archive to a file.
using DevExpress.Compression;
public void ArchiveFiles() {
// In this example, this.SourceFiles returns
// file names from the Documents folder
// in the working directory
string[] sourcefiles = this.sourceFiles;
string[] ext = new string[] {".txt", ".docx"};
using (ZipArchive archive = new ZipArchive()) {
foreach (string file in sourcefiles) {
if (ext.Contains(Path.GetExtension(file)))
archive.AddFile(file, "/");
}
archive.Save("Documents\\ArchiveFiles.zip");
}
}