Exemple #1
0
        internal static ZipIOLocalFileHeader ParseRecord(BinaryReader reader, Encoding encoding)
        {
            ZipIOLocalFileHeader header = new ZipIOLocalFileHeader();

            header._signature = reader.ReadUInt32();
            header._versionNeededToExtract = reader.ReadUInt16();
            header._generalPurposeBitFlag  = reader.ReadUInt16();
            header._compressionMethod      = reader.ReadUInt16();
            header._lastModFileDateTime    = reader.ReadUInt32();
            header._crc32            = reader.ReadUInt32();
            header._compressedSize   = reader.ReadUInt32();
            header._uncompressedSize = reader.ReadUInt32();
            header._fileNameLength   = reader.ReadUInt16();
            header._extraFieldLength = reader.ReadUInt16();
            header._fileName         = reader.ReadBytes(header._fileNameLength);
            ZipIOZip64ExtraFieldUsage none = ZipIOZip64ExtraFieldUsage.None;

            if (header._versionNeededToExtract >= 0x2d)
            {
                if (header._compressedSize == uint.MaxValue)
                {
                    none |= ZipIOZip64ExtraFieldUsage.CompressedSize;
                }
                if (header._uncompressedSize == uint.MaxValue)
                {
                    none |= ZipIOZip64ExtraFieldUsage.UncompressedSize;
                }
            }
            header._extraField     = ZipIOExtraField.ParseRecord(reader, none, header._extraFieldLength);
            header._stringFileName = ZipIOBlockManager.ValidateNormalizeFileName(encoding.GetString(header._fileName));
            header.Validate();
            return(header);
        }
Exemple #2
0
        internal static ZipIOLocalFileHeader CreateNew(string fileName, Encoding encoding, CompressionMethodEnum compressionMethod, DeflateOptionEnum deflateOption, bool streaming)
        {
            byte[] bytes = encoding.GetBytes(fileName);
            if (bytes.Length > ZipIOBlockManager.MaxFileNameSize)
            {
                throw new ArgumentOutOfRangeException("fileName");
            }
            ZipIOLocalFileHeader header = new ZipIOLocalFileHeader();

            header._signature         = 0x4034b50;
            header._compressionMethod = (ushort)compressionMethod;
            if (streaming)
            {
                header._versionNeededToExtract = 0x2d;
            }
            else
            {
                header._versionNeededToExtract = (ushort)ZipIOBlockManager.CalcVersionNeededToExtractFromCompression(compressionMethod);
            }
            if (compressionMethod != CompressionMethodEnum.Stored)
            {
                header.DeflateOption = deflateOption;
            }
            if (streaming)
            {
                header.StreamingCreationFlag = true;
            }
            header._lastModFileDateTime = ZipIOBlockManager.ToMsDosDateTime(DateTime.Now);
            header._fileNameLength      = (ushort)bytes.Length;
            header._fileName            = bytes;
            header._extraField          = ZipIOExtraField.CreateNew(!streaming);
            header._extraFieldLength    = header._extraField.Size;
            header._stringFileName      = fileName;
            return(header);
        }
Exemple #3
0
        internal static ZipIOExtraField ParseRecord(BinaryReader reader, ZipIOZip64ExtraFieldUsage zip64extraFieldUsage, ushort expectedExtraFieldSize)
        {
            if (expectedExtraFieldSize == 0)
            {
                if (zip64extraFieldUsage != ZipIOZip64ExtraFieldUsage.None)
                {
                    throw new FileFormatException(SR.Get("CorruptedData"));
                }
                return(CreateNew(false));
            }
            ZipIOExtraField field = new ZipIOExtraField();

            while (expectedExtraFieldSize > 0)
            {
                if (expectedExtraFieldSize < ZipIOExtraFieldElement.MinimumSize)
                {
                    throw new FileFormatException(SR.Get("CorruptedData"));
                }
                ZipIOExtraFieldElement        element  = ZipIOExtraFieldElement.Parse(reader, zip64extraFieldUsage);
                ZipIOExtraFieldZip64Element   element3 = element as ZipIOExtraFieldZip64Element;
                ZipIOExtraFieldPaddingElement element2 = element as ZipIOExtraFieldPaddingElement;
                if (element3 != null)
                {
                    if (field._zip64Element != null)
                    {
                        throw new FileFormatException(SR.Get("CorruptedData"));
                    }
                    field._zip64Element = element3;
                }
                else if (element2 != null)
                {
                    if (field._paddingElement != null)
                    {
                        throw new FileFormatException(SR.Get("CorruptedData"));
                    }
                    field._paddingElement = element2;
                }
                else
                {
                    if (field._extraFieldElements == null)
                    {
                        field._extraFieldElements = new ArrayList(3);
                    }
                    field._extraFieldElements.Add(element);
                }
                expectedExtraFieldSize = (ushort)(expectedExtraFieldSize - element.Size);
            }
            if (expectedExtraFieldSize != 0)
            {
                throw new FileFormatException(SR.Get("CorruptedData"));
            }
            if (field._zip64Element == null)
            {
                field._zip64Element = ZipIOExtraFieldZip64Element.CreateNew();
            }
            return(field);
        }
Exemple #4
0
        internal static ZipIOExtraField CreateNew(bool createPadding)
        {
            ZipIOExtraField field = new ZipIOExtraField();

            field._zip64Element = ZipIOExtraFieldZip64Element.CreateNew();
            if (createPadding)
            {
                field._paddingElement = ZipIOExtraFieldPaddingElement.CreateNew();
            }
            return(field);
        }
        internal static ZipIOCentralDirectoryFileHeader CreateNew(Encoding encoding, ZipIOLocalFileBlock fileBlock)
        {
            ZipIOCentralDirectoryFileHeader header = new ZipIOCentralDirectoryFileHeader(encoding);

            header._fileCommentLength      = 0;
            header._fileComment            = null;
            header._diskNumberStart        = 0;
            header._internalFileAttributes = 0;
            header._externalFileAttributes = 0;
            header._versionMadeBy          = 0x2d;
            header._extraField             = ZipIOExtraField.CreateNew(false);
            header.UpdateFromLocalFileBlock(fileBlock);
            return(header);
        }
        internal static ZipIOCentralDirectoryFileHeader ParseRecord(BinaryReader reader, Encoding encoding)
        {
            ZipIOCentralDirectoryFileHeader header = new ZipIOCentralDirectoryFileHeader(encoding);

            header._signature              = reader.ReadUInt32();
            header._versionMadeBy          = reader.ReadUInt16();
            header._versionNeededToExtract = reader.ReadUInt16();
            header._generalPurposeBitFlag  = reader.ReadUInt16();
            header._compressionMethod      = reader.ReadUInt16();
            header._lastModFileDateTime    = reader.ReadUInt32();
            header._crc32                       = reader.ReadUInt32();
            header._compressedSize              = reader.ReadUInt32();
            header._uncompressedSize            = reader.ReadUInt32();
            header._fileNameLength              = reader.ReadUInt16();
            header._extraFieldLength            = reader.ReadUInt16();
            header._fileCommentLength           = reader.ReadUInt16();
            header._diskNumberStart             = reader.ReadUInt16();
            header._internalFileAttributes      = reader.ReadUInt16();
            header._externalFileAttributes      = reader.ReadUInt32();
            header._relativeOffsetOfLocalHeader = reader.ReadUInt32();
            header._fileName                    = reader.ReadBytes(header._fileNameLength);
            ZipIOZip64ExtraFieldUsage none = ZipIOZip64ExtraFieldUsage.None;

            if (header._versionNeededToExtract >= 0x2d)
            {
                if (header._compressedSize == uint.MaxValue)
                {
                    none |= ZipIOZip64ExtraFieldUsage.CompressedSize;
                }
                if (header._uncompressedSize == uint.MaxValue)
                {
                    none |= ZipIOZip64ExtraFieldUsage.UncompressedSize;
                }
                if (header._relativeOffsetOfLocalHeader == uint.MaxValue)
                {
                    none |= ZipIOZip64ExtraFieldUsage.OffsetOfLocalHeader;
                }
                if (header._diskNumberStart == 0xffff)
                {
                    none |= ZipIOZip64ExtraFieldUsage.DiskNumber;
                }
            }
            header._extraField     = ZipIOExtraField.ParseRecord(reader, none, header._extraFieldLength);
            header._fileComment    = reader.ReadBytes(header._fileCommentLength);
            header._stringFileName = ZipIOBlockManager.ValidateNormalizeFileName(encoding.GetString(header._fileName));
            header.Validate();
            return(header);
        }