TarArchive.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using SharpCompress.Common;
  6. using SharpCompress.Common.Tar;
  7. using SharpCompress.Common.Tar.Headers;
  8. using SharpCompress.IO;
  9. using SharpCompress.Readers;
  10. using SharpCompress.Readers.Tar;
  11. using SharpCompress.Writers;
  12. using SharpCompress.Writers.Tar;
  13. namespace SharpCompress.Archives.Tar
  14. {
  15. public class TarArchive : AbstractWritableArchive<TarArchiveEntry, TarVolume>
  16. {
  17. #if !NO_FILE
  18. /// <summary>
  19. /// Constructor expects a filepath to an existing file.
  20. /// </summary>
  21. /// <param name="filePath"></param>
  22. /// <param name="readerOptions"></param>
  23. public static TarArchive Open(string filePath, ReaderOptions readerOptions = null)
  24. {
  25. filePath.CheckNotNullOrEmpty("filePath");
  26. return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions());
  27. }
  28. /// <summary>
  29. /// Constructor with a FileInfo object to an existing file.
  30. /// </summary>
  31. /// <param name="fileInfo"></param>
  32. /// <param name="readerOptions"></param>
  33. public static TarArchive Open(FileInfo fileInfo, ReaderOptions readerOptions = null)
  34. {
  35. fileInfo.CheckNotNull("fileInfo");
  36. return new TarArchive(fileInfo, readerOptions ?? new ReaderOptions());
  37. }
  38. #endif
  39. /// <summary>
  40. /// Takes a seekable Stream as a source
  41. /// </summary>
  42. /// <param name="stream"></param>
  43. /// <param name="readerOptions"></param>
  44. public static TarArchive Open(Stream stream, ReaderOptions readerOptions = null)
  45. {
  46. stream.CheckNotNull("stream");
  47. return new TarArchive(stream, readerOptions ?? new ReaderOptions());
  48. }
  49. #if !NO_FILE
  50. public static bool IsTarFile(string filePath)
  51. {
  52. return IsTarFile(new FileInfo(filePath));
  53. }
  54. public static bool IsTarFile(FileInfo fileInfo)
  55. {
  56. if (!fileInfo.Exists)
  57. {
  58. return false;
  59. }
  60. using (Stream stream = fileInfo.OpenRead())
  61. {
  62. return IsTarFile(stream);
  63. }
  64. }
  65. #endif
  66. public static bool IsTarFile(Stream stream)
  67. {
  68. try
  69. {
  70. TarHeader tar = new TarHeader(new ArchiveEncoding());
  71. tar.Read(new BinaryReader(stream));
  72. return tar.Name.Length > 0 && Enum.IsDefined(typeof(EntryType), tar.EntryType);
  73. }
  74. catch
  75. {
  76. }
  77. return false;
  78. }
  79. #if !NO_FILE
  80. /// <summary>
  81. /// Constructor with a FileInfo object to an existing file.
  82. /// </summary>
  83. /// <param name="fileInfo"></param>
  84. /// <param name="readerOptions"></param>
  85. internal TarArchive(FileInfo fileInfo, ReaderOptions readerOptions)
  86. : base(ArchiveType.Tar, fileInfo, readerOptions)
  87. {
  88. }
  89. protected override IEnumerable<TarVolume> LoadVolumes(FileInfo file)
  90. {
  91. return new TarVolume(file.OpenRead(), ReaderOptions).AsEnumerable();
  92. }
  93. #endif
  94. /// <summary>
  95. /// Takes multiple seekable Streams for a multi-part archive
  96. /// </summary>
  97. /// <param name="stream"></param>
  98. /// <param name="readerOptions"></param>
  99. internal TarArchive(Stream stream, ReaderOptions readerOptions)
  100. : base(ArchiveType.Tar, stream, readerOptions)
  101. {
  102. }
  103. internal TarArchive()
  104. : base(ArchiveType.Tar)
  105. {
  106. }
  107. protected override IEnumerable<TarVolume> LoadVolumes(IEnumerable<Stream> streams)
  108. {
  109. return new TarVolume(streams.First(), ReaderOptions).AsEnumerable();
  110. }
  111. protected override IEnumerable<TarArchiveEntry> LoadEntries(IEnumerable<TarVolume> volumes)
  112. {
  113. Stream stream = volumes.Single().Stream;
  114. TarHeader previousHeader = null;
  115. foreach (TarHeader header in TarHeaderFactory.ReadHeader(StreamingMode.Seekable, stream, ReaderOptions.ArchiveEncoding))
  116. {
  117. if (header != null)
  118. {
  119. if (header.EntryType == EntryType.LongName)
  120. {
  121. previousHeader = header;
  122. }
  123. else
  124. {
  125. if (previousHeader != null)
  126. {
  127. var entry = new TarArchiveEntry(this, new TarFilePart(previousHeader, stream),
  128. CompressionType.None);
  129. var oldStreamPos = stream.Position;
  130. using (var entryStream = entry.OpenEntryStream())
  131. {
  132. using (var memoryStream = new MemoryStream())
  133. {
  134. entryStream.TransferTo(memoryStream);
  135. memoryStream.Position = 0;
  136. var bytes = memoryStream.ToArray();
  137. header.Name = ReaderOptions.ArchiveEncoding.Decode(bytes).TrimNulls();
  138. }
  139. }
  140. stream.Position = oldStreamPos;
  141. previousHeader = null;
  142. }
  143. yield return new TarArchiveEntry(this, new TarFilePart(header, stream), CompressionType.None);
  144. }
  145. }
  146. }
  147. }
  148. public static TarArchive Create()
  149. {
  150. return new TarArchive();
  151. }
  152. protected override TarArchiveEntry CreateEntryInternal(string filePath, Stream source,
  153. long size, DateTime? modified, bool closeStream)
  154. {
  155. return new TarWritableArchiveEntry(this, source, CompressionType.Unknown, filePath, size, modified,
  156. closeStream);
  157. }
  158. protected override void SaveTo(Stream stream, WriterOptions options,
  159. IEnumerable<TarArchiveEntry> oldEntries,
  160. IEnumerable<TarArchiveEntry> newEntries)
  161. {
  162. using (var writer = new TarWriter(stream, new TarWriterOptions(options)))
  163. {
  164. foreach (var entry in oldEntries.Concat(newEntries)
  165. .Where(x => !x.IsDirectory))
  166. {
  167. using (var entryStream = entry.OpenEntryStream())
  168. {
  169. writer.Write(entry.Key, entryStream, entry.LastModifiedTime, entry.Size);
  170. }
  171. }
  172. }
  173. }
  174. protected override IReader CreateReaderForSolidExtraction()
  175. {
  176. var stream = Volumes.Single().Stream;
  177. stream.Position = 0;
  178. return TarReader.Open(stream);
  179. }
  180. }
  181. }