ZipReader.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using SharpCompress.Common;
  4. using SharpCompress.Common.Zip;
  5. using SharpCompress.Common.Zip.Headers;
  6. namespace SharpCompress.Readers.Zip
  7. {
  8. public class ZipReader : AbstractReader<ZipEntry, ZipVolume>
  9. {
  10. private readonly StreamingZipHeaderFactory _headerFactory;
  11. private ZipReader(Stream stream, ReaderOptions options)
  12. : base(options, ArchiveType.Zip)
  13. {
  14. Volume = new ZipVolume(stream, options);
  15. _headerFactory = new StreamingZipHeaderFactory(options.Password, options.ArchiveEncoding);
  16. }
  17. public override ZipVolume Volume { get; }
  18. #region Open
  19. /// <summary>
  20. /// Opens a ZipReader for Non-seeking usage with a single volume
  21. /// </summary>
  22. /// <param name="stream"></param>
  23. /// <param name="options"></param>
  24. /// <returns></returns>
  25. public static ZipReader Open(Stream stream, ReaderOptions options = null)
  26. {
  27. stream.CheckNotNull("stream");
  28. return new ZipReader(stream, options ?? new ReaderOptions());
  29. }
  30. #endregion Open
  31. protected override IEnumerable<ZipEntry> GetEntries(Stream stream)
  32. {
  33. foreach (ZipHeader h in _headerFactory.ReadStreamHeader(stream))
  34. {
  35. if (h != null)
  36. {
  37. switch (h.ZipHeaderType)
  38. {
  39. case ZipHeaderType.LocalEntry:
  40. {
  41. yield return new ZipEntry(new StreamingZipFilePart(h as LocalEntryHeader,
  42. stream));
  43. }
  44. break;
  45. case ZipHeaderType.DirectoryEnd:
  46. {
  47. yield break;
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }