public ZipFileHeader(ZipFileInfo fileInfo, bool zip64) : this() { flags = ZipFileFlags.None; compressionMethod = fileInfo.CompressionMethod; fileName = Path.Combine(fileInfo.Path, fileInfo.Name); CompressionEngine.DateTimeToDosDateAndTime( fileInfo.LastWriteTime, out lastModDate, out lastModTime); this.zip64 = zip64; if (this.zip64) { compressedSize = UInt32.MaxValue; uncompressedSize = UInt32.MaxValue; diskStart = UInt16.MaxValue; versionMadeBy = 45; versionNeeded = 45; ZipExtraFileField field = new ZipExtraFileField(); field.fieldType = ZipExtraFileFieldType.ZIP64; field.SetZip64Data( fileInfo.CompressedLength, fileInfo.Length, 0, fileInfo.ArchiveNumber); extraFields = new[] { field }; } else { compressedSize = (uint)fileInfo.CompressedLength; uncompressedSize = (uint)fileInfo.Length; diskStart = (ushort)fileInfo.ArchiveNumber; } }
public bool Read(Stream stream, bool central) { long startPos = stream.Position; if (stream.Length - startPos < (central ? CFH_FIXEDSIZE : LFH_FIXEDSIZE)) { return(false); } BinaryReader reader = new BinaryReader(stream); uint sig = reader.ReadUInt32(); if (sig == SPANSIG || sig == SPANSIG2) { // Spanned zip files may optionally begin with a special marker. // Just ignore it and move on. sig = reader.ReadUInt32(); } if (sig != (central ? CFHSIG : LFHSIG)) { return(false); } versionMadeBy = (central ? reader.ReadUInt16() : (ushort)0); versionNeeded = reader.ReadUInt16(); flags = (ZipFileFlags)reader.ReadUInt16(); compressionMethod = (ZipCompressionMethod)reader.ReadUInt16(); lastModTime = reader.ReadInt16(); lastModDate = reader.ReadInt16(); crc32 = reader.ReadUInt32(); compressedSize = reader.ReadUInt32(); uncompressedSize = reader.ReadUInt32(); zip64 = uncompressedSize == UInt32.MaxValue; int fileNameLength = reader.ReadUInt16(); int extraFieldLength = reader.ReadUInt16(); int fileCommentLength; if (central) { fileCommentLength = reader.ReadUInt16(); diskStart = reader.ReadUInt16(); internalFileAttrs = reader.ReadUInt16(); externalFileAttrs = reader.ReadUInt32(); localHeaderOffset = reader.ReadUInt32(); } else { fileCommentLength = 0; diskStart = 0; internalFileAttrs = 0; externalFileAttrs = 0; localHeaderOffset = 0; } if (stream.Length - stream.Position < fileNameLength + extraFieldLength + fileCommentLength) { return(false); } Encoding headerEncoding = ((flags | ZipFileFlags.UTF8) != 0 ? Encoding.UTF8 : Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage)); byte[] fileNameBytes = reader.ReadBytes(fileNameLength); fileName = headerEncoding.GetString(fileNameBytes).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); List <ZipExtraFileField> fields = new List <ZipExtraFileField>(); while (extraFieldLength > 0) { ZipExtraFileField field = new ZipExtraFileField(); if (!field.Read(stream, ref extraFieldLength)) { return(false); } fields.Add(field); if (field.fieldType == ZipExtraFileFieldType.ZIP64) { zip64 = true; } } extraFields = fields.ToArray(); byte[] fileCommentBytes = reader.ReadBytes(fileCommentLength); fileComment = headerEncoding.GetString(fileCommentBytes); return(true); }