GZipFilePart.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using SharpCompress.Common.Tar.Headers;
  5. using SharpCompress.Compressors;
  6. using SharpCompress.Compressors.Deflate;
  7. using SharpCompress.Converters;
  8. using System.Text;
  9. namespace SharpCompress.Common.GZip
  10. {
  11. internal class GZipFilePart : FilePart
  12. {
  13. private string _name;
  14. private readonly Stream _stream;
  15. internal GZipFilePart(Stream stream, ArchiveEncoding archiveEncoding)
  16. : base(archiveEncoding)
  17. {
  18. ReadAndValidateGzipHeader(stream);
  19. EntryStartPosition = stream.Position;
  20. _stream = stream;
  21. }
  22. internal long EntryStartPosition { get; }
  23. internal DateTime? DateModified { get; private set; }
  24. internal override string FilePartName => _name;
  25. internal override Stream GetCompressedStream()
  26. {
  27. return new DeflateStream(_stream, CompressionMode.Decompress, CompressionLevel.Default);
  28. }
  29. internal override Stream GetRawStream()
  30. {
  31. return _stream;
  32. }
  33. private void ReadAndValidateGzipHeader(Stream stream)
  34. {
  35. // read the header on the first read
  36. byte[] header = new byte[10];
  37. int n = stream.Read(header, 0, header.Length);
  38. // workitem 8501: handle edge case (decompress empty stream)
  39. if (n == 0)
  40. {
  41. return;
  42. }
  43. if (n != 10)
  44. {
  45. throw new ZlibException("Not a valid GZIP stream.");
  46. }
  47. if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8)
  48. {
  49. throw new ZlibException("Bad GZIP header.");
  50. }
  51. Int32 timet = DataConverter.LittleEndian.GetInt32(header, 4);
  52. DateModified = TarHeader.EPOCH.AddSeconds(timet);
  53. if ((header[3] & 0x04) == 0x04)
  54. {
  55. // read and discard extra field
  56. n = stream.Read(header, 0, 2); // 2-byte length field
  57. Int16 extraLength = (Int16)(header[0] + header[1] * 256);
  58. byte[] extra = new byte[extraLength];
  59. if (!stream.ReadFully(extra))
  60. {
  61. throw new ZlibException("Unexpected end-of-file reading GZIP header.");
  62. }
  63. n = extraLength;
  64. }
  65. if ((header[3] & 0x08) == 0x08)
  66. {
  67. _name = ReadZeroTerminatedString(stream);
  68. }
  69. if ((header[3] & 0x10) == 0x010)
  70. {
  71. ReadZeroTerminatedString(stream);
  72. }
  73. if ((header[3] & 0x02) == 0x02)
  74. {
  75. stream.ReadByte(); // CRC16, ignore
  76. }
  77. }
  78. private string ReadZeroTerminatedString(Stream stream)
  79. {
  80. byte[] buf1 = new byte[1];
  81. var list = new List<byte>();
  82. bool done = false;
  83. do
  84. {
  85. // workitem 7740
  86. int n = stream.Read(buf1, 0, 1);
  87. if (n != 1)
  88. {
  89. throw new ZlibException("Unexpected EOF reading GZIP header.");
  90. }
  91. if (buf1[0] == 0)
  92. {
  93. done = true;
  94. }
  95. else
  96. {
  97. list.Add(buf1[0]);
  98. }
  99. }
  100. while (!done);
  101. byte[] buffer = list.ToArray();
  102. return ArchiveEncoding.Decode(buffer);
  103. }
  104. }
  105. }