コード例 #1
0
        public static Zip64ExtraField GetJustZip64Block(Stream extraFieldStream, bool readUncompressedSize, bool readCompressedSize, bool readLocalHeaderOffset, bool readStartDiskNumber)
        {
            using (BinaryReader reader = new BinaryReader(extraFieldStream))
            {
                ZipGenericExtraField field;

                while (ZipGenericExtraField.TryReadBlock(reader, extraFieldStream.Length, out field))
                {
                    Zip64ExtraField zip64Block;

                    if (Zip64ExtraField.TryGetZip64BlockFromGenericExtraField(field, readUncompressedSize, readCompressedSize, readLocalHeaderOffset, readStartDiskNumber, out zip64Block))
                    {
                        return(zip64Block);
                    }
                }
            }

            return(new Zip64ExtraField()
            {
                _compressedSize = new long?(),
                _uncompressedSize = new long?(),
                _localHeaderOffset = new long?(),
                _startDiskNumber = new int?()
            });
        }
コード例 #2
0
        public static List <ZipGenericExtraField> ParseExtraField(Stream extraFieldData)
        {
            List <ZipGenericExtraField> list = new List <ZipGenericExtraField>();

            using (BinaryReader reader = new BinaryReader(extraFieldData))
            {
                ZipGenericExtraField field;

                while (ZipGenericExtraField.TryReadBlock(reader, extraFieldData.Length, out field))
                {
                    list.Add(field);
                }
            }

            return(list);
        }
コード例 #3
0
        public static List <ZipGenericExtraField> GetExtraFields(BinaryReader reader)
        {
            reader.BaseStream.Seek(26L, SeekOrigin.Current);
            ushort num1 = reader.ReadUInt16();
            ushort num2 = reader.ReadUInt16();

            reader.BaseStream.Seek((long)num1, SeekOrigin.Current);
            List <ZipGenericExtraField> extraFields;

            using (Stream extraFieldData = (Stream) new SubReadStream(reader.BaseStream, reader.BaseStream.Position, (long)num2))
            {
                extraFields = ZipGenericExtraField.ParseExtraField(extraFieldData);
            }

            Zip64ExtraField.RemoveZip64Blocks(extraFields);
            return(extraFields);
        }
コード例 #4
0
        public static bool TryReadBlock(BinaryReader reader, long endExtraField, out ZipGenericExtraField field)
        {
            field = new ZipGenericExtraField();

            if (endExtraField - reader.BaseStream.Position < 4L)
            {
                return(false);
            }

            field._tag  = reader.ReadUInt16();
            field._size = reader.ReadUInt16();

            if (endExtraField - reader.BaseStream.Position < (long)field._size)
            {
                return(false);
            }

            field._data = reader.ReadBytes((int)field._size);

            return(true);
        }
コード例 #5
0
        private static bool TryGetZip64BlockFromGenericExtraField(ZipGenericExtraField extraField, bool readUncompressedSize, bool readCompressedSize, bool readLocalHeaderOffset, bool readStartDiskNumber, out Zip64ExtraField zip64Block)
        {
            zip64Block = new Zip64ExtraField();
            zip64Block._compressedSize    = new long?();
            zip64Block._uncompressedSize  = new long?();
            zip64Block._localHeaderOffset = new long?();
            zip64Block._startDiskNumber   = new int?();

            if ((int)extraField.Tag != 1)
            {
                return(false);
            }

            MemoryStream memoryStream = (MemoryStream)null;

            try
            {
                memoryStream = new MemoryStream(extraField.Data);
                using (BinaryReader binaryReader = new BinaryReader((Stream)memoryStream))
                {
                    memoryStream     = (MemoryStream)null;
                    zip64Block._size = extraField.Size;
                    ushort num = (ushort)0;

                    if (readUncompressedSize)
                    {
                        num += (ushort)8;
                    }

                    if (readCompressedSize)
                    {
                        num += (ushort)8;
                    }

                    if (readLocalHeaderOffset)
                    {
                        num += (ushort)8;
                    }

                    if (readStartDiskNumber)
                    {
                        num += (ushort)4;
                    }

                    if ((int)num != (int)zip64Block._size)
                    {
                        return(false);
                    }

                    if (readUncompressedSize)
                    {
                        zip64Block._uncompressedSize = new long?(binaryReader.ReadInt64());
                    }

                    if (readCompressedSize)
                    {
                        zip64Block._compressedSize = new long?(binaryReader.ReadInt64());
                    }

                    if (readLocalHeaderOffset)
                    {
                        zip64Block._localHeaderOffset = new long?(binaryReader.ReadInt64());
                    }

                    if (readStartDiskNumber)
                    {
                        zip64Block._startDiskNumber = new int?(binaryReader.ReadInt32());
                    }

                    long?nullable1 = zip64Block._uncompressedSize;

                    if ((nullable1.GetValueOrDefault() >= 0L ? 0 : (nullable1.HasValue ? 1 : 0)) != 0)
                    {
                        throw new InvalidDataException(CompressionConstants.FieldTooBigUncompressedSize);
                    }

                    long?nullable2 = zip64Block._compressedSize;

                    if ((nullable2.GetValueOrDefault() >= 0L ? 0 : (nullable2.HasValue ? 1 : 0)) != 0)
                    {
                        throw new InvalidDataException(CompressionConstants.FieldTooBigCompressedSize);
                    }

                    long?nullable3 = zip64Block._localHeaderOffset;

                    if ((nullable3.GetValueOrDefault() >= 0L ? 0 : (nullable3.HasValue ? 1 : 0)) != 0)
                    {
                        throw new InvalidDataException(CompressionConstants.FieldTooBigLocalHeaderOffset);
                    }

                    int?nullable4 = zip64Block._startDiskNumber;

                    if ((nullable4.GetValueOrDefault() >= 0 ? 0 : (nullable4.HasValue ? 1 : 0)) != 0)
                    {
                        throw new InvalidDataException(CompressionConstants.FieldTooBigStartDiskNumber);
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
            finally
            {
                if (memoryStream != null)
                {
                    memoryStream.Close();
                }
            }
        }
コード例 #6
0
        public static bool TryReadBlock(BinaryReader reader, bool saveExtraFieldsAndComments, out ZipCentralDirectoryFileHeader header)
        {
            header = new ZipCentralDirectoryFileHeader();

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

            header.VersionMadeBy          = reader.ReadUInt16();
            header.VersionNeededToExtract = reader.ReadUInt16();
            header.GeneralPurposeBitFlag  = reader.ReadUInt16();
            header.CompressionMethod      = reader.ReadUInt16();
            header.LastModified           = reader.ReadUInt32();
            header.Crc32 = reader.ReadUInt32();
            uint num  = reader.ReadUInt32();
            uint num2 = reader.ReadUInt32();

            header.FilenameLength    = reader.ReadUInt16();
            header.ExtraFieldLength  = reader.ReadUInt16();
            header.FileCommentLength = reader.ReadUInt16();
            ushort num3 = reader.ReadUInt16();

            header.InternalFileAttributes = reader.ReadUInt16();
            header.ExternalFileAttributes = reader.ReadUInt32();
            uint num4 = reader.ReadUInt32();

            header.Filename = reader.ReadBytes((int)header.FilenameLength);
            bool            readUncompressedSize  = num2 == 4294967295u;
            bool            readCompressedSize    = num == 4294967295u;
            bool            readLocalHeaderOffset = num4 == 4294967295u;
            bool            readStartDiskNumber   = num3 == 65535;
            long            position = reader.BaseStream.Position + (long)((ulong)header.ExtraFieldLength);
            Zip64ExtraField zip64ExtraField;

            using (Stream stream = new SubReadStream(reader.BaseStream, reader.BaseStream.Position, (long)((ulong)header.ExtraFieldLength)))
            {
                if (saveExtraFieldsAndComments)
                {
                    header.ExtraFields = ZipGenericExtraField.ParseExtraField(stream);
                    zip64ExtraField    = Zip64ExtraField.GetAndRemoveZip64Block(header.ExtraFields, readUncompressedSize, readCompressedSize, readLocalHeaderOffset, readStartDiskNumber);
                }
                else
                {
                    header.ExtraFields = null;
                    zip64ExtraField    = Zip64ExtraField.GetJustZip64Block(stream, readUncompressedSize, readCompressedSize, readLocalHeaderOffset, readStartDiskNumber);
                }
            }

            ZipHelper.AdvanceToPosition(reader.BaseStream, position);

            if (saveExtraFieldsAndComments)
            {
                header.FileComment = reader.ReadBytes((int)header.FileCommentLength);
            }
            else
            {
                reader.BaseStream.Position += (long)((ulong)header.FileCommentLength);
                header.FileComment          = null;
            }

            header.UncompressedSize            = (long)((!zip64ExtraField.UncompressedSize.HasValue) ? ((ulong)num2) : ((ulong)zip64ExtraField.UncompressedSize.Value));
            header.CompressedSize              = (long)((!zip64ExtraField.CompressedSize.HasValue) ? ((ulong)num) : ((ulong)zip64ExtraField.CompressedSize.Value));
            header.RelativeOffsetOfLocalHeader = (long)((!zip64ExtraField.LocalHeaderOffset.HasValue) ? ((ulong)num4) : ((ulong)zip64ExtraField.LocalHeaderOffset.Value));
            header.DiskNumberStart             = ((!zip64ExtraField.StartDiskNumber.HasValue) ? ((int)num3) : zip64ExtraField.StartDiskNumber.Value);

            return(true);
        }
コード例 #7
0
        private bool WriteLocalFileHeader(bool isEmptyFile)
        {
            BinaryWriter    binaryWriter    = new BinaryWriter(this._archive.ArchiveStream);
            Zip64ExtraField zip64ExtraField = new Zip64ExtraField();
            bool            flag            = false;
            uint            num1;
            uint            num2;

            if (isEmptyFile)
            {
                this.CompressionMethod = ZipArchiveEntry.CompressionMethodValues.Stored;
                num1 = 0U;
                num2 = 0U;
            }
            else if (this._archive.Mode == ZipArchiveMode.Create && !this._archive.ArchiveStream.CanSeek && !isEmptyFile)
            {
                this._generalPurposeBitFlag = this._generalPurposeBitFlag | ZipArchiveEntry.BitFlagValues.DataDescriptor;
                flag = false;
                num1 = 0U;
                num2 = 0U;
            }
            else if (this.SizesTooLarge())
            {
                flag = true;
                num1 = uint.MaxValue;
                num2 = uint.MaxValue;
                zip64ExtraField.CompressedSize   = new long?(this._compressedSize);
                zip64ExtraField.UncompressedSize = new long?(this._uncompressedSize);
                this.VersionToExtractAtLeast(ZipVersionNeededValues.Zip64);
            }
            else
            {
                flag = false;
                num1 = (uint)this._compressedSize;
                num2 = (uint)this._uncompressedSize;
            }

            this._offsetOfLocalHeader = binaryWriter.BaseStream.Position;
            int    num3 = (flag ? (int)zip64ExtraField.TotalSize : 0) + (this._lhUnknownExtraFields != null ? ZipGenericExtraField.TotalSize(this._lhUnknownExtraFields) : 0);
            ushort num4;

            if (num3 > (int)ushort.MaxValue)
            {
                num4 = flag ? zip64ExtraField.TotalSize : (ushort)0;
                this._lhUnknownExtraFields = null;
            }
            else
            {
                num4 = (ushort)num3;
            }

            binaryWriter.Write(67324752U);
            binaryWriter.Write((ushort)this._versionToExtract);
            binaryWriter.Write((ushort)this._generalPurposeBitFlag);
            binaryWriter.Write((ushort)this.CompressionMethod);
            binaryWriter.Write(ZipHelper.DateTimeToDosTime(this._lastModified.DateTime));
            binaryWriter.Write(this._crc32);
            binaryWriter.Write(num1);
            binaryWriter.Write(num2);
            binaryWriter.Write((ushort)this._storedEntryNameBytes.Length);
            binaryWriter.Write(num4);
            binaryWriter.Write(this._storedEntryNameBytes);

            if (flag)
            {
                zip64ExtraField.WriteBlock(this._archive.ArchiveStream);
            }

            if (this._lhUnknownExtraFields != null)
            {
                ZipGenericExtraField.WriteAllBlocks(this._lhUnknownExtraFields, this._archive.ArchiveStream);
            }

            return(flag);
        }
コード例 #8
0
        internal void WriteCentralDirectoryFileHeader()
        {
            BinaryWriter    binaryWriter    = new BinaryWriter(this._archive.ArchiveStream);
            Zip64ExtraField zip64ExtraField = new Zip64ExtraField();
            bool            flag            = false;
            uint            num1;
            uint            num2;

            if (this.SizesTooLarge())
            {
                flag = true;
                num1 = uint.MaxValue;
                num2 = uint.MaxValue;
                zip64ExtraField.CompressedSize   = new long?(this._compressedSize);
                zip64ExtraField.UncompressedSize = new long?(this._uncompressedSize);
            }
            else
            {
                num1 = (uint)this._compressedSize;
                num2 = (uint)this._uncompressedSize;
            }

            uint num3;

            if (this._offsetOfLocalHeader > (long)uint.MaxValue)
            {
                flag = true;
                num3 = uint.MaxValue;
                zip64ExtraField.LocalHeaderOffset = new long?(this._offsetOfLocalHeader);
            }
            else
            {
                num3 = (uint)this._offsetOfLocalHeader;
            }

            if (flag)
            {
                this.VersionToExtractAtLeast(ZipVersionNeededValues.Zip64);
            }

            int    num4 = (flag ? (int)zip64ExtraField.TotalSize : 0) + (this._cdUnknownExtraFields != null ? ZipGenericExtraField.TotalSize(this._cdUnknownExtraFields) : 0);
            ushort num5;

            if (num4 > (int)ushort.MaxValue)
            {
                num5 = flag ? zip64ExtraField.TotalSize : (ushort)0;
                this._cdUnknownExtraFields = null;
            }
            else
            {
                num5 = (ushort)num4;
            }

            binaryWriter.Write(33639248U);
            binaryWriter.Write((ushort)this._versionToExtract);
            binaryWriter.Write((ushort)this._versionToExtract);
            binaryWriter.Write((ushort)this._generalPurposeBitFlag);
            binaryWriter.Write((ushort)this.CompressionMethod);
            binaryWriter.Write(ZipHelper.DateTimeToDosTime(this._lastModified.DateTime));
            binaryWriter.Write(this._crc32);
            binaryWriter.Write(num1);
            binaryWriter.Write(num2);
            binaryWriter.Write((ushort)this._storedEntryNameBytes.Length);
            binaryWriter.Write(num5);
            binaryWriter.Write(this._fileComment != null ? (ushort)this._fileComment.Length : (ushort)0);
            binaryWriter.Write((ushort)0);
            binaryWriter.Write((ushort)0);
            binaryWriter.Write((uint)this._externalFileAttributes);
            binaryWriter.Write(num3);
            binaryWriter.Write(this._storedEntryNameBytes);

            if (flag)
            {
                zip64ExtraField.WriteBlock(this._archive.ArchiveStream);
            }

            if (this._cdUnknownExtraFields != null)
            {
                ZipGenericExtraField.WriteAllBlocks(this._cdUnknownExtraFields, this._archive.ArchiveStream);
            }

            if (this._fileComment != null)
            {
                binaryWriter.Write(this._fileComment);
            }
        }
コード例 #9
0
        public static bool TryReadBlock(BinaryReader reader, bool saveExtraFieldsAndComments, out ZipCentralDirectoryFileHeader header)
        {
            header = new ZipCentralDirectoryFileHeader();

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

            header.VersionMadeBy          = reader.ReadUInt16();
            header.VersionNeededToExtract = reader.ReadUInt16();
            header.GeneralPurposeBitFlag  = reader.ReadUInt16();
            header.CompressionMethod      = reader.ReadUInt16();
            header.LastModified           = reader.ReadUInt32();
            header.Crc32 = reader.ReadUInt32();
            uint num1 = reader.ReadUInt32();
            uint num2 = reader.ReadUInt32();

            header.FilenameLength    = reader.ReadUInt16();
            header.ExtraFieldLength  = reader.ReadUInt16();
            header.FileCommentLength = reader.ReadUInt16();
            ushort num3 = reader.ReadUInt16();

            header.InternalFileAttributes = reader.ReadUInt16();
            header.ExternalFileAttributes = reader.ReadUInt32();
            uint num4 = reader.ReadUInt32();

            header.FileName = reader.ReadBytes((int)header.FilenameLength);
            bool            readUncompressedSize  = (int)num2 == -1;
            bool            readCompressedSize    = (int)num1 == -1;
            bool            readLocalHeaderOffset = (int)num4 == -1;
            bool            readStartDiskNumber   = (int)num3 == (int)ushort.MaxValue;
            Zip64ExtraField zip64ExtraField;

            using (Stream stream = (Stream) new SubReadStream(reader.BaseStream, reader.BaseStream.Position, (long)header.ExtraFieldLength))
            {
                if (saveExtraFieldsAndComments)
                {
                    header.ExtraFields = ZipGenericExtraField.ParseExtraField(stream);
                    zip64ExtraField    = Zip64ExtraField.GetAndRemoveZip64Block(header.ExtraFields, readUncompressedSize, readCompressedSize, readLocalHeaderOffset, readStartDiskNumber);
                }
                else
                {
                    header.ExtraFields = (List <ZipGenericExtraField>)null;
                    zip64ExtraField    = Zip64ExtraField.GetJustZip64Block(stream, readUncompressedSize, readCompressedSize, readLocalHeaderOffset, readStartDiskNumber);
                }
            }

            if (saveExtraFieldsAndComments)
            {
                header.FileComment = reader.ReadBytes((int)header.FileCommentLength);
            }
            else
            {
                reader.BaseStream.Position += (long)header.FileCommentLength;
                header.FileComment          = (byte[])null;
            }

            header.UncompressedSize            = !zip64ExtraField.UncompressedSize.HasValue ? (long)num2 : zip64ExtraField.UncompressedSize.Value;
            header.CompressedSize              = !zip64ExtraField.CompressedSize.HasValue ? (long)num1 : zip64ExtraField.CompressedSize.Value;
            header.RelativeOffsetOfLocalHeader = !zip64ExtraField.LocalHeaderOffset.HasValue ? (long)num4 : zip64ExtraField.LocalHeaderOffset.Value;
            header.DiskNumberStart             = !zip64ExtraField.StartDiskNumber.HasValue ? (int)num3 : zip64ExtraField.StartDiskNumber.Value;
            return(true);
        }