コード例 #1
0
        private void WriteArchive()
        {
            if (this.Mode == ZipArchiveMode.Update)
            {
                foreach (ZipArchiveEntry zipArchiveEntry in this.entries.Values)
                {
                    zipArchiveEntry.Dispose();
                }
            }
            long position1 = this.Writer.BaseStream.Position;

            foreach (ZipArchiveEntry zipArchiveEntry in this.entries.Values)
            {
                zipArchiveEntry.WriteCentralDirectoryHeader();
            }
            long num  = this.Writer.BaseStream.Position - position1;
            bool flag = false;

            if (position1 >= (long)uint.MaxValue || num >= (long)uint.MaxValue || this.entries.Count >= (int)ushort.MaxValue)
            {
                flag = true;
            }
            if (flag)
            {
                long position2 = this.Writer.BaseStream.Position;
                if (this.zip64EndOfCentralDirectoryRecord == null)
                {
                    this.zip64EndOfCentralDirectoryRecord = new Zip64EndOfCentralDirectoryRecord();
                }
                this.zip64EndOfCentralDirectoryRecord.NumberOfEntriesInTheCentralDirectory           = (ulong)this.entries.Count;
                this.zip64EndOfCentralDirectoryRecord.NumberOfEntriesInTheCentralDirectoryOnThisDisk = (ulong)this.entries.Count;
                this.zip64EndOfCentralDirectoryRecord.OffsetOfStartOfCentralDirectoryWithRespectToTheStartingDiskNumber = (ulong)position1;
                this.zip64EndOfCentralDirectoryRecord.SizeOfCentralDirectory = (ulong)num;
                this.zip64EndOfCentralDirectoryRecord.WriteBlock(this.Writer);
                if (this.zip64EndOfCentralDirectoryLocator == null)
                {
                    this.zip64EndOfCentralDirectoryLocator = new Zip64EndOfCentralDirectoryLocator();
                }
                this.zip64EndOfCentralDirectoryLocator.NumberOfTheDiskWithTheStartOfTheZip64EndOfCentralDirectory = 0U;
                this.zip64EndOfCentralDirectoryLocator.NumberOfDisks = 1U;
                this.zip64EndOfCentralDirectoryLocator.RelativeOffsetOfTheZip64EndOfCentralDirectoryRecord = (ulong)position2;
                this.zip64EndOfCentralDirectoryLocator.WriteBlock(this.Writer);
            }
            if (this.endOfCentralDirectoryRecord == null)
            {
                this.endOfCentralDirectoryRecord = new EndOfCentralDirectoryRecord();
            }
            this.endOfCentralDirectoryRecord.NumberOfThisDisk = (ushort)0;
            this.endOfCentralDirectoryRecord.NumberOfTheDiskWithTheStartOfTheCentralDirectory = (ushort)0;
            this.endOfCentralDirectoryRecord.NumberOfEntriesInTheCentralDirectoryOnThisDisk   = this.entries.Count < (int)ushort.MaxValue ? (ushort)this.entries.Count : ushort.MaxValue;
            this.endOfCentralDirectoryRecord.NumberOfEntriesInTheCentralDirectory             = this.entries.Count < (int)ushort.MaxValue ? (ushort)this.entries.Count : ushort.MaxValue;
            this.endOfCentralDirectoryRecord.OffsetOfStartOfCentralDirectoryWithRespectToTheStartingDiskNumber = position1 > (long)uint.MaxValue ? uint.MaxValue : (uint)position1;
            this.endOfCentralDirectoryRecord.SizeOfCentralDirectory = num > (long)uint.MaxValue ? uint.MaxValue : (uint)num;
            this.endOfCentralDirectoryRecord.WriteBlock(this.Writer);
            if (this.Mode != ZipArchiveMode.Update)
            {
                return;
            }
            this.workingStream.Seek(0L, SeekOrigin.Begin);
            this.originalStream.Seek(0L, SeekOrigin.Begin);
            ZipFile.CopyStreamTo(this.workingStream, this.originalStream);
            this.originalStream.SetLength(this.workingStream.Length);
        }
コード例 #2
0
 private void ReadEndOfCentralDirectory()
 {
     try
     {
         this.Reader.BaseStream.Seek(-18L, SeekOrigin.End);
         if (!ZipHelper.SeekBackwardsToSignature(this.Reader.BaseStream, 101010256U))
         {
             throw new InvalidDataException("End of central directory not found.");
         }
         long position = this.Reader.BaseStream.Position;
         this.endOfCentralDirectoryRecord = new EndOfCentralDirectoryRecord();
         if (!this.endOfCentralDirectoryRecord.TryReadBlock(this.archiveReader))
         {
             throw new InvalidDataException("End of central directory not found.");
         }
         if ((int)this.endOfCentralDirectoryRecord.NumberOfThisDisk != (int)this.endOfCentralDirectoryRecord.NumberOfTheDiskWithTheStartOfTheCentralDirectory)
         {
             throw new InvalidDataException("Splitted archive is not supported.");
         }
         if ((int)this.endOfCentralDirectoryRecord.NumberOfEntriesInTheCentralDirectory != (int)this.endOfCentralDirectoryRecord.NumberOfEntriesInTheCentralDirectoryOnThisDisk)
         {
             throw new InvalidDataException("Splitted archive is not supported.");
         }
         if (this.endOfCentralDirectoryRecord.NumberOfThisDisk == ushort.MaxValue || this.endOfCentralDirectoryRecord.OffsetOfStartOfCentralDirectoryWithRespectToTheStartingDiskNumber == uint.MaxValue || this.endOfCentralDirectoryRecord.NumberOfEntriesInTheCentralDirectory == ushort.MaxValue)
         {
             this.Reader.BaseStream.Seek(position - 16L, SeekOrigin.Begin);
             if (ZipHelper.SeekBackwardsToSignature(this.Reader.BaseStream, 117853008U))
             {
                 this.zip64EndOfCentralDirectoryLocator = new Zip64EndOfCentralDirectoryLocator();
                 if (!this.zip64EndOfCentralDirectoryLocator.TryReadBlock(this.archiveReader))
                 {
                     throw new InvalidDataException("ZIP64 End of central directory locator not found.");
                 }
                 if (this.zip64EndOfCentralDirectoryLocator.RelativeOffsetOfTheZip64EndOfCentralDirectoryRecord > (ulong)long.MaxValue)
                 {
                     throw new InvalidDataException("Relative offset of the Zip64 End Of Central Directory Record is too big.");
                 }
                 this.Reader.BaseStream.Seek((long)this.zip64EndOfCentralDirectoryLocator.RelativeOffsetOfTheZip64EndOfCentralDirectoryRecord, SeekOrigin.Begin);
                 this.zip64EndOfCentralDirectoryRecord = new Zip64EndOfCentralDirectoryRecord();
                 if (!this.zip64EndOfCentralDirectoryRecord.TryReadBlock(this.archiveReader))
                 {
                     throw new InvalidDataException("Zip64 End Of Central Directory Record not found.");
                 }
                 if (this.zip64EndOfCentralDirectoryRecord.NumberOfEntriesInTheCentralDirectory > (ulong)long.MaxValue)
                 {
                     throw new InvalidDataException("Number of entries is too big.");
                 }
                 if (this.zip64EndOfCentralDirectoryRecord.OffsetOfStartOfCentralDirectoryWithRespectToTheStartingDiskNumber > (ulong)long.MaxValue)
                 {
                     throw new InvalidDataException("Relative offset of the Central Directory Start is too big.");
                 }
                 if ((long)this.zip64EndOfCentralDirectoryRecord.NumberOfEntriesInTheCentralDirectory != (long)this.zip64EndOfCentralDirectoryRecord.NumberOfEntriesInTheCentralDirectoryOnThisDisk)
                 {
                     throw new InvalidDataException("Splitted archive is not supported.");
                 }
             }
         }
         if (this.CentralDirectoryStart > this.Reader.BaseStream.Length)
         {
             throw new InvalidDataException("Relative offset of the Central Directory Start is too big.");
         }
     }
     catch (EndOfStreamException ex)
     {
         throw new InvalidDataException("Archive corrupted", (Exception)ex);
     }
     catch (IOException ex)
     {
         throw new InvalidDataException("Archive corrupted", (Exception)ex);
     }
 }