Exemplo n.º 1
1
        protected ZipHeader ReadHeader(uint headerBytes, BinaryReader reader)
        {
            switch (headerBytes)
            {
                case 0x5054b50:
                    return null;

                case 0x6054b50:
                {
                    DirectoryEndHeader header3 = new DirectoryEndHeader();
                    header3.Read(reader);
                    return header3;
                }
                case 0x2014b50:
                {
                    DirectoryEntryHeader header2 = new DirectoryEntryHeader();
                    header2.Read(reader);
                    return header2;
                }
                case 0x4034b50:
                {
                    LocalEntryHeader entryHeader = new LocalEntryHeader();
                    entryHeader.Read(reader);
                    this.LoadHeader(entryHeader, reader.BaseStream);
                    this.lastEntryHeader = entryHeader;
                    return entryHeader;
                }
                case 0x6064b50:
                case 0x7064b50:
                {
                    IgnoreHeader header4 = new IgnoreHeader(ZipHeaderType.Ignore);
                    header4.Read(reader);
                    return header4;
                }
                case 0x8074b50:
                    if (FlagUtility.HasFlag<HeaderFlags>(this.lastEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
                    {
                        this.lastEntryHeader.Crc = reader.ReadUInt32();
                        this.lastEntryHeader.CompressedSize = reader.ReadUInt32();
                        this.lastEntryHeader.UncompressedSize = reader.ReadUInt32();
                    }
                    else
                    {
                        reader.ReadUInt32();
                        reader.ReadUInt32();
                        reader.ReadUInt32();
                    }
                    return null;

                case 0x30304b50:
                    return new SplitHeader();
            }
            throw new NotSupportedException("Unknown header: " + headerBytes);
        }
Exemplo n.º 2
0
        protected ZipHeader ReadHeader(uint headerBytes, BinaryReader reader)
        {
            switch (headerBytes)
            {
                case ENTRY_HEADER_BYTES:
                    {
                        var entryHeader = new LocalEntryHeader();
                        entryHeader.Read(reader);
                        LoadHeader(entryHeader, reader.BaseStream);

                        lastEntryHeader = entryHeader;
                        return entryHeader;
                    }
                case DIRECTORY_START_HEADER_BYTES:
                    {
                        var entry = new DirectoryEntryHeader();
                        entry.Read(reader);
                        return entry;
                    }
                case POST_DATA_DESCRIPTOR:
                    {
                        if (FlagUtility.HasFlag(lastEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor))
                        {
                            lastEntryHeader.Crc = reader.ReadUInt32();
                            lastEntryHeader.CompressedSize = reader.ReadUInt32();
                            lastEntryHeader.UncompressedSize = reader.ReadUInt32();
                        }
                        else
                        {
                            reader.ReadUInt32();
                            reader.ReadUInt32();
                            reader.ReadUInt32();
                        }
                        return null;
                    }
                case DIGITAL_SIGNATURE:
                    return null;
                case DIRECTORY_END_HEADER_BYTES:
                    {
                        var entry = new DirectoryEndHeader();
                        entry.Read(reader);
                        return entry;
                    }
                case SPLIT_ARCHIVE_HEADER_BYTES:
                    {
                        return new SplitHeader();
                    }
                case ZIP64_END_OF_CENTRAL_DIRECTORY:
                case ZIP64_END_OF_CENTRAL_DIRECTORY_LOCATOR:
                    {
                        var entry = new IgnoreHeader(ZipHeaderType.Ignore);
                        entry.Read(reader);
                        return entry;
                    }
                default:
                    throw new NotSupportedException("Unknown header: " + headerBytes);
            }
        }
Exemplo n.º 3
0
        internal static IEnumerable<ZipHeader> ReadHeaderNonseekable(Stream stream)
        {
            while (true)
            {
                ZipHeader header = null;
                try
                {
                    MarkingBinaryReader reader = new MarkingBinaryReader(stream);

                    uint headerBytes = reader.ReadUInt32();
                    switch (headerBytes)
                    {
                        case ENTRY_HEADER_BYTES:
                            {
                                var entry = new LocalEntryHeader();
                                entry.Read(reader);
                                if (entry.CompressedSize > 0)
                                {
                                    entry.PackedStream = new ReadOnlySubStream(stream, entry.CompressedSize, true);
                                }
                                header = entry;
                            }
                            break;
                        case DIRECTORY_START_HEADER_BYTES:
                            {
                                var entry = new DirectoryEntryHeader();
                                entry.Read(reader);
                                header = entry;
                            }
                            break;
                        case POST_DATA_DESCRIPTOR:
                        case DIGITAL_SIGNATURE:
                            break;
                        case DIRECTORY_END_HEADER_BYTES:
                            {
                                var entry = new DirectoryEndHeader();
                                entry.Read(reader);
                                header = entry;
                            }
                            break;
                        case ZIP64_END_OF_CENTRAL_DIRECTORY:
                        default:
                            break;
                    }
                }
                catch
                {
                    header = null;
                }
                yield return header;
            }
        }
        internal IEnumerable<DirectoryEntryHeader> ReadSeekableHeader(Stream stream)
        {
            long offset = 0;
            uint signature;
            BinaryReader reader = new BinaryReader(stream);

            int iterationCount = 0;
            do
            {
                if ((stream.Length + offset) - 4 < 0)
                {
                    throw new ArchiveException("Failed to locate the Zip Header");
                }
                stream.Seek(offset - 4, SeekOrigin.End);
                signature = reader.ReadUInt32();
                offset--;
                iterationCount++;
                if (iterationCount > MAX_ITERATIONS_FOR_DIRECTORY_HEADER)
                {
                    throw new ArchiveException(
                        "Could not find Zip file Directory at the end of the file.  File may be corrupted.");
                }
            } while (signature != DIRECTORY_END_HEADER_BYTES);

            var entry = new DirectoryEndHeader();
            entry.Read(reader);
            stream.Seek(entry.DirectoryStartOffsetRelativeToDisk, SeekOrigin.Begin);

            DirectoryEntryHeader directoryEntryHeader = null;
            long position = stream.Position;
            while (true)
            {
                stream.Position = position;
                signature = reader.ReadUInt32();
                directoryEntryHeader = ReadHeader(signature, reader) as DirectoryEntryHeader;
                position = stream.Position;
                if (directoryEntryHeader == null)
                {
                    yield break;
                }
                //entry could be zero bytes so we need to know that.
                directoryEntryHeader.HasData = directoryEntryHeader.CompressedSize != 0;
                yield return directoryEntryHeader;
            }
        }
 internal IEnumerable<DirectoryEntryHeader> ReadSeekableHeader(Stream stream)
 {
     uint iteratorVariable1;
     long iteratorVariable0 = 0L;
     BinaryReader reader = new BinaryReader(stream);
     int iteratorVariable3 = 0;
     do
     {
         if (((stream.Length + iteratorVariable0) - 4L) < 0L)
         {
             throw new ArchiveException("Failed to locate the Zip Header");
         }
         stream.Seek(iteratorVariable0 - 4L, SeekOrigin.End);
         iteratorVariable1 = reader.ReadUInt32();
         iteratorVariable0 -= 1L;
         iteratorVariable3++;
         if (iteratorVariable3 > 0x3e8)
         {
             throw new ArchiveException("Could not find Zip file Directory at the end of the file.  File may be corrupted.");
         }
     }
     while (iteratorVariable1 != 0x6054b50);
     DirectoryEndHeader iteratorVariable4 = new DirectoryEndHeader();
     iteratorVariable4.Read(reader);
     stream.Seek((long) iteratorVariable4.DirectoryStartOffsetRelativeToDisk, SeekOrigin.Begin);
     DirectoryEntryHeader iteratorVariable5 = null;
     long position = stream.Position;
     while (true)
     {
         stream.Position = position;
         iteratorVariable1 = reader.ReadUInt32();
         iteratorVariable5 = this.ReadHeader(iteratorVariable1, reader) as DirectoryEntryHeader;
         position = stream.Position;
         if (iteratorVariable5 == null)
         {
             yield break;
         }
         iteratorVariable5.HasData = iteratorVariable5.CompressedSize != 0;
         yield return iteratorVariable5;
     }
 }