ZipArchive.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using SharpCompress.Common;
  6. using SharpCompress.Common.Zip;
  7. using SharpCompress.Common.Zip.Headers;
  8. using SharpCompress.Compressors.Deflate;
  9. using SharpCompress.Readers;
  10. using SharpCompress.Readers.Zip;
  11. using SharpCompress.Writers;
  12. using SharpCompress.Writers.Zip;
  13. namespace SharpCompress.Archives.Zip
  14. {
  15. public class ZipArchive : AbstractWritableArchive<ZipArchiveEntry, ZipVolume>
  16. {
  17. private readonly SeekableZipHeaderFactory headerFactory;
  18. /// <summary>
  19. /// Gets or sets the compression level applied to files added to the archive,
  20. /// if the compression method is set to deflate
  21. /// </summary>
  22. public CompressionLevel DeflateCompressionLevel { get; set; }
  23. #if !NO_FILE
  24. /// <summary>
  25. /// Constructor expects a filepath to an existing file.
  26. /// </summary>
  27. /// <param name="filePath"></param>
  28. /// <param name="readerOptions"></param>
  29. public static ZipArchive Open(string filePath, ReaderOptions readerOptions = null)
  30. {
  31. filePath.CheckNotNullOrEmpty("filePath");
  32. return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions());
  33. }
  34. /// <summary>
  35. /// Constructor with a FileInfo object to an existing file.
  36. /// </summary>
  37. /// <param name="fileInfo"></param>
  38. /// <param name="readerOptions"></param>
  39. public static ZipArchive Open(FileInfo fileInfo, ReaderOptions readerOptions = null)
  40. {
  41. fileInfo.CheckNotNull("fileInfo");
  42. return new ZipArchive(fileInfo, readerOptions ?? new ReaderOptions());
  43. }
  44. #endif
  45. /// <summary>
  46. /// Takes a seekable Stream as a source
  47. /// </summary>
  48. /// <param name="stream"></param>
  49. /// <param name="readerOptions"></param>
  50. public static ZipArchive Open(Stream stream, ReaderOptions readerOptions = null)
  51. {
  52. stream.CheckNotNull("stream");
  53. return new ZipArchive(stream, readerOptions ?? new ReaderOptions());
  54. }
  55. #if !NO_FILE
  56. public static bool IsZipFile(string filePath, string password = null)
  57. {
  58. return IsZipFile(new FileInfo(filePath), password);
  59. }
  60. public static bool IsZipFile(FileInfo fileInfo, string password = null)
  61. {
  62. if (!fileInfo.Exists)
  63. {
  64. return false;
  65. }
  66. using (Stream stream = fileInfo.OpenRead())
  67. {
  68. return IsZipFile(stream, password);
  69. }
  70. }
  71. #endif
  72. public static bool IsZipFile(Stream stream, string password = null)
  73. {
  74. StreamingZipHeaderFactory headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding());
  75. try
  76. {
  77. ZipHeader header =
  78. headerFactory.ReadStreamHeader(stream).FirstOrDefault(x => x.ZipHeaderType != ZipHeaderType.Split);
  79. if (header == null)
  80. {
  81. return false;
  82. }
  83. return Enum.IsDefined(typeof(ZipHeaderType), header.ZipHeaderType);
  84. }
  85. catch (CryptographicException)
  86. {
  87. return true;
  88. }
  89. catch
  90. {
  91. return false;
  92. }
  93. }
  94. #if !NO_FILE
  95. /// <summary>
  96. /// Constructor with a FileInfo object to an existing file.
  97. /// </summary>
  98. /// <param name="fileInfo"></param>
  99. /// <param name="readerOptions"></param>
  100. internal ZipArchive(FileInfo fileInfo, ReaderOptions readerOptions)
  101. : base(ArchiveType.Zip, fileInfo, readerOptions)
  102. {
  103. headerFactory = new SeekableZipHeaderFactory(readerOptions.Password, readerOptions.ArchiveEncoding);
  104. }
  105. protected override IEnumerable<ZipVolume> LoadVolumes(FileInfo file)
  106. {
  107. return new ZipVolume(file.OpenRead(), ReaderOptions).AsEnumerable();
  108. }
  109. #endif
  110. internal ZipArchive()
  111. : base(ArchiveType.Zip)
  112. {
  113. }
  114. /// <summary>
  115. /// Takes multiple seekable Streams for a multi-part archive
  116. /// </summary>
  117. /// <param name="stream"></param>
  118. /// <param name="readerOptions"></param>
  119. internal ZipArchive(Stream stream, ReaderOptions readerOptions)
  120. : base(ArchiveType.Zip, stream, readerOptions)
  121. {
  122. headerFactory = new SeekableZipHeaderFactory(readerOptions.Password, readerOptions.ArchiveEncoding);
  123. }
  124. protected override IEnumerable<ZipVolume> LoadVolumes(IEnumerable<Stream> streams)
  125. {
  126. return new ZipVolume(streams.First(), ReaderOptions).AsEnumerable();
  127. }
  128. protected override IEnumerable<ZipArchiveEntry> LoadEntries(IEnumerable<ZipVolume> volumes)
  129. {
  130. var volume = volumes.Single();
  131. Stream stream = volume.Stream;
  132. foreach (ZipHeader h in headerFactory.ReadSeekableHeader(stream))
  133. {
  134. if (h != null)
  135. {
  136. switch (h.ZipHeaderType)
  137. {
  138. case ZipHeaderType.DirectoryEntry:
  139. {
  140. yield return new ZipArchiveEntry(this,
  141. new SeekableZipFilePart(headerFactory,
  142. h as DirectoryEntryHeader,
  143. stream));
  144. }
  145. break;
  146. case ZipHeaderType.DirectoryEnd:
  147. {
  148. byte[] bytes = (h as DirectoryEndHeader).Comment;
  149. volume.Comment = ReaderOptions.ArchiveEncoding.Decode(bytes);
  150. yield break;
  151. }
  152. }
  153. }
  154. }
  155. }
  156. public void SaveTo(Stream stream)
  157. {
  158. SaveTo(stream, new WriterOptions(CompressionType.Deflate));
  159. }
  160. protected override void SaveTo(Stream stream, WriterOptions options,
  161. IEnumerable<ZipArchiveEntry> oldEntries,
  162. IEnumerable<ZipArchiveEntry> newEntries)
  163. {
  164. using (var writer = new ZipWriter(stream, new ZipWriterOptions(options)))
  165. {
  166. foreach (var entry in oldEntries.Concat(newEntries)
  167. .Where(x => !x.IsDirectory))
  168. {
  169. using (var entryStream = entry.OpenEntryStream())
  170. {
  171. writer.Write(entry.Key, entryStream, entry.LastModifiedTime);
  172. }
  173. }
  174. }
  175. }
  176. protected override ZipArchiveEntry CreateEntryInternal(string filePath, Stream source, long size, DateTime? modified,
  177. bool closeStream)
  178. {
  179. return new ZipWritableArchiveEntry(this, source, filePath, size, modified, closeStream);
  180. }
  181. public static ZipArchive Create()
  182. {
  183. return new ZipArchive();
  184. }
  185. protected override IReader CreateReaderForSolidExtraction()
  186. {
  187. var stream = Volumes.Single().Stream;
  188. stream.Position = 0;
  189. return ZipReader.Open(stream, ReaderOptions);
  190. }
  191. }
  192. }