RarArchiveVolumeFactory.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using SharpCompress.Common.Rar;
  5. using SharpCompress.Readers;
  6. #if !NO_FILE
  7. using System.Linq;
  8. using System.Text;
  9. using SharpCompress.Common.Rar.Headers;
  10. #endif
  11. namespace SharpCompress.Archives.Rar
  12. {
  13. internal static class RarArchiveVolumeFactory
  14. {
  15. internal static IEnumerable<RarVolume> GetParts(IEnumerable<Stream> streams, ReaderOptions options)
  16. {
  17. foreach (Stream s in streams)
  18. {
  19. if (!s.CanRead || !s.CanSeek)
  20. {
  21. throw new ArgumentException("Stream is not readable and seekable");
  22. }
  23. StreamRarArchiveVolume part = new StreamRarArchiveVolume(s, options);
  24. yield return part;
  25. }
  26. }
  27. #if !NO_FILE
  28. internal static IEnumerable<RarVolume> GetParts(FileInfo fileInfo, ReaderOptions options)
  29. {
  30. FileInfoRarArchiveVolume part = new FileInfoRarArchiveVolume(fileInfo, options);
  31. yield return part;
  32. ArchiveHeader ah = part.ArchiveHeader;
  33. if (!ah.IsVolume)
  34. {
  35. yield break; //if file isn't volume then there is no reason to look
  36. }
  37. fileInfo = GetNextFileInfo(ah, part.FileParts.FirstOrDefault() as FileInfoRarFilePart);
  38. //we use fileinfo because rar is dumb and looks at file names rather than archive info for another volume
  39. while (fileInfo != null && fileInfo.Exists)
  40. {
  41. part = new FileInfoRarArchiveVolume(fileInfo, options);
  42. fileInfo = GetNextFileInfo(ah, part.FileParts.FirstOrDefault() as FileInfoRarFilePart);
  43. yield return part;
  44. }
  45. }
  46. private static FileInfo GetNextFileInfo(ArchiveHeader ah, FileInfoRarFilePart currentFilePart)
  47. {
  48. if (currentFilePart == null)
  49. {
  50. return null;
  51. }
  52. bool oldNumbering = ah.OldNumberingFormat
  53. || currentFilePart.MarkHeader.OldNumberingFormat;
  54. if (oldNumbering)
  55. {
  56. return FindNextFileWithOldNumbering(currentFilePart.FileInfo);
  57. }
  58. else
  59. {
  60. return FindNextFileWithNewNumbering(currentFilePart.FileInfo);
  61. }
  62. }
  63. private static FileInfo FindNextFileWithOldNumbering(FileInfo currentFileInfo)
  64. {
  65. // .rar, .r00, .r01, ...
  66. string extension = currentFileInfo.Extension;
  67. StringBuilder buffer = new StringBuilder(currentFileInfo.FullName.Length);
  68. buffer.Append(currentFileInfo.FullName.Substring(0,
  69. currentFileInfo.FullName.Length - extension.Length));
  70. if (string.Compare(extension, ".rar", StringComparison.OrdinalIgnoreCase) == 0)
  71. {
  72. buffer.Append(".r00");
  73. }
  74. else
  75. {
  76. int num = 0;
  77. if (int.TryParse(extension.Substring(2, 2), out num))
  78. {
  79. num++;
  80. buffer.Append(".r");
  81. if (num < 10)
  82. {
  83. buffer.Append('0');
  84. }
  85. buffer.Append(num);
  86. }
  87. else
  88. {
  89. ThrowInvalidFileName(currentFileInfo);
  90. }
  91. }
  92. return new FileInfo(buffer.ToString());
  93. }
  94. private static FileInfo FindNextFileWithNewNumbering(FileInfo currentFileInfo)
  95. {
  96. // part1.rar, part2.rar, ...
  97. string extension = currentFileInfo.Extension;
  98. if (string.Compare(extension, ".rar", StringComparison.OrdinalIgnoreCase) != 0)
  99. {
  100. throw new ArgumentException("Invalid extension, expected 'rar': " + currentFileInfo.FullName);
  101. }
  102. int startIndex = currentFileInfo.FullName.LastIndexOf(".part");
  103. if (startIndex < 0)
  104. {
  105. ThrowInvalidFileName(currentFileInfo);
  106. }
  107. StringBuilder buffer = new StringBuilder(currentFileInfo.FullName.Length);
  108. buffer.Append(currentFileInfo.FullName, 0, startIndex);
  109. int num = 0;
  110. string numString = currentFileInfo.FullName.Substring(startIndex + 5,
  111. currentFileInfo.FullName.IndexOf('.', startIndex + 5) -
  112. startIndex - 5);
  113. buffer.Append(".part");
  114. if (int.TryParse(numString, out num))
  115. {
  116. num++;
  117. for (int i = 0; i < numString.Length - num.ToString().Length; i++)
  118. {
  119. buffer.Append('0');
  120. }
  121. buffer.Append(num);
  122. }
  123. else
  124. {
  125. ThrowInvalidFileName(currentFileInfo);
  126. }
  127. buffer.Append(".rar");
  128. return new FileInfo(buffer.ToString());
  129. }
  130. private static void ThrowInvalidFileName(FileInfo fileInfo)
  131. {
  132. throw new ArgumentException("Filename invalid or next archive could not be found:"
  133. + fileInfo.FullName);
  134. }
  135. #endif
  136. }
  137. }