Задача: Как работать с zip архивами стандартными средствами windows
Исходник: как открыть zip архив, язык: C# [code #601, hits: 13758]
автор: - [добавлен: 23.12.2009]
  1. // C# version 3.0
  2. using System;
  3. using System.IO;
  4. using System.Text;
  5. using Shell32; // Reference to C:\WINDOWS\system32\shell32.dll
  6.  
  7. public static class Zipper {
  8. public static void Compress(string zipFileName, params string[] fileNames) {
  9. //Create an empty zip file
  10. byte[] emptyzip = new byte[] {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  11. using (FileStream fileStream = File.Create(zipFileName)) {
  12. fileStream.Write(emptyzip, 0, emptyzip.Length);
  13. fileStream.Flush();
  14. fileStream.Close();
  15. }
  16. Add(zipFileName, fileNames);
  17. }
  18.  
  19. public static void Add(string zipFileName, params string[] fileNames) {
  20. Folder destFolder = new ShellClass().NameSpace(zipFileName);
  21. foreach (string filename in fileNames)
  22. destFolder.CopyHere(filename, 20);
  23. }
  24.  
  25. public static void Extract(string zipFileName, string targetPath) {
  26. Folder srcFolder = new ShellClass().NameSpace(zipFileName);
  27. Folder dstFolder = new ShellClass().NameSpace(targetPath);
  28. dstFolder.CopyHere(srcFolder.Items(), 20);
  29. }
  30. }
  31.  
  32.  
  33. // Zipper test
  34. internal class Program {
  35. private static void Main(string[] args) {
  36. FileInfo zipFileInfo = new FileInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".zip"));
  37. if (zipFileInfo.Exists)
  38. zipFileInfo.Delete();
  39. Zipper.Compress(zipFileInfo.FullName); // just create empty zip-file
  40. DirectoryInfo sourceDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
  41. if (!sourceDir.Exists)
  42. sourceDir.Create();
  43. for (int i = 0; i < 31; i++) {
  44. FileInfo fileInfo = new FileInfo(Path.Combine(sourceDir.FullName, Path.GetRandomFileName()));
  45. if (!fileInfo.Exists) {
  46. using (FileStream fileStream = fileInfo.Create()) {
  47. for (int j = 0; j < 15; j++) {
  48. byte[] bytes = Encoding.Default.GetBytes(fileInfo.FullName + Environment.NewLine);
  49. fileStream.Write(bytes, 0, bytes.Length);
  50. }
  51. fileStream.Close();
  52. }
  53. }
  54. Zipper.Add(zipFileInfo.FullName, fileInfo.FullName);
  55. }
  56. DirectoryInfo targetDir = new DirectoryInfo(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
  57. if (!targetDir.Exists)
  58. targetDir.Create();
  59. Zipper.Extract(zipFileInfo.FullName, targetDir.FullName);
  60.  
  61. sourceDir.Delete(true);
  62. targetDir.Delete(true);
  63. zipFileInfo.Delete();
  64. }
  65. }
подумал может кому пригодится пример, как работать с zip-архивами стандартными средствами windows.
(про Magic-Parameter 20 даже не спрашивайте.)

источник: rsdn.ru

+добавить реализацию