public ZipArchive() { CentralDirectoryFooter = new CentralDirectoryFooter { ZipArchive = this }; Entries = new ZipEntryCollection(this); }
internal override int GetSize() { return(CentralDirectoryFooter.GetSize() + Entries.Sum(entry => entry.CentralDirectoryHeader.GetSize() + entry.LocalFileHeader.GetSize() + (entry.Data?.Length ?? 0) + (entry.HasDataDescriptor ? entry.DataDescriptor.GetSize() : 0))); }
public void Write(BinaryWriter writer) { Offset = writer.BaseStream.Position; foreach (var entry in Entries) { entry.Write(writer); } foreach (var entry in Entries) { entry.CentralDirectoryHeader.Write(writer); } CentralDirectoryFooter.Write(writer); if (writer.BaseStream.Position - Offset != GetSize()) { throw new NotSupportedException("size mismatch"); } }
public static ZipArchive Read(BinaryReader reader) { var zip = new ZipArchive { Offset = reader.BaseStream.Position, CentralDirectoryFooter = CentralDirectoryFooter.Read(reader) }; zip.CentralDirectoryFooter.ZipArchive = zip; var headers = new List <CentralDirectoryHeader>(); reader.BaseStream.Seek(zip.CentralDirectoryFooter.CentralDirectoryOffset, SeekOrigin.Begin); for (var i = 0; i < zip.CentralDirectoryFooter.DiskEntries; i++) { headers.Add(CentralDirectoryHeader.Read(reader)); } foreach (var header in headers) { zip.Entries.Add(ZipEntry.Read(reader, header)); } return(zip); }