RarArchive.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using SharpCompress.Common;
  5. using SharpCompress.Common.Rar;
  6. using SharpCompress.Common.Rar.Headers;
  7. using SharpCompress.Compressors.Rar;
  8. using SharpCompress.Readers;
  9. using SharpCompress.Readers.Rar;
  10. namespace SharpCompress.Archives.Rar
  11. {
  12. public class RarArchive : AbstractArchive<RarArchiveEntry, RarVolume>
  13. {
  14. internal Lazy<IRarUnpack> UnpackV2017 { get; } = new Lazy<IRarUnpack>(() => new SharpCompress.Compressors.Rar.UnpackV2017.Unpack());
  15. internal Lazy<IRarUnpack> UnpackV1 { get; } = new Lazy<IRarUnpack>(() => new SharpCompress.Compressors.Rar.UnpackV1.Unpack());
  16. #if !NO_FILE
  17. /// <summary>
  18. /// Constructor with a FileInfo object to an existing file.
  19. /// </summary>
  20. /// <param name="fileInfo"></param>
  21. /// <param name="options"></param>
  22. internal RarArchive(FileInfo fileInfo, ReaderOptions options)
  23. : base(ArchiveType.Rar, fileInfo, options)
  24. {
  25. }
  26. protected override IEnumerable<RarVolume> LoadVolumes(FileInfo file)
  27. {
  28. return RarArchiveVolumeFactory.GetParts(file, ReaderOptions);
  29. }
  30. #endif
  31. /// <summary>
  32. /// Takes multiple seekable Streams for a multi-part archive
  33. /// </summary>
  34. /// <param name="streams"></param>
  35. /// <param name="options"></param>
  36. internal RarArchive(IEnumerable<Stream> streams, ReaderOptions options)
  37. : base(ArchiveType.Rar, streams, options)
  38. {
  39. }
  40. protected override IEnumerable<RarArchiveEntry> LoadEntries(IEnumerable<RarVolume> volumes)
  41. {
  42. return RarArchiveEntryFactory.GetEntries(this, volumes);
  43. }
  44. protected override IEnumerable<RarVolume> LoadVolumes(IEnumerable<Stream> streams)
  45. {
  46. return RarArchiveVolumeFactory.GetParts(streams, ReaderOptions);
  47. }
  48. protected override IReader CreateReaderForSolidExtraction()
  49. {
  50. var stream = Volumes.First().Stream;
  51. stream.Position = 0;
  52. return RarReader.Open(stream, ReaderOptions);
  53. }
  54. public override bool IsSolid => Volumes.First().IsSolidArchive;
  55. #region Creation
  56. #if !NO_FILE
  57. /// <summary>
  58. /// Constructor with a FileInfo object to an existing file.
  59. /// </summary>
  60. /// <param name="filePath"></param>
  61. /// <param name="options"></param>
  62. public static RarArchive Open(string filePath, ReaderOptions options = null)
  63. {
  64. filePath.CheckNotNullOrEmpty("filePath");
  65. return new RarArchive(new FileInfo(filePath), options ?? new ReaderOptions());
  66. }
  67. /// <summary>
  68. /// Constructor with a FileInfo object to an existing file.
  69. /// </summary>
  70. /// <param name="fileInfo"></param>
  71. /// <param name="options"></param>
  72. public static RarArchive Open(FileInfo fileInfo, ReaderOptions options = null)
  73. {
  74. fileInfo.CheckNotNull("fileInfo");
  75. return new RarArchive(fileInfo, options ?? new ReaderOptions());
  76. }
  77. #endif
  78. /// <summary>
  79. /// Takes a seekable Stream as a source
  80. /// </summary>
  81. /// <param name="stream"></param>
  82. /// <param name="options"></param>
  83. public static RarArchive Open(Stream stream, ReaderOptions options = null)
  84. {
  85. stream.CheckNotNull("stream");
  86. return Open(stream.AsEnumerable(), options ?? new ReaderOptions());
  87. }
  88. /// <summary>
  89. /// Takes multiple seekable Streams for a multi-part archive
  90. /// </summary>
  91. /// <param name="streams"></param>
  92. /// <param name="options"></param>
  93. public static RarArchive Open(IEnumerable<Stream> streams, ReaderOptions options = null)
  94. {
  95. streams.CheckNotNull("streams");
  96. return new RarArchive(streams, options ?? new ReaderOptions());
  97. }
  98. #if !NO_FILE
  99. public static bool IsRarFile(string filePath)
  100. {
  101. return IsRarFile(new FileInfo(filePath));
  102. }
  103. public static bool IsRarFile(FileInfo fileInfo)
  104. {
  105. if (!fileInfo.Exists)
  106. {
  107. return false;
  108. }
  109. using (Stream stream = fileInfo.OpenRead())
  110. {
  111. return IsRarFile(stream);
  112. }
  113. }
  114. #endif
  115. public static bool IsRarFile(Stream stream, ReaderOptions options = null)
  116. {
  117. try
  118. {
  119. MarkHeader.Read(stream, true, false);
  120. return true;
  121. }
  122. catch
  123. {
  124. return false;
  125. }
  126. }
  127. #endregion
  128. }
  129. }