ReaderFactory.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System;
  2. using System.IO;
  3. using SharpCompress.Archives.GZip;
  4. using SharpCompress.Archives.Rar;
  5. using SharpCompress.Archives.Tar;
  6. using SharpCompress.Archives.Zip;
  7. using SharpCompress.Common;
  8. using SharpCompress.Compressors;
  9. using SharpCompress.Compressors.BZip2;
  10. using SharpCompress.Compressors.Deflate;
  11. using SharpCompress.IO;
  12. using SharpCompress.Readers.GZip;
  13. using SharpCompress.Readers.Rar;
  14. using SharpCompress.Readers.Tar;
  15. using SharpCompress.Readers.Zip;
  16. using SharpCompress.Compressors.LZMA;
  17. using SharpCompress.Compressors.Xz;
  18. namespace SharpCompress.Readers
  19. {
  20. public static class ReaderFactory
  21. {
  22. /// <summary>
  23. /// Opens a Reader for Non-seeking usage
  24. /// </summary>
  25. /// <param name="stream"></param>
  26. /// <param name="options"></param>
  27. /// <returns></returns>
  28. public static IReader Open(Stream stream, ReaderOptions options = null)
  29. {
  30. stream.CheckNotNull("stream");
  31. options = options ?? new ReaderOptions()
  32. {
  33. LeaveStreamOpen = false
  34. };
  35. RewindableStream rewindableStream = new RewindableStream(stream);
  36. rewindableStream.StartRecording();
  37. if (ZipArchive.IsZipFile(rewindableStream, options.Password))
  38. {
  39. rewindableStream.Rewind(true);
  40. return ZipReader.Open(rewindableStream, options);
  41. }
  42. rewindableStream.Rewind(false);
  43. if (GZipArchive.IsGZipFile(rewindableStream))
  44. {
  45. rewindableStream.Rewind(false);
  46. GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
  47. if (TarArchive.IsTarFile(testStream))
  48. {
  49. rewindableStream.Rewind(true);
  50. return new TarReader(rewindableStream, options, CompressionType.GZip);
  51. }
  52. rewindableStream.Rewind(true);
  53. return GZipReader.Open(rewindableStream, options);
  54. }
  55. rewindableStream.Rewind(false);
  56. if (BZip2Stream.IsBZip2(rewindableStream))
  57. {
  58. rewindableStream.Rewind(false);
  59. BZip2Stream testStream = new BZip2Stream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress, false);
  60. if (TarArchive.IsTarFile(testStream))
  61. {
  62. rewindableStream.Rewind(true);
  63. return new TarReader(rewindableStream, options, CompressionType.BZip2);
  64. }
  65. }
  66. rewindableStream.Rewind(false);
  67. if (LZipStream.IsLZipFile(rewindableStream))
  68. {
  69. rewindableStream.Rewind(false);
  70. LZipStream testStream = new LZipStream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress);
  71. if (TarArchive.IsTarFile(testStream))
  72. {
  73. rewindableStream.Rewind(true);
  74. return new TarReader(rewindableStream, options, CompressionType.LZip);
  75. }
  76. }
  77. rewindableStream.Rewind(false);
  78. if (RarArchive.IsRarFile(rewindableStream, options))
  79. {
  80. rewindableStream.Rewind(true);
  81. return RarReader.Open(rewindableStream, options);
  82. }
  83. rewindableStream.Rewind(false);
  84. if (TarArchive.IsTarFile(rewindableStream))
  85. {
  86. rewindableStream.Rewind(true);
  87. return TarReader.Open(rewindableStream, options);
  88. }
  89. rewindableStream.Rewind(false);
  90. if (XZStream.IsXZStream(rewindableStream))
  91. {
  92. rewindableStream.Rewind(true);
  93. XZStream testStream = new XZStream(rewindableStream);
  94. if (TarArchive.IsTarFile(testStream))
  95. {
  96. rewindableStream.Rewind(true);
  97. return new TarReader(rewindableStream, options, CompressionType.Xz);
  98. }
  99. }
  100. throw new InvalidOperationException("Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ");
  101. }
  102. }
  103. }