LocalEntryHeader.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.IO;
  2. using System.Linq;
  3. using System.Text;
  4. namespace SharpCompress.Common.Zip.Headers
  5. {
  6. internal class LocalEntryHeader : ZipFileEntry
  7. {
  8. public LocalEntryHeader(ArchiveEncoding archiveEncoding)
  9. : base(ZipHeaderType.LocalEntry, archiveEncoding)
  10. {
  11. }
  12. internal override void Read(BinaryReader reader)
  13. {
  14. Version = reader.ReadUInt16();
  15. Flags = (HeaderFlags)reader.ReadUInt16();
  16. CompressionMethod = (ZipCompressionMethod)reader.ReadUInt16();
  17. LastModifiedTime = reader.ReadUInt16();
  18. LastModifiedDate = reader.ReadUInt16();
  19. Crc = reader.ReadUInt32();
  20. CompressedSize = reader.ReadUInt32();
  21. UncompressedSize = reader.ReadUInt32();
  22. ushort nameLength = reader.ReadUInt16();
  23. ushort extraLength = reader.ReadUInt16();
  24. byte[] name = reader.ReadBytes(nameLength);
  25. byte[] extra = reader.ReadBytes(extraLength);
  26. // According to .ZIP File Format Specification
  27. //
  28. // For example: https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT
  29. //
  30. // Bit 11: Language encoding flag (EFS). If this bit is set,
  31. // the filename and comment fields for this file
  32. // MUST be encoded using UTF-8. (see APPENDIX D)
  33. if (Flags.HasFlag(HeaderFlags.Efs))
  34. {
  35. Name = ArchiveEncoding.DecodeUTF8(name);
  36. }
  37. else
  38. {
  39. Name = ArchiveEncoding.Decode(name);
  40. }
  41. LoadExtra(extra);
  42. var unicodePathExtra = Extra.FirstOrDefault(u => u.Type == ExtraDataType.UnicodePathExtraField);
  43. if (unicodePathExtra != null)
  44. {
  45. Name = ((ExtraUnicodePathExtraField)unicodePathExtra).UnicodeName;
  46. }
  47. var zip64ExtraData = Extra.OfType<Zip64ExtendedInformationExtraField>().FirstOrDefault();
  48. if (zip64ExtraData != null)
  49. {
  50. if (CompressedSize == uint.MaxValue)
  51. {
  52. CompressedSize = zip64ExtraData.CompressedSize;
  53. }
  54. if (UncompressedSize == uint.MaxValue)
  55. {
  56. UncompressedSize = zip64ExtraData.UncompressedSize;
  57. }
  58. }
  59. }
  60. internal ushort Version { get; private set; }
  61. }
  62. }