internal ZipArchiveEntry(ZipArchive archive, ZipCentralDirectoryFileHeader cd) { this._archive = archive; this._originallyInArchive = true; this._diskNumberStart = cd.DiskNumberStart; this._versionToExtract = (ZipVersionNeededValues)cd.VersionNeededToExtract; this._generalPurposeBitFlag = (ZipArchiveEntry.BitFlagValues)cd.GeneralPurposeBitFlag; this.CompressionMethod = (ZipArchiveEntry.CompressionMethodValues)cd.CompressionMethod; this._lastModified = new DateTimeOffset(ZipHelper.DosTimeToDateTime(cd.LastModified)); this._compressedSize = cd.CompressedSize; this._uncompressedSize = cd.UncompressedSize; this._offsetOfLocalHeader = cd.RelativeOffsetOfLocalHeader; this._storedOffsetOfCompressedData = new long?(); this._crc32 = cd.Crc32; this._compressedBytes = null; this._storedUncompressedData = null; this._currentlyOpenForWrite = false; this._everOpenedForWrite = false; this._outstandingWriteStream = null; this.FullName = this.DecodeEntryName(cd.Filename); this._lhUnknownExtraFields = null; this._cdUnknownExtraFields = cd.ExtraFields; this._fileComment = cd.FileComment; this._externalFileAttributes = cd.ExternalFileAttributes; }
private void ReadCentralDirectory() { try { this._archiveStream.Seek(this._centralDirectoryStart, SeekOrigin.Begin); long num = 0L; bool saveExtraFieldsAndComments = this.Mode == ZipArchiveMode.Update; ZipCentralDirectoryFileHeader header; while (ZipCentralDirectoryFileHeader.TryReadBlock(this._archiveReader, saveExtraFieldsAndComments, out header)) { this.AddEntry(new ZipArchiveEntry(this, header)); ++num; } if (num != this._expectedNumberOfEntries) { throw new InvalidDataException(CompressionConstants.NumEntriesWrong); } } catch (EndOfStreamException ex) { throw new InvalidDataException(CompressionConstants.CentralDirectoryInvalid, (Exception)ex); } }
public static bool TryReadBlock(BinaryReader reader, bool saveExtraFieldsAndComments, out ZipCentralDirectoryFileHeader header) { header = new ZipCentralDirectoryFileHeader(); if (reader.ReadUInt32() != 33639248u) { return(false); } header.VersionMadeBy = reader.ReadUInt16(); header.VersionNeededToExtract = reader.ReadUInt16(); header.GeneralPurposeBitFlag = reader.ReadUInt16(); header.CompressionMethod = reader.ReadUInt16(); header.LastModified = reader.ReadUInt32(); header.Crc32 = reader.ReadUInt32(); uint num = reader.ReadUInt32(); uint num2 = reader.ReadUInt32(); header.FilenameLength = reader.ReadUInt16(); header.ExtraFieldLength = reader.ReadUInt16(); header.FileCommentLength = reader.ReadUInt16(); ushort num3 = reader.ReadUInt16(); header.InternalFileAttributes = reader.ReadUInt16(); header.ExternalFileAttributes = reader.ReadUInt32(); uint num4 = reader.ReadUInt32(); header.Filename = reader.ReadBytes((int)header.FilenameLength); bool readUncompressedSize = num2 == 4294967295u; bool readCompressedSize = num == 4294967295u; bool readLocalHeaderOffset = num4 == 4294967295u; bool readStartDiskNumber = num3 == 65535; long position = reader.BaseStream.Position + (long)((ulong)header.ExtraFieldLength); Zip64ExtraField zip64ExtraField; using (Stream stream = new SubReadStream(reader.BaseStream, reader.BaseStream.Position, (long)((ulong)header.ExtraFieldLength))) { if (saveExtraFieldsAndComments) { header.ExtraFields = ZipGenericExtraField.ParseExtraField(stream); zip64ExtraField = Zip64ExtraField.GetAndRemoveZip64Block(header.ExtraFields, readUncompressedSize, readCompressedSize, readLocalHeaderOffset, readStartDiskNumber); } else { header.ExtraFields = null; zip64ExtraField = Zip64ExtraField.GetJustZip64Block(stream, readUncompressedSize, readCompressedSize, readLocalHeaderOffset, readStartDiskNumber); } } ZipHelper.AdvanceToPosition(reader.BaseStream, position); if (saveExtraFieldsAndComments) { header.FileComment = reader.ReadBytes((int)header.FileCommentLength); } else { reader.BaseStream.Position += (long)((ulong)header.FileCommentLength); header.FileComment = null; } header.UncompressedSize = (long)((!zip64ExtraField.UncompressedSize.HasValue) ? ((ulong)num2) : ((ulong)zip64ExtraField.UncompressedSize.Value)); header.CompressedSize = (long)((!zip64ExtraField.CompressedSize.HasValue) ? ((ulong)num) : ((ulong)zip64ExtraField.CompressedSize.Value)); header.RelativeOffsetOfLocalHeader = (long)((!zip64ExtraField.LocalHeaderOffset.HasValue) ? ((ulong)num4) : ((ulong)zip64ExtraField.LocalHeaderOffset.Value)); header.DiskNumberStart = ((!zip64ExtraField.StartDiskNumber.HasValue) ? ((int)num3) : zip64ExtraField.StartDiskNumber.Value); return(true); }
public static bool TryReadBlock(BinaryReader reader, bool saveExtraFieldsAndComments, out ZipCentralDirectoryFileHeader header) { header = new ZipCentralDirectoryFileHeader(); if ((int)reader.ReadUInt32() != 33639248) { return(false); } header.VersionMadeBy = reader.ReadUInt16(); header.VersionNeededToExtract = reader.ReadUInt16(); header.GeneralPurposeBitFlag = reader.ReadUInt16(); header.CompressionMethod = reader.ReadUInt16(); header.LastModified = reader.ReadUInt32(); header.Crc32 = reader.ReadUInt32(); uint num1 = reader.ReadUInt32(); uint num2 = reader.ReadUInt32(); header.FilenameLength = reader.ReadUInt16(); header.ExtraFieldLength = reader.ReadUInt16(); header.FileCommentLength = reader.ReadUInt16(); ushort num3 = reader.ReadUInt16(); header.InternalFileAttributes = reader.ReadUInt16(); header.ExternalFileAttributes = reader.ReadUInt32(); uint num4 = reader.ReadUInt32(); header.FileName = reader.ReadBytes((int)header.FilenameLength); bool readUncompressedSize = (int)num2 == -1; bool readCompressedSize = (int)num1 == -1; bool readLocalHeaderOffset = (int)num4 == -1; bool readStartDiskNumber = (int)num3 == (int)ushort.MaxValue; Zip64ExtraField zip64ExtraField; using (Stream stream = (Stream) new SubReadStream(reader.BaseStream, reader.BaseStream.Position, (long)header.ExtraFieldLength)) { if (saveExtraFieldsAndComments) { header.ExtraFields = ZipGenericExtraField.ParseExtraField(stream); zip64ExtraField = Zip64ExtraField.GetAndRemoveZip64Block(header.ExtraFields, readUncompressedSize, readCompressedSize, readLocalHeaderOffset, readStartDiskNumber); } else { header.ExtraFields = (List <ZipGenericExtraField>)null; zip64ExtraField = Zip64ExtraField.GetJustZip64Block(stream, readUncompressedSize, readCompressedSize, readLocalHeaderOffset, readStartDiskNumber); } } if (saveExtraFieldsAndComments) { header.FileComment = reader.ReadBytes((int)header.FileCommentLength); } else { reader.BaseStream.Position += (long)header.FileCommentLength; header.FileComment = (byte[])null; } header.UncompressedSize = !zip64ExtraField.UncompressedSize.HasValue ? (long)num2 : zip64ExtraField.UncompressedSize.Value; header.CompressedSize = !zip64ExtraField.CompressedSize.HasValue ? (long)num1 : zip64ExtraField.CompressedSize.Value; header.RelativeOffsetOfLocalHeader = !zip64ExtraField.LocalHeaderOffset.HasValue ? (long)num4 : zip64ExtraField.LocalHeaderOffset.Value; header.DiskNumberStart = !zip64ExtraField.StartDiskNumber.HasValue ? (int)num3 : zip64ExtraField.StartDiskNumber.Value; return(true); }