/// <summary> /// Creates a new entry for the <see cref="ZipWriteOnlyStorer"></see>. /// </summary> /// <param name="path"> Path to the data. </param> /// <param name="size"> Size of the data. </param> /// <param name="compressionMethod"> Compression method for the data. </param> /// <param name="compressedSize"> Size of the compressed data. </param> /// <param name="headerOffset"> Offset of header information. </param> /// <param name="crc32"> 32-bit checksum of the data. </param> /// <param name="modifyTime"> Modification time of the data. </param> /// <param name="comment"> User comment for the data. </param> public ZipWriteOnlyStorerEntry(string path, uint size, CompressionMethod compressionMethod, uint compressedSize, uint headerOffset, uint crc32, DateTime modifyTime, string comment) : base(path, compressionMethod, compressedSize, headerOffset, crc32, modifyTime, comment) { this.PathAsBytes = utf8Encoding.GetBytes(path); this.PathLengthAsBytes = BytesConverter.GetBytes((ushort)this.PathAsBytes.Length); this.CommentAsBytes = utf8Encoding.GetBytes(comment); this.CommentLengthAsBytes = BytesConverter.GetBytes((ushort)this.CommentAsBytes.Length); this.CompressionMethodAsBytes = BytesConverter.GetBytes((ushort)compressionMethod); this.ModifyTimeAsBytes = BytesConverter.GetBytes(ZipStorerUtils.DateTimeToDosTime(modifyTime)); this.CRC32AsBytes = BytesConverter.GetBytes(crc32); this.CompressedSizeAsBytes = BytesConverter.GetBytes(compressedSize >= 0xFFFFFFFF ? 0xFFFFFFFF : compressedSize); this.SizeAsBytes = BytesConverter.GetBytes(size >= 0xFFFFFFFF ? 0xFFFFFFFF : size); this.HeaderOffsetAsBytes = BytesConverter.GetBytes(headerOffset >= 0xFFFFFFFF ? 0xFFFFFFFF : (uint)headerOffset); this.CompressedSizeZip64AsBytes = BytesConverter.GetBytes(compressedSize); this.SizeZip64AsBytes = BytesConverter.GetBytes(size); this.HeaderOffsetZip64AsBytes = BytesConverter.GetBytes(headerOffset); }
/// <summary> /// Creates a new read-only zip storer from the data. /// </summary> /// <param name="data"> The data. </param> /// <returns> New read-only zip storer. </returns> /// <exception cref="Localization.ValueNullException"> /// The exception that is thrown when data is null. /// </exception> /// <exception cref="InvalidDataException"> /// The exception that is thrown when data is invalid. /// </exception> public static IReadOnlyDataStorer FromData(params byte[] data) { Validation.NotNull("Data", data); const int cfhSize = 46; // fixed size of the central file header const int lfhSize = 30; // fixed size of the local file header const int lfnOffset = 26; // relative offset to the size of the file name (local header) const int lefOffset = 28; // relative offset to the size of the extra field (local header) int length = data.Length; if (length < 22) { // Min length of the zip data is more than or equals to 22 bytes throw new InvalidDataException(); } int lastIndex = length - 1; int max = length - 5; int signaturePosition = -1; for (int i = max; i >= 0; i--) { signaturePosition = lastIndex - i - 4; if (BytesConverter.ToUInt32(data, signaturePosition) == endOfCentralDirRecordSignature) { bool isZip64 = false; long centralDirEntries = BytesConverter.ToUInt16(data, signaturePosition + 10); long centralDirSize = BytesConverter.ToUInt32(data, signaturePosition + 12); long centralDirOffset = BytesConverter.ToUInt32(data, signaturePosition + 16); if (centralDirOffset == zip64CentralDirSignature) // It is a Zip64 file { isZip64 = true; int zip64CentralDirLocatorOffset = signaturePosition - 20; if (BytesConverter.ToUInt32(data, zip64CentralDirLocatorOffset) != zip64EndOfCentralDirLocatorSignature) { // Not a ZIP64 central dir locator throw new InvalidDataException(); } int zip64CentralDirRecordOffset = (int)BytesConverter.ToUInt32(data, zip64CentralDirLocatorOffset + 8); if (BytesConverter.ToUInt32(data, zip64CentralDirRecordOffset) != zip64EndOfCentralDirRecordSignature) { // Not a ZIP64 central dir record throw new InvalidDataException(); } centralDirEntries = (long)BytesConverter.ToUInt64(data, zip64CentralDirRecordOffset + 32); centralDirSize = (long)BytesConverter.ToUInt64(data, zip64CentralDirRecordOffset + 40); centralDirOffset = (long)BytesConverter.ToUInt64(data, zip64CentralDirRecordOffset + 48); } int centralDirStartIndex = (int)centralDirOffset; int centralDirEndIndex = centralDirStartIndex + (int)centralDirSize; IDataStorerEntry[] fileEntries = new IDataStorerEntry[centralDirEntries]; int index = 0; for (int pointer = centralDirStartIndex; pointer < centralDirEndIndex;) { if (BytesConverter.ToUInt32(data, pointer) != centralDirectoryFileHeaderSignature) { // Not a ZIP64 central dir file header throw new InvalidDataException(); } // 4 bytes, central directory file header signature // 2 bytes, version made by // 2 bytes, version needed to extract (minimum) // 2 bytes, general purpose bit flag ushort method = BytesConverter.ToUInt16(data, pointer + 10); // 2 bytes, compression method uint modifyTime = BytesConverter.ToUInt32(data, pointer + 12); // 2+2 bytes, file last modification time and date uint crc32 = BytesConverter.ToUInt32(data, pointer + 16); // 4 bytes, CRC-32 uint compressedSize = BytesConverter.ToUInt32(data, pointer + 20); // 4 bytes, compressed size uint fileSize = BytesConverter.ToUInt32(data, pointer + 24); // 4 bytes, uncompressed size ushort fileNameSize = BytesConverter.ToUInt16(data, pointer + 28); // 2 bytes, file name length ushort extraFieldSize = BytesConverter.ToUInt16(data, pointer + 30); // 2 bytes, extra field length ushort commentSize = BytesConverter.ToUInt16(data, pointer + 32); // 2 bytes, file comment length // 2+2=4 bytes, disk number where file starts (disk=0), internal file attributes // 4 bytes, External file attributes uint headerOffset = BytesConverter.ToUInt32(data, pointer + 42); // 4 bytes, relative offset of header uint headerSize = (uint)(cfhSize + fileNameSize + extraFieldSize + commentSize); int commentsPointer = (int)(pointer + headerSize - commentSize); int extraFieldBlockPointer = commentsPointer - extraFieldSize; while (true) { if (extraFieldBlockPointer >= commentsPointer) { break; } if (BytesConverter.ToUInt16(data, extraFieldBlockPointer) == zip64ExtraBlockTagSignature) { if (fileSize == zip64CentralDirSignature) { fileSize = BytesConverter.ToUInt32(data, extraFieldBlockPointer + 12); } if (compressedSize == zip64CentralDirSignature) { compressedSize = BytesConverter.ToUInt32(data, extraFieldBlockPointer + 20); } if (headerOffset == zip64CentralDirSignature) { headerOffset = BytesConverter.ToUInt32(data, extraFieldBlockPointer + 28); } break; } else { extraFieldBlockPointer += BytesConverter.ToUInt16(data, extraFieldBlockPointer + 2); } } if (BytesConverter.ToUInt32(data, (int)headerOffset) != localFileHeaderSignature) { throw new InvalidDataException(); } uint lfeSize = (uint)(BytesConverter.ToUInt16(data, (int)headerOffset + lfnOffset) + BytesConverter.ToUInt16(data, (int)headerOffset + lefOffset)); ZipReadOnlyStorerEntry fileEntry = new ZipReadOnlyStorerEntry( utf8Encoding.GetString(data, cfhSize + pointer, fileNameSize), fileSize, (CompressionMethod)method, compressedSize, headerOffset, (uint)(lfhSize + lfeSize + headerOffset), crc32, ZipStorerUtils.DosTimeToDateTime(modifyTime), (commentSize > 0) ? utf8Encoding.GetString(data, (int)(pointer + headerSize - commentSize), commentSize) : "" ); fileEntries[index++] = fileEntry; pointer += (int)headerSize; } return(new ZipReadOnlyStorer(isZip64, data, fileEntries)); } } throw new InvalidDataException(); }