From 42553d82bfa94e522cde2e6137da15d2e9c37558 Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Mon, 31 Jul 2017 17:49:44 +0300 Subject: [PATCH] Fix problem with 7z containers with empty and non-empty files unpackSizes array doesn't have values for empty files. --- Sources/7zContainer.swift | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Sources/7zContainer.swift b/Sources/7zContainer.swift index 8a1e7b59..125197fd 100644 --- a/Sources/7zContainer.swift +++ b/Sources/7zContainer.swift @@ -17,6 +17,8 @@ public class SevenZipContainer: Container { guard let files = header.fileInfo?.files else { return [] } + var nonEmptyFileIndex = 0 + /// Index of currently opened folder in `streamInfo.coderInfo.folders`. var folderIndex = 0 @@ -52,8 +54,6 @@ public class SevenZipContainer: Container { let data: Data? if !file.isEmptyStream { - // TODO: Does empty files need all this? - // Without `SevenZipStreamInfo` and `SevenZipPackInfo` objects, // we cannot find file data location in container. guard let streamInfo = header.mainStreams @@ -131,15 +131,15 @@ public class SevenZipContainer: Container { // File's unpack size is required to proceed. // Next check ensures that we don't `unpackSizes` array's boundaries. - guard fileIndex < substreamInfo.unpackSizes.count + guard nonEmptyFileIndex < substreamInfo.unpackSizes.count else { throw SevenZipError.noFileSize } - let fileSize = substreamInfo.unpackSizes[fileIndex] + let fileSize = substreamInfo.unpackSizes[nonEmptyFileIndex] let fileData = Data(bytes: rawFileData.bytes(count: fileSize)) let calculatedFileCRC = CheckSums.crc32(fileData) - if fileIndex < substreamInfo.digests.count { - guard calculatedFileCRC == substreamInfo.digests[fileIndex] + if nonEmptyFileIndex < substreamInfo.digests.count { + guard calculatedFileCRC == substreamInfo.digests[nonEmptyFileIndex] else { throw SevenZipError.wrongCRC } } @@ -154,6 +154,7 @@ public class SevenZipContainer: Container { rawUnpackSize += fileSize fileInFolderCount += 1 + nonEmptyFileIndex += 1 if fileInFolderCount > folder.numUnpackSubstreams { // If we read all files in folder... // We need to check folder's unpacked size as well as its CRC32 (if it is available).