GZipArchive.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using SharpCompress.Common;
  6. using SharpCompress.Common.GZip;
  7. using SharpCompress.Readers;
  8. using SharpCompress.Readers.GZip;
  9. using SharpCompress.Writers;
  10. using SharpCompress.Writers.GZip;
  11. namespace SharpCompress.Archives.GZip
  12. {
  13. public class GZipArchive : AbstractWritableArchive<GZipArchiveEntry, GZipVolume>
  14. {
  15. #if !NO_FILE
  16. /// <summary>
  17. /// Constructor expects a filepath to an existing file.
  18. /// </summary>
  19. /// <param name="filePath"></param>
  20. /// <param name="readerOptions"></param>
  21. public static GZipArchive Open(string filePath, ReaderOptions readerOptions = null)
  22. {
  23. filePath.CheckNotNullOrEmpty("filePath");
  24. return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions());
  25. }
  26. /// <summary>
  27. /// Constructor with a FileInfo object to an existing file.
  28. /// </summary>
  29. /// <param name="fileInfo"></param>
  30. /// <param name="readerOptions"></param>
  31. public static GZipArchive Open(FileInfo fileInfo, ReaderOptions readerOptions = null)
  32. {
  33. fileInfo.CheckNotNull("fileInfo");
  34. return new GZipArchive(fileInfo, readerOptions ?? new ReaderOptions());
  35. }
  36. #endif
  37. /// <summary>
  38. /// Takes a seekable Stream as a source
  39. /// </summary>
  40. /// <param name="stream"></param>
  41. /// <param name="readerOptions"></param>
  42. public static GZipArchive Open(Stream stream, ReaderOptions readerOptions = null)
  43. {
  44. stream.CheckNotNull("stream");
  45. return new GZipArchive(stream, readerOptions ?? new ReaderOptions());
  46. }
  47. public static GZipArchive Create()
  48. {
  49. return new GZipArchive();
  50. }
  51. #if !NO_FILE
  52. /// <summary>
  53. /// Constructor with a FileInfo object to an existing file.
  54. /// </summary>
  55. /// <param name="fileInfo"></param>
  56. /// <param name="options"></param>
  57. internal GZipArchive(FileInfo fileInfo, ReaderOptions options)
  58. : base(ArchiveType.GZip, fileInfo, options)
  59. {
  60. }
  61. protected override IEnumerable<GZipVolume> LoadVolumes(FileInfo file)
  62. {
  63. return new GZipVolume(file, ReaderOptions).AsEnumerable();
  64. }
  65. public static bool IsGZipFile(string filePath)
  66. {
  67. return IsGZipFile(new FileInfo(filePath));
  68. }
  69. public static bool IsGZipFile(FileInfo fileInfo)
  70. {
  71. if (!fileInfo.Exists)
  72. {
  73. return false;
  74. }
  75. using (Stream stream = fileInfo.OpenRead())
  76. {
  77. return IsGZipFile(stream);
  78. }
  79. }
  80. public void SaveTo(string filePath)
  81. {
  82. SaveTo(new FileInfo(filePath));
  83. }
  84. public void SaveTo(FileInfo fileInfo)
  85. {
  86. using (var stream = fileInfo.Open(FileMode.Create, FileAccess.Write))
  87. {
  88. SaveTo(stream, new WriterOptions(CompressionType.GZip));
  89. }
  90. }
  91. #endif
  92. public static bool IsGZipFile(Stream stream)
  93. {
  94. // read the header on the first read
  95. byte[] header = new byte[10];
  96. // workitem 8501: handle edge case (decompress empty stream)
  97. if (!stream.ReadFully(header))
  98. {
  99. return false;
  100. }
  101. if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8)
  102. {
  103. return false;
  104. }
  105. return true;
  106. }
  107. /// <summary>
  108. /// Takes multiple seekable Streams for a multi-part archive
  109. /// </summary>
  110. /// <param name="stream"></param>
  111. /// <param name="options"></param>
  112. internal GZipArchive(Stream stream, ReaderOptions options)
  113. : base(ArchiveType.GZip, stream, options)
  114. {
  115. }
  116. internal GZipArchive()
  117. : base(ArchiveType.GZip)
  118. {
  119. }
  120. protected override GZipArchiveEntry CreateEntryInternal(string filePath, Stream source, long size, DateTime? modified,
  121. bool closeStream)
  122. {
  123. if (Entries.Any())
  124. {
  125. throw new InvalidOperationException("Only one entry is allowed in a GZip Archive");
  126. }
  127. return new GZipWritableArchiveEntry(this, source, filePath, size, modified, closeStream);
  128. }
  129. protected override void SaveTo(Stream stream, WriterOptions options,
  130. IEnumerable<GZipArchiveEntry> oldEntries,
  131. IEnumerable<GZipArchiveEntry> newEntries)
  132. {
  133. if (Entries.Count > 1)
  134. {
  135. throw new InvalidOperationException("Only one entry is allowed in a GZip Archive");
  136. }
  137. using (var writer = new GZipWriter(stream, new GZipWriterOptions(options)))
  138. {
  139. foreach (var entry in oldEntries.Concat(newEntries)
  140. .Where(x => !x.IsDirectory))
  141. {
  142. using (var entryStream = entry.OpenEntryStream())
  143. {
  144. writer.Write(entry.Key, entryStream, entry.LastModifiedTime);
  145. }
  146. }
  147. }
  148. }
  149. protected override IEnumerable<GZipVolume> LoadVolumes(IEnumerable<Stream> streams)
  150. {
  151. return new GZipVolume(streams.First(), ReaderOptions).AsEnumerable();
  152. }
  153. protected override IEnumerable<GZipArchiveEntry> LoadEntries(IEnumerable<GZipVolume> volumes)
  154. {
  155. Stream stream = volumes.Single().Stream;
  156. yield return new GZipArchiveEntry(this, new GZipFilePart(stream, ReaderOptions.ArchiveEncoding));
  157. }
  158. protected override IReader CreateReaderForSolidExtraction()
  159. {
  160. var stream = Volumes.Single().Stream;
  161. stream.Position = 0;
  162. return GZipReader.Open(stream);
  163. }
  164. }
  165. }