Пример #1
0
        private bool WriteLocalFileHeader(bool isEmptyFile)
        {
            uint            maxValue;
            uint            num2;
            ushort          num4;
            BinaryWriter    writer = new BinaryWriter(_archive.ArchiveStream);
            Zip64ExtraField field  = new Zip64ExtraField();
            bool            flag   = false;

            if (isEmptyFile)
            {
                CompressionMethod = CompressionMethodValues.Stored;
                maxValue          = 0;
                num2 = 0;
            }
            else if (((_archive.Mode == ZipArchiveMode.Create) && !_archive.ArchiveStream.CanSeek) && !isEmptyFile)
            {
                _generalPurposeBitFlag |= BitFlagValues.DataDescriptor;
                flag     = false;
                maxValue = 0;
                num2     = 0;
            }
            else if (SizesTooLarge())
            {
                flag                   = true;
                maxValue               = uint.MaxValue;
                num2                   = uint.MaxValue;
                field.CompressedSize   = new long?(_compressedSize);
                field.UncompressedSize = new long?(_uncompressedSize);
                VersionToExtractAtLeast(ZipVersionNeededValues.Zip64);
            }
            else
            {
                flag     = false;
                maxValue = (uint)_compressedSize;
                num2     = (uint)_uncompressedSize;
            }
            _offsetOfLocalHeader = writer.BaseStream.Position;
            int num3 = (flag ? field.TotalSize : 0) + ((_lhUnknownExtraFields != null) ? ZipGenericExtraField.TotalSize(_lhUnknownExtraFields) : 0);

            if (num3 > 0xffff)
            {
                num4 = flag ? field.TotalSize : ((ushort)0);
                _lhUnknownExtraFields = null;
            }
            else
            {
                num4 = (ushort)num3;
            }
            writer.Write((uint)0x4034b50);
            writer.Write((ushort)_versionToExtract);
            writer.Write((ushort)_generalPurposeBitFlag);
            writer.Write((ushort)CompressionMethod);
            writer.Write(ZipHelper.DateTimeToDosTime(_lastModified.DateTime));
            writer.Write(_crc32);
            writer.Write(maxValue);
            writer.Write(num2);
            writer.Write((ushort)_storedEntryNameBytes.Length);
            writer.Write(num4);
            writer.Write(_storedEntryNameBytes);
            if (flag)
            {
                field.WriteBlock(_archive.ArchiveStream);
            }
            if (_lhUnknownExtraFields != null)
            {
                ZipGenericExtraField.WriteAllBlocks(_lhUnknownExtraFields, _archive.ArchiveStream);
            }
            return(flag);
        }
Пример #2
0
 private void ReadEndOfCentralDirectory()
 {
     try
     {
         ZipEndOfCentralDirectoryBlock block;
         ArchiveStream1.Seek(-18L, SeekOrigin.End);
         if (!ZipHelper.SeekBackwardsToSignature(ArchiveStream1, 0x6054b50))
         {
             throw new InvalidDataException(Messages.EOCDNotFound);
         }
         long position = ArchiveStream1.Position;
         ZipEndOfCentralDirectoryBlock.TryReadBlock(_archiveReader, out block);
         if (block.NumberOfThisDisk != block.NumberOfTheDiskWithTheStartOfTheCentralDirectory)
         {
             throw new InvalidDataException(Messages.SplitSpanned);
         }
         _numberOfThisDisk      = block.NumberOfThisDisk;
         _centralDirectoryStart = block.OffsetOfStartOfCentralDirectoryWithRespectToTheStartingDiskNumber;
         if (block.NumberOfEntriesInTheCentralDirectory != block.NumberOfEntriesInTheCentralDirectoryOnThisDisk)
         {
             throw new InvalidDataException(Messages.SplitSpanned);
         }
         _expectedNumberOfEntries = block.NumberOfEntriesInTheCentralDirectory;
         if (_mode == ZipArchiveMode.Update)
         {
             _archiveComment = block.ArchiveComment;
         }
         if (((block.NumberOfThisDisk == 0xffff) || (block.OffsetOfStartOfCentralDirectoryWithRespectToTheStartingDiskNumber == uint.MaxValue)) || (block.NumberOfEntriesInTheCentralDirectory == 0xffff))
         {
             ArchiveStream1.Seek(position - 0x10L, SeekOrigin.Begin);
             if (ZipHelper.SeekBackwardsToSignature(ArchiveStream1, 0x7064b50))
             {
                 Zip64EndOfCentralDirectoryLocator locator;
                 Zip64EndOfCentralDirectoryRecord  record;
                 Zip64EndOfCentralDirectoryLocator.TryReadBlock(_archiveReader, out locator);
                 if (locator.OffsetOfZip64EOCD > 0x7fffffffffffffffL)
                 {
                     throw new InvalidDataException(Messages.FieldTooBigOffsetToZip64EOCD);
                 }
                 long offset = (long)locator.OffsetOfZip64EOCD;
                 ArchiveStream1.Seek(offset, SeekOrigin.Begin);
                 if (!Zip64EndOfCentralDirectoryRecord.TryReadBlock(_archiveReader, out record))
                 {
                     throw new InvalidDataException(Messages.Zip64EOCDNotWhereExpected);
                 }
                 _numberOfThisDisk = record.NumberOfThisDisk;
                 if (record.NumberOfEntriesTotal > 0x7fffffffffffffffL)
                 {
                     throw new InvalidDataException(Messages.FieldTooBigNumEntries);
                 }
                 if (record.OffsetOfCentralDirectory > 0x7fffffffffffffffL)
                 {
                     throw new InvalidDataException(Messages.FieldTooBigOffsetToCD);
                 }
                 if (record.NumberOfEntriesTotal != record.NumberOfEntriesOnThisDisk)
                 {
                     throw new InvalidDataException(Messages.SplitSpanned);
                 }
                 _expectedNumberOfEntries = (long)record.NumberOfEntriesTotal;
                 _centralDirectoryStart   = (long)record.OffsetOfCentralDirectory;
             }
         }
         if (_centralDirectoryStart > ArchiveStream1.Length)
         {
             throw new InvalidDataException(Messages.FieldTooBigOffsetToCD);
         }
     }
     catch (EndOfStreamException exception)
     {
         throw new InvalidDataException(Messages.CDCorrupt, exception);
     }
     catch (IOException exception2)
     {
         throw new InvalidDataException(Messages.CDCorrupt, exception2);
     }
 }
Пример #3
0
        internal void WriteCentralDirectoryFileHeader()
        {
            uint            maxValue;
            uint            num2;
            uint            num3;
            ushort          num5;
            BinaryWriter    writer = new BinaryWriter(_archive.ArchiveStream);
            Zip64ExtraField field  = new Zip64ExtraField();
            bool            flag   = false;

            if (SizesTooLarge())
            {
                flag                   = true;
                maxValue               = uint.MaxValue;
                num2                   = uint.MaxValue;
                field.CompressedSize   = new long?(_compressedSize);
                field.UncompressedSize = new long?(_uncompressedSize);
            }
            else
            {
                maxValue = (uint)_compressedSize;
                num2     = (uint)_uncompressedSize;
            }
            if (_offsetOfLocalHeader > 0xffffffffL)
            {
                flag = true;
                num3 = uint.MaxValue;
                field.LocalHeaderOffset = new long?(_offsetOfLocalHeader);
            }
            else
            {
                num3 = (uint)_offsetOfLocalHeader;
            }
            if (flag)
            {
                VersionToExtractAtLeast(ZipVersionNeededValues.Zip64);
            }
            int num4 = (flag ? field.TotalSize : 0) + ((_cdUnknownExtraFields != null) ? ZipGenericExtraField.TotalSize(_cdUnknownExtraFields) : 0);

            if (num4 > 0xffff)
            {
                num5 = flag ? field.TotalSize : ((ushort)0);
                _cdUnknownExtraFields = null;
            }
            else
            {
                num5 = (ushort)num4;
            }
            writer.Write((uint)0x2014b50);
            writer.Write((ushort)_versionToExtract);
            writer.Write((ushort)_versionToExtract);
            writer.Write((ushort)_generalPurposeBitFlag);
            writer.Write((ushort)CompressionMethod);
            writer.Write(ZipHelper.DateTimeToDosTime(_lastModified.DateTime));
            writer.Write(_crc32);
            writer.Write(maxValue);
            writer.Write(num2);
            writer.Write((ushort)_storedEntryNameBytes.Length);
            writer.Write(num5);
            writer.Write((_fileComment != null) ? ((ushort)_fileComment.Length) : ((ushort)0));
            writer.Write((ushort)0);
            writer.Write((ushort)0);
            writer.Write((uint)0);
            writer.Write(num3);
            writer.Write(_storedEntryNameBytes);
            if (flag)
            {
                field.WriteBlock(_archive.ArchiveStream);
            }
            if (_cdUnknownExtraFields != null)
            {
                ZipGenericExtraField.WriteAllBlocks(_cdUnknownExtraFields, _archive.ArchiveStream);
            }
            if (_fileComment != null)
            {
                writer.Write(_fileComment);
            }
        }