Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

How to compress/decompress directories using GZipStream

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
4 Feb 2012CPOL 8.7K   2  
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:
C#
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:
C#
[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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Independer
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --