/// <summary>
/// Encodes a <paramref name="timestamp"/> as a <see cref="ZipEntry.ExtraData"/> in a format that ensures it will be read for <see cref="ZipEntry.DateTime"/>, preserving second-accuracy.
/// </summary>
private static byte[] GetUnixTimestamp(DateTime timestamp)
{
var extraData = new ZipExtraData();
extraData.AddEntry(new ExtendedUnixData
{
AccessTime = timestamp, CreateTime = timestamp, ModificationTime = timestamp,
Include = ExtendedUnixData.Flags.AccessTime | ExtendedUnixData.Flags.CreateTime | ExtendedUnixData.Flags.ModificationTime
});
return extraData.GetEntryData();
}
int WriteCentralDirectoryHeader(ZipEntry entry)
{
if ( entry.CompressedSize < 0 ) {
throw new ZipException("Attempt to write central directory entry with unknown csize");
}
if ( entry.Size < 0 ) {
throw new ZipException("Attempt to write central directory entry with unknown size");
}
if ( entry.Crc < 0 ) {
throw new ZipException("Attempt to write central directory entry with unknown crc");
}
// Write the central file header
WriteLEInt(ZipConstants.CentralHeaderSignature);
// Version made by
WriteLEShort(ZipConstants.VersionMadeBy);
// Version required to extract
WriteLEShort(entry.Version);
WriteLEShort(entry.Flags);
unchecked {
WriteLEShort((byte)entry.CompressionMethod);
WriteLEInt((int)entry.DosTime);
WriteLEInt((int)entry.Crc);
}
if ( (entry.IsZip64Forced()) || (entry.CompressedSize >= 0xffffffff) ) {
WriteLEInt(-1);
}
else {
WriteLEInt((int)(entry.CompressedSize & 0xffffffff));
}
if ( (entry.IsZip64Forced()) || (entry.Size >= 0xffffffff) ) {
WriteLEInt(-1);
}
else {
WriteLEInt((int)entry.Size);
}
byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
if ( name.Length > 0xFFFF ) {
throw new ZipException("Entry name is too long.");
}
WriteLEShort(name.Length);
// Central header extra data is different to local header version so regenerate.
ZipExtraData ed = new ZipExtraData(entry.ExtraData);
if ( entry.CentralHeaderRequiresZip64 ) {
ed.StartNewEntry();
if ( (entry.Size >= 0xffffffff) || (useZip64_ == UseZip64.On) )
{
ed.AddLeLong(entry.Size);
}
if ( (entry.CompressedSize >= 0xffffffff) || (useZip64_ == UseZip64.On) )
{
ed.AddLeLong(entry.CompressedSize);
}
if ( entry.Offset >= 0xffffffff ) {
ed.AddLeLong(entry.Offset);
}
// Number of disk on which this file starts isnt supported and is never written here.
ed.AddNewEntry(1);
}
else {
// Should have already be done when local header was added.
ed.Delete(1);
}
byte[] centralExtraData = ed.GetEntryData();
WriteLEShort(centralExtraData.Length);
WriteLEShort(entry.Comment != null ? entry.Comment.Length : 0);
WriteLEShort(0); // disk number
WriteLEShort(0); // internal file attributes
// External file attributes...
if ( entry.ExternalFileAttributes != -1 ) {
WriteLEInt(entry.ExternalFileAttributes);
}
else {
if ( entry.IsDirectory ) {
WriteLEUint(16);
}
else {
WriteLEUint(0);
}
//.........这里部分代码省略.........
/// <summary>
/// Test a local header against that provided from the central directory
/// </summary>
/// <param name="entry">
/// The entry to test against
/// </param>
/// <param name="tests">The type of <see cref="HeaderTest">tests</see> to carry out.</param>
/// <returns>The offset of the entries data in the file</returns>
long TestLocalHeader(ZipEntry entry, HeaderTest tests)
{
lock(baseStream_)
{
bool testHeader = (tests & HeaderTest.Header) != 0;
bool testData = (tests & HeaderTest.Extract) != 0;
baseStream_.Seek(offsetOfFirstEntry + entry.Offset, SeekOrigin.Begin);
if ((int)ReadLEUint() != ZipConstants.LocalHeaderSignature) {
throw new ZipException(string.Format("Wrong local header signature @{0:X}", offsetOfFirstEntry + entry.Offset));
}
short extractVersion = ( short ) (ReadLEUshort() & 0x00ff);
short localFlags = ( short )ReadLEUshort();
short compressionMethod = ( short )ReadLEUshort();
short fileTime = ( short )ReadLEUshort();
short fileDate = ( short )ReadLEUshort();
uint crcValue = ReadLEUint();
long compressedSize = ReadLEUint();
long size = ReadLEUint();
int storedNameLength = ReadLEUshort();
int extraDataLength = ReadLEUshort();
byte[] nameData = new byte[storedNameLength];
StreamUtils.ReadFully(baseStream_, nameData);
byte[] extraData = new byte[extraDataLength];
StreamUtils.ReadFully(baseStream_, extraData);
ZipExtraData localExtraData = new ZipExtraData(extraData);
// Extra data / zip64 checks
if (localExtraData.Find(1))
{
// 2010-03-04 Forum 10512: removed checks for version >= ZipConstants.VersionZip64
// and size or compressedSize = MaxValue, due to rogue creators.
size = localExtraData.ReadLong();
compressedSize = localExtraData.ReadLong();
if ((localFlags & (int)GeneralBitFlags.Descriptor) != 0)
{
// These may be valid if patched later
if ( (size != -1) && (size != entry.Size)) {
throw new ZipException("Size invalid for descriptor");
}
if ((compressedSize != -1) && (compressedSize != entry.CompressedSize)) {
throw new ZipException("Compressed size invalid for descriptor");
}
}
}
else
{
// No zip64 extra data but entry requires it.
if ((extractVersion >= ZipConstants.VersionZip64) &&
(((uint)size == uint.MaxValue) || ((uint)compressedSize == uint.MaxValue)))
{
throw new ZipException("Required Zip64 extended information missing");
}
}
if ( testData ) {
if ( entry.IsFile ) {
if ( !entry.IsCompressionMethodSupported() ) {
throw new ZipException("Compression method not supported");
}
if ( (extractVersion > ZipConstants.VersionMadeBy)
|| ((extractVersion > 20) && (extractVersion < ZipConstants.VersionZip64)) ) {
throw new ZipException(string.Format("Version required to extract this entry not supported ({0})", extractVersion));
}
if ( (localFlags & ( int )(GeneralBitFlags.Patched | GeneralBitFlags.StrongEncryption | GeneralBitFlags.EnhancedCompress | GeneralBitFlags.HeaderMasked)) != 0 ) {
throw new ZipException("The library does not support the zip version required to extract this entry");
}
}
}
if (testHeader)
{
if ((extractVersion <= 63) && // Ignore later versions as we dont know about them..
(extractVersion != 10) &&
(extractVersion != 11) &&
(extractVersion != 20) &&
(extractVersion != 21) &&
(extractVersion != 25) &&
(extractVersion != 27) &&
(extractVersion != 45) &&
(extractVersion != 46) &&
(extractVersion != 50) &&
(extractVersion != 51) &&
//.........这里部分代码省略.........
/// <summary>
/// Process extra data fields updating the entry based on the contents.
/// </summary>
/// <param name="localHeader">True if the extra data fields should be handled
/// for a local header, rather than for a central header.
/// </param>
internal void ProcessExtraData(bool localHeader)
{
ZipExtraData extraData = new ZipExtraData(this.extra);
if ( extraData.Find(0x0001) ) {
// Version required to extract is ignored here as some archivers dont set it correctly
// in theory it should be version 45 or higher
// The recorded size will change but remember that this is zip64.
forceZip64_ = true;
if ( extraData.ValueLength < 4 ) {
throw new ZipException("Extra data extended Zip64 information length is invalid");
}
if ( localHeader || (size == uint.MaxValue) ) {
size = (ulong)extraData.ReadLong();
}
if ( localHeader || (compressedSize == uint.MaxValue) ) {
compressedSize = (ulong)extraData.ReadLong();
}
if ( !localHeader && (offset == uint.MaxValue) ) {
offset = extraData.ReadLong();
}
// Disk number on which file starts is ignored
}
else {
if (
((versionToExtract & 0xff) >= ZipConstants.VersionZip64) &&
((size == uint.MaxValue) || (compressedSize == uint.MaxValue))
) {
throw new ZipException("Zip64 Extended information required but is missing.");
}
}
dateTime = GetDateTime(extraData);
if (method == CompressionMethod.WinZipAES) {
ProcessAESExtraData(extraData);
}
}
/// <summary>
/// Finishes the stream. This will write the central directory at the
/// end of the zip file and flush the stream.
/// </summary>
/// <remarks>
/// This is automatically called when the stream is closed.
/// </remarks>
/// <exception cref="System.IO.IOException">
/// An I/O error occurs.
/// </exception>
/// <exception cref="ZipException">
/// Comment exceeds the maximum length<br/>
/// Entry name exceeds the maximum length
/// </exception>
public override void Finish()
{
if (entries == null)
{
return;
}
if (curEntry != null)
{
CloseEntry();
}
long numEntries = entries.Count;
long sizeEntries = 0;
foreach (ZipEntry entry in entries)
{
WriteLeInt(ZipConstants.CentralHeaderSignature);
WriteLeShort(ZipConstants.VersionMadeBy);
WriteLeShort(entry.Version);
WriteLeShort(entry.Flags);
WriteLeShort((short) entry.CompressionMethodForHeader);
WriteLeInt((int) entry.DosTime);
WriteLeInt((int) entry.Crc);
if (entry.IsZip64Forced() ||
(entry.CompressedSize >= uint.MaxValue))
{
WriteLeInt(-1);
}
else
{
WriteLeInt((int) entry.CompressedSize);
}
if (entry.IsZip64Forced() ||
(entry.Size >= uint.MaxValue))
{
WriteLeInt(-1);
}
else
{
WriteLeInt((int) entry.Size);
}
byte[] name = ZipConstants.ConvertToArray(entry.Flags, entry.Name);
if (name.Length > 0xffff)
{
throw new ZipException("Name too long.");
}
var ed = new ZipExtraData(entry.ExtraData);
if (entry.CentralHeaderRequiresZip64)
{
ed.StartNewEntry();
if (entry.IsZip64Forced() ||
(entry.Size >= 0xffffffff))
{
ed.AddLeLong(entry.Size);
}
if (entry.IsZip64Forced() ||
(entry.CompressedSize >= 0xffffffff))
{
ed.AddLeLong(entry.CompressedSize);
}
if (entry.Offset >= 0xffffffff)
{
ed.AddLeLong(entry.Offset);
}
ed.AddNewEntry(1);
}
else
{
ed.Delete(1);
}
#if !NET_1_1 && !NETCF_2_0
if (entry.AESKeySize > 0)
{
AddExtraDataAES(entry, ed);
}
//.........这里部分代码省略.........
// For AES the method in the entry is 99, and the real compression method is in the extradata
//
private void ProcessAESExtraData(ZipExtraData extraData)
{
throw new ZipException("AES unsupported");
}
//.........这里部分代码省略.........
throw new ZipException(string.Format("Patched data requires higher version than ({0})", extractVersion));
}
// Central header flags match local entry flags.
if ( localFlags != entry.Flags ) {
throw new ZipException("Central header/local header flags mismatch");
}
// Central header compression method matches local entry
if ( entry.CompressionMethod != ( CompressionMethod )compressionMethod ) {
throw new ZipException("Central header/local header compression method mismatch");
}
// Strong encryption and extract version match
if ( (localFlags & ( int )GeneralBitFlags.StrongEncryption) != 0 ) {
if ( extractVersion < 62 ) {
throw new ZipException("Strong encryption flag set but version not high enough");
}
}
if ( (localFlags & ( int )GeneralBitFlags.HeaderMasked) != 0 ) {
if ( (fileTime != 0) || (fileDate != 0) ) {
throw new ZipException("Header masked set but date/time values non-zero");
}
}
if ( (localFlags & ( int )GeneralBitFlags.Descriptor) == 0 ) {
if ( crcValue != (uint)entry.Crc ) {
throw new ZipException("Central header/local header crc mismatch");
}
}
// Crc valid for empty entry.
if ( (size == 0) && (compressedSize == 0) ) {
if ( crcValue != 0 ) {
throw new ZipException("Invalid CRC for empty entry");
}
}
// TODO: make test more correct... can't compare lengths as was done originally as this can fail for MBCS strings
// Assuming a code page at this point is not valid? Best is to store the name length in the ZipEntry probably
if ( entry.Name.Length > storedNameLength ) {
throw new ZipException("File name length mismatch");
}
byte[] nameData = new byte[storedNameLength];
StreamUtils.ReadFully(baseStream_, nameData);
string localName = ZipConstants.ConvertToStringExt(localFlags, nameData);
// Central directory and local entry name match
if ( localName != entry.Name ) {
throw new ZipException("Central header and local header file name mismatch");
}
// Directories have zero size.
if ( entry.IsDirectory ) {
if ( (compressedSize != 0) || (size != 0) ) {
throw new ZipException("Directory cannot have size");
}
}
if ( !ZipNameTransform.IsValidName(localName, true) ) {
throw new ZipException("Name is invalid");
}
byte[] data = new byte[extraDataLength];
StreamUtils.ReadFully(baseStream_, data);
ZipExtraData ed = new ZipExtraData(data);
// Extra data / zip64 checks
if ( ed.Find(1) ) {
// Zip64 extra data but 'extract version' is too low
if ( extractVersion < ZipConstants.VersionZip64 ) {
throw new ZipException(
string.Format("Extra data contains Zip64 information but version {0}.{1} is not high enough",
extractVersion / 10, extractVersion % 10));
}
// Zip64 extra data but size fields dont indicate its required.
if ( (( uint )size != uint.MaxValue) && (( uint )compressedSize != uint.MaxValue) ) {
throw new ZipException("Entry sizes not correct for Zip64");
}
size = ed.ReadLong();
compressedSize = ed.ReadLong();
}
else {
// No zip64 extra data but entry requires it.
if ( (extractVersion >= ZipConstants.VersionZip64) &&
((( uint )size == uint.MaxValue) || (( uint )compressedSize == uint.MaxValue)) ) {
throw new ZipException("Required Zip64 extended information missing");
}
}
}
int extraLength = storedNameLength + extraDataLength;
return offsetOfFirstEntry + entry.Offset + ZipConstants.LocalHeaderBaseSize + extraLength;
}
}
请发表评论