internal static ZipEntry ReadDirEntry(ZipFile zipfile, Stream stream) { var reader = zipfile.CreateReader(stream); var v = reader.Read <UInt32>(); if (v != ZipConstants.ZipDirEntrySignature) { if (v != ZipConstants.EndOfCentralDirectorySignature && v != ZipConstants.ZipEntrySignature) { throw new ZipException("0x{0:X8}处签名错误!", stream.Position); } return(null); } reader.Stream.Position -= 4; var entry = reader.Read <ZipEntry>(); return(entry); }
internal static ZipEntry ReadDirEntry(ZipFile zipfile, Stream stream) { var reader = zipfile.CreateReader(stream); if (!reader.Expect(ZipConstants.ZipDirEntrySignature)) { if (!reader.Expect( ZipConstants.EndOfCentralDirectorySignature, //ZipConstants.Zip64EndOfCentralDirectoryRecordSignature, ZipConstants.ZipEntrySignature)) { throw new ZipException("0x{0:X8}处签名错误!", stream.Position); } return null; } var entry = reader.ReadObject<ZipEntry>(); return entry; }
internal static ZipEntry ReadEntry(ZipFile zipfile, Stream stream, Boolean first, Boolean embedFileData) { var reader = zipfile.CreateReader(stream); // 读取文件头时忽略掉这些字段,这些都是DirEntry的字段 reader.Settings.IgnoreMembers = dirMembers; // 有时候Zip文件以PK00开头 if (first && reader.Expect(ZipConstants.PackedToRemovableMedia)) reader.ReadBytes(4); // 验证头部 if (!reader.Expect(ZipConstants.ZipEntrySignature)) { if (!reader.Expect(ZipConstants.ZipDirEntrySignature, ZipConstants.EndOfCentralDirectorySignature)) throw new ZipException("0x{0:X8}处签名错误!", stream.Position); return null; } var entry = reader.ReadObject<ZipEntry>(); if (entry.IsDirectory) return entry; // 0长度的实体不要设置数据源 if (entry.CompressedSize > 0) { // 是否内嵌文件数据 entry.DataSource = embedFileData ? new ArrayDataSource(stream, (Int32)entry.CompressedSize) : new StreamDataSource(stream, stream.Position, 0); entry.DataSource.IsCompressed = entry.CompressionMethod != CompressionMethod.Stored; // 移到文件数据之后,可能是文件头 if (!embedFileData) stream.Seek(entry.CompressedSize, SeekOrigin.Current); } // 如果有扩展,则跳过 if (entry.BitField.Has(GeneralBitFlags.Descriptor)) { //stream.Seek(20, SeekOrigin.Current); // 在某些只读流中,可能无法回头设置校验和大小,此时可通过描述符在文件内容之后设置 entry.Crc = reader.ReadUInt32(); entry.CompressedSize = reader.ReadUInt32(); entry.UncompressedSize = reader.ReadUInt32(); } return entry; }
internal static ZipEntry ReadEntry(ZipFile zipfile, Stream stream, Boolean first, Boolean embedFileData) { var reader = zipfile.CreateReader(stream); // 读取文件头时忽略掉这些字段,这些都是DirEntry的字段 //reader.Settings.IgnoreMembers = dirMembers; var bn = (reader as Binary); if (bn != null) { bn.IgnoreMembers = dirMembers; } // 有时候Zip文件以PK00开头 if (first) { if (reader.Read <UInt32>() != ZipConstants.PackedToRemovableMedia) { reader.Stream.Position -= 4; } } // 验证头部 var v = reader.Read <UInt32>(); if (v != ZipConstants.ZipEntrySignature) { if (v != ZipConstants.ZipDirEntrySignature && v != ZipConstants.EndOfCentralDirectorySignature) { throw new ZipException("0x{0:X8}处签名错误!", stream.Position); } return(null); } reader.Stream.Position -= 4; var entry = reader.Read <ZipEntry>(); if (entry.IsDirectory) { return(entry); } // 0长度的实体不要设置数据源 if (entry.CompressedSize > 0) { // 是否内嵌文件数据 entry.DataSource = embedFileData ? new ArrayDataSource(stream, (Int32)entry.CompressedSize) : new StreamDataSource(stream, stream.Position, 0); entry.DataSource.IsCompressed = entry.CompressionMethod != CompressionMethod.Stored; // 移到文件数据之后,可能是文件头 if (!embedFileData) { stream.Seek(entry.CompressedSize, SeekOrigin.Current); } } // 如果有扩展,则跳过 if (entry.BitField.Has(GeneralBitFlags.Descriptor)) { //stream.Seek(20, SeekOrigin.Current); // 在某些只读流中,可能无法回头设置校验和大小,此时可通过描述符在文件内容之后设置 entry.Crc = reader.Read <UInt32>(); entry.CompressedSize = reader.Read <UInt32>(); entry.UncompressedSize = reader.Read <UInt32>(); } return(entry); }