Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix consumer is never called when extract uncompressed 0 byte entry #190

Merged
merged 7 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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