Skip to content

Commit

Permalink
Merge pull request #190 from mtgto/uncompressed_zerobyte
Browse files Browse the repository at this point in the history
Fix consumer is never called when extract uncompressed 0 byte entry
  • Loading branch information
weichsel authored Dec 2, 2020
2 parents ea9572b + f3cbad7 commit 8b20f45
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
7 changes: 6 additions & 1 deletion Sources/ZIPFoundation/Data+Serialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ extension Data {

static func consumePart(of size: Int, chunkSize: Int, skipCRC32: Bool = false,
provider: Provider, consumer: Consumer) throws -> CRC32 {
var checksum = CRC32(0)
guard size > 0 else {
try consumer(Data())
return checksum
}

let readInOneChunk = (size < chunkSize)
var chunkSize = readInOneChunk ? size : chunkSize
var checksum = CRC32(0)
var bytesRead = 0
while bytesRead < size {
let remainingSize = size - bytesRead
Expand Down
Binary file not shown.
18 changes: 18 additions & 0 deletions Tests/ZIPFoundationTests/ZIPFoundationReadingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,24 @@ extension ZIPFoundationTests {
XCTAssert(entriesRead == 0)
}

func testExtractUncompressedEmptyFile() {
// We had a logic error, where completion handlers for empty entries were not called
// Ensure that this edge case works
var didCallCompletion = false
let archive = self.archive(for: #function, mode: .read)
guard let entry = archive["empty.txt"] else { XCTFail("Failed to extract entry."); return }

do {
_ = try archive.extract(entry) { (data) in
XCTAssertEqual(data.count, 0)
didCallCompletion = true
}
} catch {
XCTFail("Unexpected error while trying to extract empty file of uncompressed archive.")
}
XCTAssert(didCallCompletion)
}

func testExtractUncompressedEntryCancelation() {
let archive = self.archive(for: #function, mode: .read)
guard let entry = archive["original"] else { XCTFail("Failed to extract entry."); return }
Expand Down
1 change: 1 addition & 0 deletions Tests/ZIPFoundationTests/ZIPFoundationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ extension ZIPFoundationTests {
("testExtractMSDOSArchive", testExtractMSDOSArchive),
("testExtractUncompressedDataDescriptorArchive", testExtractUncompressedDataDescriptorArchive),
("testExtractUncompressedFolderEntries", testExtractUncompressedFolderEntries),
("testExtractUncompressedEmptyFile", testExtractUncompressedEmptyFile),
("testExtractZIP64ArchiveErrorConditions", testExtractZIP64ArchiveErrorConditions),
("testFileAttributeHelperMethods", testFileAttributeHelperMethods),
("testFilePermissionHelperMethods", testFilePermissionHelperMethods),
Expand Down

0 comments on commit 8b20f45

Please sign in to comment.