-
Notifications
You must be signed in to change notification settings - Fork 980
FastZip
This page shows how to create and extract zip files using the FastZip class.
FastZip fastZip = new FastZip();
bool recurse = true; // Include all files by recursing through the directory structure
string filter = null; // Dont filter any files at all
fastZip.CreateZip("fileName.zip", @"C:\SourceDirectory", recurse, filter);
If the file filter is not a null string, it is interpreted as a regular expression which is tested against each file name.
For example, a fileFilter value of 'txt' would match with 'Test.txt' but would also match with 'SometxtFile.doc'. To match with '.txt' you would need to add a a dot, but even then a filter of ".txt" would be interpreted as [any-character]txt. You need to add an escape character "" before the dot to specify it as a literal match. However, in my tests doing a single \ didn't seem to work, I needed to add a second .
Note that ".txt" would still match a file such as "file.txtold" or "file.txt.old", so you can append the $ specifier which matches with the end of the string: e.g ".txt$" to ensure you only match files with the last extension as .txt
FastZip fastZip = new FastZip();
bool recurse = true; // Include all files by recursing through the directory structure
string filter = @"\\.txt$"; // Only files ending in ".txt"
fastZip.CreateZip("fileName.zip", @"C:\SourceDirectory", recurse, filter);
You can set events which will fire on each file. This gives you the opportunity to display a progress notification, and to decide whether to include each file.
using System;
using System.IO;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
private int _uptoFileCount;
private int _totalFileCount;
public void TestFastZipCreate(string backupFolderPath) {
_totalFileCount = FolderContentsCount(backupFolderPath);
FastZipEvents events = new FastZipEvents();
events.ProcessFile = ProcessFileMethod;
FastZip fastZip = new FastZip(events);
fastZip.CreateEmptyDirectories = true;
string zipFileName = Directory.GetParent(backupFolderPath).FullName + "\\ZipTest.zip";
fastZip.CreateZip(zipFileName, backupFolderPath, true, "");
}
private void ProcessFileMethod(object sender, ScanEventArgs args) {
_uptoFileCount ++;
int percentCompleted = _uptoFileCount * 100 / _totalFileCount;
// do something here with a progress bar
// file counts are easier as sizes take more work to calculate, and compression levels vary by file type
string fileName = args.Name;
// To skip this file, set args.ContinueRunning = false
// Only include files ending in ".txt"
if (!fileName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase))
args.ContinueRunning = false;
}
// Returns the number of files in this and all subdirectories
private int FolderContentsCount(string path) {
int result = Directory.GetFiles(path).Length;
string[ ] subFolders = Directory.GetDirectories(path);
foreach (string subFolder in subFolders) {
result += FolderContentsCount(subFolder);
}
return result;
}
public void TestFastZipUnpack(string zipFileName, string targetDir) {
FastZip fastZip = new FastZip();
string fileFilter = null;
// Will always overwrite if target filenames already exist
fastZip.ExtractZip(zipFileName, targetDir, fileFilter);
}