ZipFile.x-IEnumerable.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // ZipFile.x-IEnumerable.cs
  2. // ------------------------------------------------------------------
  3. //
  4. // Copyright (c) 2006, 2007, 2008, 2009 Dino Chiesa and Microsoft Corporation.
  5. // All rights reserved.
  6. //
  7. // This code module is part of DotNetZip, a zipfile class library.
  8. //
  9. // ------------------------------------------------------------------
  10. //
  11. // This code is licensed under the Microsoft Public License.
  12. // See the file License.txt for the license details.
  13. // More info on: http://dotnetzip.codeplex.com
  14. //
  15. // ------------------------------------------------------------------
  16. //
  17. // last saved (in emacs):
  18. // Time-stamp: <2009-December-26 15:13:26>
  19. //
  20. // ------------------------------------------------------------------
  21. //
  22. // This module defines smoe methods for IEnumerable support. It is
  23. // particularly important for COM to have these things in a separate module.
  24. //
  25. // ------------------------------------------------------------------
  26. namespace Ionic.Zip
  27. {
  28. // For some weird reason, the method with the DispId(-4) attribute, which is used as
  29. // the _NewEnum() method, and which is required to get enumeration to work from COM
  30. // environments like VBScript and Javascript (etc) must be the LAST MEMBER in the
  31. // source. In the event of Partial classes, it needs to be the last member defined
  32. // in the last source module. The source modules are ordered alphabetically by
  33. // filename. Not sure why this is true. In any case, we put the enumeration stuff
  34. // here in this oddly-named module, for this reason.
  35. //
  36. public partial class ZipFile
  37. {
  38. /// <summary>
  39. /// Generic IEnumerator support, for use of a ZipFile in an enumeration.
  40. /// </summary>
  41. ///
  42. /// <remarks>
  43. /// You probably do not want to call <c>GetEnumerator</c> explicitly. Instead
  44. /// it is implicitly called when you use a <see langword="foreach"/> loop in C#, or a
  45. /// <c>For Each</c> loop in VB.NET.
  46. /// </remarks>
  47. ///
  48. /// <example>
  49. /// This example reads a zipfile of a given name, then enumerates the
  50. /// entries in that zip file, and displays the information about each
  51. /// entry on the Console.
  52. /// <code>
  53. /// using (ZipFile zip = ZipFile.Read(zipfile))
  54. /// {
  55. /// bool header = true;
  56. /// foreach (ZipEntry e in zip)
  57. /// {
  58. /// if (header)
  59. /// {
  60. /// System.Console.WriteLine("Zipfile: {0}", zip.Name);
  61. /// System.Console.WriteLine("Version Needed: 0x{0:X2}", e.VersionNeeded);
  62. /// System.Console.WriteLine("BitField: 0x{0:X2}", e.BitField);
  63. /// System.Console.WriteLine("Compression Method: 0x{0:X2}", e.CompressionMethod);
  64. /// System.Console.WriteLine("\n{1,-22} {2,-6} {3,4} {4,-8} {0}",
  65. /// "Filename", "Modified", "Size", "Ratio", "Packed");
  66. /// System.Console.WriteLine(new System.String('-', 72));
  67. /// header = false;
  68. /// }
  69. ///
  70. /// System.Console.WriteLine("{1,-22} {2,-6} {3,4:F0}% {4,-8} {0}",
  71. /// e.FileName,
  72. /// e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"),
  73. /// e.UncompressedSize,
  74. /// e.CompressionRatio,
  75. /// e.CompressedSize);
  76. ///
  77. /// e.Extract();
  78. /// }
  79. /// }
  80. /// </code>
  81. ///
  82. /// <code lang="VB">
  83. /// Dim ZipFileToExtract As String = "c:\foo.zip"
  84. /// Using zip As ZipFile = ZipFile.Read(ZipFileToExtract)
  85. /// Dim header As Boolean = True
  86. /// Dim e As ZipEntry
  87. /// For Each e In zip
  88. /// If header Then
  89. /// Console.WriteLine("Zipfile: {0}", zip.Name)
  90. /// Console.WriteLine("Version Needed: 0x{0:X2}", e.VersionNeeded)
  91. /// Console.WriteLine("BitField: 0x{0:X2}", e.BitField)
  92. /// Console.WriteLine("Compression Method: 0x{0:X2}", e.CompressionMethod)
  93. /// Console.WriteLine(ChrW(10) &amp; "{1,-22} {2,-6} {3,4} {4,-8} {0}", _
  94. /// "Filename", "Modified", "Size", "Ratio", "Packed" )
  95. /// Console.WriteLine(New String("-"c, 72))
  96. /// header = False
  97. /// End If
  98. /// Console.WriteLine("{1,-22} {2,-6} {3,4:F0}% {4,-8} {0}", _
  99. /// e.FileName, _
  100. /// e.LastModified.ToString("yyyy-MM-dd HH:mm:ss"), _
  101. /// e.UncompressedSize, _
  102. /// e.CompressionRatio, _
  103. /// e.CompressedSize )
  104. /// e.Extract
  105. /// Next
  106. /// End Using
  107. /// </code>
  108. /// </example>
  109. ///
  110. /// <returns>A generic enumerator suitable for use within a foreach loop.</returns>
  111. public System.Collections.Generic.IEnumerator<ZipEntry> GetEnumerator()
  112. {
  113. foreach (ZipEntry e in _entries.Values)
  114. yield return e;
  115. }
  116. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  117. {
  118. return GetEnumerator();
  119. }
  120. /// <summary>
  121. /// An IEnumerator, for use of a ZipFile in a foreach construct.
  122. /// </summary>
  123. ///
  124. /// <remarks>
  125. /// This method is included for COM support. An application generally does not call
  126. /// this method directly. It is called implicitly by COM clients when enumerating
  127. /// the entries in the ZipFile instance. In VBScript, this is done with a <c>For Each</c>
  128. /// statement. In Javascript, this is done with <c>new Enumerator(zipfile)</c>.
  129. /// </remarks>
  130. ///
  131. /// <returns>
  132. /// The IEnumerator over the entries in the ZipFile.
  133. /// </returns>
  134. [System.Runtime.InteropServices.DispId(-4)]
  135. public System.Collections.IEnumerator GetNewEnum() // the name of this method is not significant
  136. {
  137. return GetEnumerator();
  138. }
  139. }
  140. }