Example #1
0
        //writes eocd, and if needed, zip 64 eocd, zip64 eocd locator
        //should only throw an exception in extremely exceptional cases because it is called from dispose
        private void WriteArchiveEpilogue(Int64 startOfCentralDirectory, Int64 sizeOfCentralDirectory)
        {
            //determine if we need Zip 64
            Boolean needZip64 = false;

            if (startOfCentralDirectory >= UInt32.MaxValue ||
                sizeOfCentralDirectory >= UInt32.MaxValue ||
                _entries.Count >= ZipHelper.Mask16Bit
#if DEBUG_FORCE_ZIP64
                || _forceZip64
#endif
                )
            {
                needZip64 = true;
            }

            //if we need zip 64, write zip 64 eocd and locator
            if (needZip64)
            {
                Int64 zip64EOCDRecordStart = _archiveStream.Position;

                Zip64EndOfCentralDirectoryRecord.WriteBlock(_archiveStream, _entries.Count, startOfCentralDirectory, sizeOfCentralDirectory);
                Zip64EndOfCentralDirectoryLocator.WriteBlock(_archiveStream, zip64EOCDRecordStart);
            }

            //write normal eocd
            ZipEndOfCentralDirectoryBlock.WriteBlock(_archiveStream, _entries.Count, startOfCentralDirectory, sizeOfCentralDirectory, _archiveComment);
        }
Example #2
0
        public static Boolean TryReadBlock(BinaryReader reader, out Zip64EndOfCentralDirectoryLocator zip64EOCDLocator)
        {
            zip64EOCDLocator = new Zip64EndOfCentralDirectoryLocator();

            if (reader.ReadUInt32() != SignatureConstant)
            {
                return(false);
            }

            zip64EOCDLocator.NumberOfDiskWithZip64EOCD = reader.ReadUInt32();
            zip64EOCDLocator.OffsetOfZip64EOCD         = reader.ReadUInt64();
            zip64EOCDLocator.TotalNumberOfDisks        = reader.ReadUInt32();
            return(true);
        }
Example #3
0
        //This function reads all the EOCD stuff it needs to find the offset to the start of the central directory
        //This offset gets put in _centralDirectoryStart and the number of this disk gets put in _numberOfThisDisk
        //Also does some verification that this isn't a split/spanned archive
        //Also checks that offset to CD isn't out of bounds
        private void ReadEndOfCentralDirectory()
        {
            try
            {
                //this seeks to the start of the end of central directory record
                _archiveStream.Seek(-ZipEndOfCentralDirectoryBlock.SizeOfBlockWithoutSignature, SeekOrigin.End);
                if (!ZipHelper.SeekBackwardsToSignature(_archiveStream, ZipEndOfCentralDirectoryBlock.SignatureConstant))
                {
                    throw new InvalidDataException(SR.EOCDNotFound);
                }

                Int64 eocdStart = _archiveStream.Position;

                //read the EOCD
                ZipEndOfCentralDirectoryBlock eocd;
                Boolean eocdProper = ZipEndOfCentralDirectoryBlock.TryReadBlock(_archiveReader, out eocd);
                Debug.Assert(eocdProper); //we just found this using the signature finder, so it should be okay

                if (eocd.NumberOfThisDisk != eocd.NumberOfTheDiskWithTheStartOfTheCentralDirectory)
                {
                    throw new InvalidDataException(SR.SplitSpanned);
                }

                _numberOfThisDisk      = eocd.NumberOfThisDisk;
                _centralDirectoryStart = eocd.OffsetOfStartOfCentralDirectoryWithRespectToTheStartingDiskNumber;
                if (eocd.NumberOfEntriesInTheCentralDirectory != eocd.NumberOfEntriesInTheCentralDirectoryOnThisDisk)
                {
                    throw new InvalidDataException(SR.SplitSpanned);
                }
                _expectedNumberOfEntries = eocd.NumberOfEntriesInTheCentralDirectory;

                //only bother saving the comment if we are in update mode
                if (_mode == ZipArchiveMode.Update)
                {
                    _archiveComment = eocd.ArchiveComment;
                }

                //only bother looking for zip64 EOCD stuff if we suspect it is needed because some value is FFFFFFFFF
                //because these are the only two values we need, we only worry about these
                //if we don't find the zip64 EOCD, we just give up and try to use the original values
                if (eocd.NumberOfThisDisk == ZipHelper.Mask16Bit ||
                    eocd.OffsetOfStartOfCentralDirectoryWithRespectToTheStartingDiskNumber == ZipHelper.Mask32Bit ||
                    eocd.NumberOfEntriesInTheCentralDirectory == ZipHelper.Mask16Bit)
                {
                    //we need to look for zip 64 EOCD stuff
                    //seek to the zip 64 EOCD locator
                    _archiveStream.Seek(eocdStart - Zip64EndOfCentralDirectoryLocator.SizeOfBlockWithoutSignature, SeekOrigin.Begin);
                    //if we don't find it, assume it doesn't exist and use data from normal eocd
                    if (ZipHelper.SeekBackwardsToSignature(_archiveStream, Zip64EndOfCentralDirectoryLocator.SignatureConstant))
                    {
                        //use locator to get to Zip64EOCD
                        Zip64EndOfCentralDirectoryLocator locator;
                        Boolean zip64eocdLocatorProper = Zip64EndOfCentralDirectoryLocator.TryReadBlock(_archiveReader, out locator);
                        Debug.Assert(zip64eocdLocatorProper); //we just found this using the signature finder, so it should be okay

                        if (locator.OffsetOfZip64EOCD > (UInt64)Int64.MaxValue)
                        {
                            throw new InvalidDataException(SR.FieldTooBigOffsetToZip64EOCD);
                        }
                        Int64 zip64EOCDOffset = (Int64)locator.OffsetOfZip64EOCD;

                        _archiveStream.Seek(zip64EOCDOffset, SeekOrigin.Begin);

                        //read Zip64EOCD
                        Zip64EndOfCentralDirectoryRecord record;
                        if (!Zip64EndOfCentralDirectoryRecord.TryReadBlock(_archiveReader, out record))
                        {
                            throw new InvalidDataException(SR.Zip64EOCDNotWhereExpected);
                        }

                        _numberOfThisDisk = record.NumberOfThisDisk;

                        if (record.NumberOfEntriesTotal > (UInt64)Int64.MaxValue)
                        {
                            throw new InvalidDataException(SR.FieldTooBigNumEntries);
                        }
                        if (record.OffsetOfCentralDirectory > (UInt64)Int64.MaxValue)
                        {
                            throw new InvalidDataException(SR.FieldTooBigOffsetToCD);
                        }
                        if (record.NumberOfEntriesTotal != record.NumberOfEntriesOnThisDisk)
                        {
                            throw new InvalidDataException(SR.SplitSpanned);
                        }

                        _expectedNumberOfEntries = (Int64)record.NumberOfEntriesTotal;
                        _centralDirectoryStart   = (Int64)record.OffsetOfCentralDirectory;
                    }
                }

                if (_centralDirectoryStart > _archiveStream.Length)
                {
                    throw new InvalidDataException(SR.FieldTooBigOffsetToCD);
                }
            }
            catch (EndOfStreamException ex)
            {
                throw new InvalidDataException(SR.CDCorrupt, ex);
            }
            catch (IOException ex)
            {
                throw new InvalidDataException(SR.CDCorrupt, ex);
            }
        }