示例#1
0
 public ZipArchive()
 {
     CentralDirectoryFooter = new CentralDirectoryFooter {
         ZipArchive = this
     };
     Entries = new ZipEntryCollection(this);
 }
示例#2
0
 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)));
 }
示例#3
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");
            }
        }
示例#4
0
        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);
        }