ZipFileEntry.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using SharpCompress.Converters;
  6. namespace SharpCompress.Common.Zip.Headers
  7. {
  8. internal abstract class ZipFileEntry : ZipHeader
  9. {
  10. protected ZipFileEntry(ZipHeaderType type, ArchiveEncoding archiveEncoding)
  11. : base(type)
  12. {
  13. Extra = new List<ExtraData>();
  14. ArchiveEncoding = archiveEncoding;
  15. }
  16. internal bool IsDirectory
  17. {
  18. get
  19. {
  20. if (Name.EndsWith("/"))
  21. {
  22. return true;
  23. }
  24. //.NET Framework 4.5 : System.IO.Compression::CreateFromDirectory() probably writes backslashes to headers
  25. return CompressedSize == 0
  26. && UncompressedSize == 0
  27. && Name.EndsWith("\\");
  28. }
  29. }
  30. internal Stream PackedStream { get; set; }
  31. internal ArchiveEncoding ArchiveEncoding { get; }
  32. internal string Name { get; set; }
  33. internal HeaderFlags Flags { get; set; }
  34. internal ZipCompressionMethod CompressionMethod { get; set; }
  35. internal long CompressedSize { get; set; }
  36. internal long? DataStartPosition { get; set; }
  37. internal long UncompressedSize { get; set; }
  38. internal List<ExtraData> Extra { get; set; }
  39. public string Password { get; set; }
  40. internal PkwareTraditionalEncryptionData ComposeEncryptionData(Stream archiveStream)
  41. {
  42. if (archiveStream == null)
  43. {
  44. throw new ArgumentNullException(nameof(archiveStream));
  45. }
  46. var buffer = new byte[12];
  47. archiveStream.ReadFully(buffer);
  48. PkwareTraditionalEncryptionData encryptionData = PkwareTraditionalEncryptionData.ForRead(Password, this, buffer);
  49. return encryptionData;
  50. }
  51. #if !NO_CRYPTO
  52. internal WinzipAesEncryptionData WinzipAesEncryptionData { get; set; }
  53. #endif
  54. internal ushort LastModifiedDate { get; set; }
  55. internal ushort LastModifiedTime { get; set; }
  56. internal uint Crc { get; set; }
  57. protected void LoadExtra(byte[] extra)
  58. {
  59. for (int i = 0; i < extra.Length - 4;)
  60. {
  61. ExtraDataType type = (ExtraDataType)DataConverter.LittleEndian.GetUInt16(extra, i);
  62. if (!Enum.IsDefined(typeof(ExtraDataType), type))
  63. {
  64. type = ExtraDataType.NotImplementedExtraData;
  65. }
  66. ushort length = DataConverter.LittleEndian.GetUInt16(extra, i + 2);
  67. byte[] data = new byte[length];
  68. Buffer.BlockCopy(extra, i + 4, data, 0, length);
  69. Extra.Add(LocalEntryHeaderExtraFactory.Create(type, length, data));
  70. i += length + 4;
  71. }
  72. }
  73. internal ZipFilePart Part { get; set; }
  74. internal bool IsZip64 => CompressedSize == uint.MaxValue;
  75. }
  76. }