StreamingZipFilePart.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System.IO;
  2. using SharpCompress.Common.Zip.Headers;
  3. using SharpCompress.Compressors.Deflate;
  4. using SharpCompress.IO;
  5. namespace SharpCompress.Common.Zip
  6. {
  7. internal class StreamingZipFilePart : ZipFilePart
  8. {
  9. private Stream _decompressionStream;
  10. internal StreamingZipFilePart(ZipFileEntry header, Stream stream)
  11. : base(header, stream)
  12. {
  13. }
  14. protected override Stream CreateBaseStream()
  15. {
  16. return Header.PackedStream;
  17. }
  18. internal override Stream GetCompressedStream()
  19. {
  20. if (!Header.HasData)
  21. {
  22. return Stream.Null;
  23. }
  24. _decompressionStream = CreateDecompressionStream(GetCryptoStream(CreateBaseStream()), Header.CompressionMethod);
  25. if (LeaveStreamOpen)
  26. {
  27. return new NonDisposingStream(_decompressionStream);
  28. }
  29. return _decompressionStream;
  30. }
  31. internal BinaryReader FixStreamedFileLocation(ref RewindableStream rewindableStream)
  32. {
  33. if (Header.IsDirectory)
  34. {
  35. return new BinaryReader(rewindableStream);
  36. }
  37. if (Header.HasData && !Skipped)
  38. {
  39. if (_decompressionStream == null)
  40. {
  41. _decompressionStream = GetCompressedStream();
  42. }
  43. _decompressionStream.Skip();
  44. DeflateStream deflateStream = _decompressionStream as DeflateStream;
  45. if (deflateStream != null)
  46. {
  47. rewindableStream.Rewind(deflateStream.InputBuffer);
  48. }
  49. Skipped = true;
  50. }
  51. var reader = new BinaryReader(rewindableStream);
  52. _decompressionStream = null;
  53. return reader;
  54. }
  55. }
  56. }