More .NET libraries
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Easy-to-use API
Creating a ZIP archive
To create a ZIP archive or add files into existing one, use the static ZipArchive.Add
method.
// add a file to the root of the ZIP archive // (creates the ZIP archive if it doesn't exist) ZipArchive.Add(@"C:\MyData\archive.zip", @"C:\MyData\file1.txt", "/");
To perform more operations using the same ZIP archive, use an instance of ZipArchive
class.
// create new ZIP archive // if the ZIP archive already exists, it will be overwritten using (var zip = new ZipArchive(@"C:\MyData\archive.zip", ArchiveOpenMode.Create)) { // add a file to ZIP archive zip.AddFile(@"C:\MyData\file1.txt"); // add whole directory to ZIP archive zip.Add(@"C:\MyData\Web\*"); }
Extracting a ZIP archive
To extract files from a ZIP archive, use ZipArchive.Extract
or ZipArchive.ExtractAll
methods.
// decompress all files from ZIP archive to "C:\MyData\Out" directory ZipArchive.ExtractAll(@"C:\MyData\archive.zip", @"C:\MyData\Out");
To perform more operations using the same ZIP archive, use an instance of the ZipArchive
class.
// open a ZIP archive using (var zip = new ZipArchive(@"C:\MyData\archive.zip")) { // extract "file1.txt" as "extracted.txt" zip.ExtractFile("file1.txt", @"C:\MyData\Out\extracted.txt"); // extract all ".html" files zip.Extract("*.html", @"C:\MyData\Out"); // or simply extract all items from the ZIP archive // (replace all existing files) zip.ExtractAll(@"C:\MyData\Out", ActionOnExistingFiles.OverwriteAll); }
Displaying a progress bar
To display current progress, register the ProgressChanged
event:
// WinForm initializations // ... var fileProgressBar = new System.Windows.Forms.ProgressBar(); var totalProgressBar = new System.Windows.Forms.ProgressBar(); var bytesProcessedLabel = new System.Windows.Forms.Label(); // ... // create ZipArchive instance // ... // register progress event handler zip.ProgressChanged += (s, e) => { // update ProgressBar of the whole compression/decompression totalProgressBar.Value = (int)e.ProgressPercentage; // update ProgressBar of the current file fileProgressBar.Value = (int)e.CurrentFileProgressPercentage; // update Label of total processed bytes bytesProcessedLabel.Text = e.BytesProcessed.ToString(); }; // add, extract or delete files // ...
To learn more about the ProgressChanged
event, check out
Progress reporting.
Back to feature list...