TarReader.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using SharpCompress.Archives.GZip;
  5. using SharpCompress.Archives.Tar;
  6. using SharpCompress.Common;
  7. using SharpCompress.Common.Tar;
  8. using SharpCompress.Compressors;
  9. using SharpCompress.Compressors.BZip2;
  10. using SharpCompress.Compressors.Deflate;
  11. using SharpCompress.IO;
  12. using SharpCompress.Compressors.LZMA;
  13. using SharpCompress.Compressors.Xz;
  14. namespace SharpCompress.Readers.Tar
  15. {
  16. public class TarReader : AbstractReader<TarEntry, TarVolume>
  17. {
  18. private readonly CompressionType compressionType;
  19. internal TarReader(Stream stream, ReaderOptions options, CompressionType compressionType)
  20. : base(options, ArchiveType.Tar)
  21. {
  22. this.compressionType = compressionType;
  23. Volume = new TarVolume(stream, options);
  24. }
  25. public override TarVolume Volume { get; }
  26. protected override Stream RequestInitialStream()
  27. {
  28. var stream = base.RequestInitialStream();
  29. switch (compressionType)
  30. {
  31. case CompressionType.BZip2:
  32. {
  33. return new BZip2Stream(stream, CompressionMode.Decompress, false);
  34. }
  35. case CompressionType.GZip:
  36. {
  37. return new GZipStream(stream, CompressionMode.Decompress);
  38. }
  39. case CompressionType.LZip:
  40. {
  41. return new LZipStream(stream, CompressionMode.Decompress);
  42. }
  43. case CompressionType.Xz:
  44. {
  45. return new XZStream(stream);
  46. }
  47. case CompressionType.None:
  48. {
  49. return stream;
  50. }
  51. default:
  52. {
  53. throw new NotSupportedException("Invalid compression type: " + compressionType);
  54. }
  55. }
  56. }
  57. #region Open
  58. /// <summary>
  59. /// Opens a TarReader for Non-seeking usage with a single volume
  60. /// </summary>
  61. /// <param name="stream"></param>
  62. /// <param name="options"></param>
  63. /// <returns></returns>
  64. public static TarReader Open(Stream stream, ReaderOptions options = null)
  65. {
  66. stream.CheckNotNull("stream");
  67. options = options ?? new ReaderOptions();
  68. RewindableStream rewindableStream = new RewindableStream(stream);
  69. rewindableStream.StartRecording();
  70. if (GZipArchive.IsGZipFile(rewindableStream))
  71. {
  72. rewindableStream.Rewind(false);
  73. GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
  74. if (TarArchive.IsTarFile(testStream))
  75. {
  76. rewindableStream.Rewind(true);
  77. return new TarReader(rewindableStream, options, CompressionType.GZip);
  78. }
  79. throw new InvalidFormatException("Not a tar file.");
  80. }
  81. rewindableStream.Rewind(false);
  82. if (BZip2Stream.IsBZip2(rewindableStream))
  83. {
  84. rewindableStream.Rewind(false);
  85. BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false);
  86. if (TarArchive.IsTarFile(testStream))
  87. {
  88. rewindableStream.Rewind(true);
  89. return new TarReader(rewindableStream, options, CompressionType.BZip2);
  90. }
  91. throw new InvalidFormatException("Not a tar file.");
  92. }
  93. rewindableStream.Rewind(false);
  94. if (LZipStream.IsLZipFile(rewindableStream))
  95. {
  96. rewindableStream.Rewind(false);
  97. LZipStream testStream = new LZipStream(rewindableStream, CompressionMode.Decompress);
  98. if (TarArchive.IsTarFile(testStream))
  99. {
  100. rewindableStream.Rewind(true);
  101. return new TarReader(rewindableStream, options, CompressionType.LZip);
  102. }
  103. throw new InvalidFormatException("Not a tar file.");
  104. }
  105. rewindableStream.Rewind(true);
  106. return new TarReader(rewindableStream, options, CompressionType.None);
  107. }
  108. #endregion Open
  109. protected override IEnumerable<TarEntry> GetEntries(Stream stream)
  110. {
  111. return TarEntry.GetEntries(StreamingMode.Streaming, stream, compressionType, Options.ArchiveEncoding);
  112. }
  113. }
  114. }