internal void AddFileBlock(ZipIOLocalFileBlock fileBlock)
        {
            this._dirtyFlag = true;
            ZipIOCentralDirectoryFileHeader header = ZipIOCentralDirectoryFileHeader.CreateNew(this._blockManager.Encoding, fileBlock);

            this.CentralDirectoryDictionary.Add(header.FileName, header);
        }
 public void UpdateReferences(bool closingFlag)
 {
     foreach (IZipIOBlock block3 in (IEnumerable)this._blockManager)
     {
         ZipIOLocalFileBlock   fileBlock = block3 as ZipIOLocalFileBlock;
         ZipIORawDataFileBlock block     = block3 as ZipIORawDataFileBlock;
         if (fileBlock != null)
         {
             ZipIOCentralDirectoryFileHeader header2 = (ZipIOCentralDirectoryFileHeader)this.CentralDirectoryDictionary[fileBlock.FileName];
             if (header2.UpdateIfNeeded(fileBlock))
             {
                 this._dirtyFlag = true;
             }
         }
         else if (block != null)
         {
             long diskImageShift = block.DiskImageShift;
             if (diskImageShift != 0L)
             {
                 foreach (ZipIOCentralDirectoryFileHeader header in this.CentralDirectoryDictionary.Values)
                 {
                     if (block.DiskImageContains(header.OffsetOfLocalHeader))
                     {
                         header.MoveReference(diskImageShift);
                         this._dirtyFlag = true;
                     }
                 }
                 continue;
             }
         }
     }
 }
Example #3
0
        internal static ZipIOLocalFileBlock SeekableLoad(ZipIOBlockManager blockManager, string fileName)
        {
            ZipIOCentralDirectoryBlock      centralDirectoryBlock      = blockManager.CentralDirectoryBlock;
            ZipIOCentralDirectoryFileHeader centralDirectoryFileHeader = centralDirectoryBlock.GetCentralDirectoryFileHeader(fileName);
            long offsetOfLocalHeader = centralDirectoryFileHeader.OffsetOfLocalHeader;
            bool folderFlag          = centralDirectoryFileHeader.FolderFlag;
            bool volumeLabelFlag     = centralDirectoryFileHeader.VolumeLabelFlag;

            blockManager.Stream.Seek(offsetOfLocalHeader, SeekOrigin.Begin);
            ZipIOLocalFileBlock block = new ZipIOLocalFileBlock(blockManager, folderFlag, volumeLabelFlag);

            block.ParseRecord(blockManager.BinaryReader, fileName, offsetOfLocalHeader, centralDirectoryBlock, centralDirectoryFileHeader);
            return(block);
        }
        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);
        }
            // Methods
            int IComparer.Compare(object o1, object o2)
            {
                ZipIOCentralDirectoryFileHeader header2 = o1 as ZipIOCentralDirectoryFileHeader;
                ZipIOCentralDirectoryFileHeader header  = o2 as ZipIOCentralDirectoryFileHeader;

                if (header2.OffsetOfLocalHeader > header.OffsetOfLocalHeader)
                {
                    return(1);
                }
                if (header2.OffsetOfLocalHeader < header.OffsetOfLocalHeader)
                {
                    return(-1);
                }
                return(0);
            }
        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);
        }
 private void ParseRecord(BinaryReader reader, long centralDirectoryOffset, int centralDirectoryCount, long expectedCentralDirectorySize)
 {
     if (centralDirectoryCount > 0)
     {
         SortedList list = new SortedList(centralDirectoryCount);
         for (int i = 0; i < centralDirectoryCount; i++)
         {
             ZipIOCentralDirectoryFileHeader header2 = ZipIOCentralDirectoryFileHeader.ParseRecord(reader, this._blockManager.Encoding);
             list.Add(header2.OffsetOfLocalHeader, header2);
         }
         if ((reader.BaseStream.Position - centralDirectoryOffset) > expectedCentralDirectorySize)
         {
             throw new FileFormatException(SR.Get("CorruptedData"));
         }
         foreach (ZipIOCentralDirectoryFileHeader header in list.Values)
         {
             this.CentralDirectoryDictionary.Add(header.FileName, header);
         }
         this._centralDirectoryDigitalSignature = ZipIOCentralDirectoryDigitalSignature.ParseRecord(reader);
     }
     this._offset    = centralDirectoryOffset;
     this._dirtyFlag = false;
     this.Validate(expectedCentralDirectorySize);
 }
Example #8
0
 private void Validate(string fileName, ZipIOCentralDirectoryBlock centralDir, ZipIOCentralDirectoryFileHeader centralDirFileHeader)
 {
     if (string.CompareOrdinal(this._localFileHeader.FileName, fileName) != 0)
     {
         throw new FileFormatException(SR.Get("CorruptedData"));
     }
     if ((((this.VersionNeededToExtract != centralDirFileHeader.VersionNeededToExtract) || (this.GeneralPurposeBitFlag != centralDirFileHeader.GeneralPurposeBitFlag)) || ((this.CompressedSize != centralDirFileHeader.CompressedSize) || (this.UncompressedSize != centralDirFileHeader.UncompressedSize))) || ((this.CompressionMethod != centralDirFileHeader.CompressionMethod) || (this.Crc32 != centralDirFileHeader.Crc32)))
     {
         throw new FileFormatException(SR.Get("CorruptedData"));
     }
     if ((this.Offset + this.Size) > centralDir.Offset)
     {
         throw new FileFormatException(SR.Get("CorruptedData"));
     }
 }
Example #9
0
 private void ParseRecord(BinaryReader reader, string fileName, long position, ZipIOCentralDirectoryBlock centralDir, ZipIOCentralDirectoryFileHeader centralDirFileHeader)
 {
     this.CheckDisposed();
     this._localFileHeader = ZipIOLocalFileHeader.ParseRecord(reader, this._blockManager.Encoding);
     if (this._localFileHeader.StreamingCreationFlag)
     {
         this._blockManager.Stream.Seek(centralDirFileHeader.CompressedSize, SeekOrigin.Current);
         this._localFileDataDescriptor = ZipIOLocalFileDataDescriptor.ParseRecord(reader, centralDirFileHeader.CompressedSize, centralDirFileHeader.UncompressedSize, centralDirFileHeader.Crc32, this._localFileHeader.VersionNeededToExtract);
     }
     else
     {
         this._localFileDataDescriptor = null;
     }
     this._offset         = position;
     this._dirtyFlag      = false;
     this._fileItemStream = new ZipIOFileItemStream(this._blockManager, this, position + this._localFileHeader.Size, centralDirFileHeader.CompressedSize);
     if (this._localFileHeader.CompressionMethod == CompressionMethodEnum.Deflated)
     {
         this._deflateStream        = new CompressStream(this._fileItemStream, centralDirFileHeader.UncompressedSize);
         this._crcCalculatingStream = new ProgressiveCrcCalculatingStream(this._blockManager, this._deflateStream, this.Crc32);
     }
     else
     {
         if (this._localFileHeader.CompressionMethod != CompressionMethodEnum.Stored)
         {
             throw new NotSupportedException(SR.Get("ZipNotSupportedCompressionMethod"));
         }
         this._crcCalculatingStream = new ProgressiveCrcCalculatingStream(this._blockManager, this._fileItemStream, this.Crc32);
     }
     this.Validate(fileName, centralDir, centralDirFileHeader);
 }