How to compress/decompress directories using GZipStream
Note this code will not unzip general zip files. It only works with files zipped according to the package standard.To unzip regular zip files, there are several components available.I've used the Windows built-in COM component "Microsoft Shell Controls And Automation" (Add Reference, COM...
Note this code will not unzip general zip files. It only works with files zipped according to the package standard.
To unzip regular zip files, there are several components available.
I've used the Windows built-in COM component "Microsoft Shell Controls And Automation" (Add Reference, COM tab).
The code to unzip using this component is:
using Shell32;
static void Unzip(string zipFile, string directory) {
ShellClass sc = new ShellClass();
Folder source = sc.NameSpace(zipFile);
Folder target = sc.NameSpace(directory);
FolderItems items = source.Items();
const int options = (int)(CopyOptions.NoProgressDialog | CopyOptions.YesToAll);
target.CopyHere(items, options);
}
Where the enum
values are:
[Flags]
internal enum CopyOptions {
/// <summary>Do not display a progress dialog box.</summary>
NoProgressDialog = 4,
/// <summary>Rename the target file if a file exists at the target location with the same name.</summary>
RenameDuplicates = 8,
/// <summary>Click "Yes to All" in any dialog box displayed.</summary>
YesToAll = 16,
/// <summary>Preserve undo information, if possible.</summary>
PreserveUndo = 64,
/// <summary>Perform the operation only if a wildcard file name (*.*) is specified.</summary>
AcceptWildcardOnly = 128,
/// <summary>Display a progress dialog box but do not show the file names.</summary>
ProgressDialogWithoutFilenames = 256,
/// <summary>Do not confirm the creation of a new directory if the operation requires one to be created.</summary>
CreateDirectoryWithoutDialog = 512,
/// <summary>Do not display a user interface if an error occurs.</summary>
NoDialogOnError = 1024,
/// <summary>Disable recursion.</summary>
NoRecursion = 4096,
/// <summary>Do not copy connected files as a group. Only copy the specified files.</summary>
OnlySpecifiedFiles = 8192
}
Cheers,
Michel