From 0b611edbea9281be5eba1dad4fd2f7ec9c495c82 Mon Sep 17 00:00:00 2001 From: Timofey Solomko Date: Sat, 15 Jul 2017 22:08:33 +0300 Subject: [PATCH] Fix problem with repeated access to zip entry's data --- Sources/ZipEntry.swift | 2 ++ Sources/ZipLocalHeader.swift | 3 +++ Tests/ZipTests.swift | 2 ++ 3 files changed, 7 insertions(+) diff --git a/Sources/ZipEntry.swift b/Sources/ZipEntry.swift index cdb8c282..76647abd 100644 --- a/Sources/ZipEntry.swift +++ b/Sources/ZipEntry.swift @@ -85,6 +85,8 @@ public class ZipEntry: ContainerEntry { localHeader!.lastModFileTime == cdEntry.lastModFileTime && localHeader!.lastModFileDate == cdEntry.lastModFileDate else { throw ZipError.wrongLocalHeader } + } else { + pointerData.index += localHeader!.headerSize } let hasDataDescriptor = localHeader!.generalPurposeBitFlags & 0x08 != 0 diff --git a/Sources/ZipLocalHeader.swift b/Sources/ZipLocalHeader.swift index fd3c3b84..40edd0e7 100644 --- a/Sources/ZipLocalHeader.swift +++ b/Sources/ZipLocalHeader.swift @@ -25,6 +25,8 @@ struct ZipLocalHeader { private(set) var accessTimestamp: UInt32? private(set) var creationTimestamp: UInt32? + let headerSize: Int + init(_ pointerData: DataWithPointer) throws { // Check signature. guard pointerData.uint32() == 0x04034b50 @@ -47,6 +49,7 @@ struct ZipLocalHeader { let fileNameLength = pointerData.uint16().toInt() let extraFieldLength = pointerData.uint16().toInt() + self.headerSize = 30 + fileNameLength + extraFieldLength guard let fileName = ZipCommon.getStringField(pointerData, fileNameLength, useUtf8) else { throw ZipError.wrongTextField } diff --git a/Tests/ZipTests.swift b/Tests/ZipTests.swift index c71ecce0..d5cc1f5f 100644 --- a/Tests/ZipTests.swift +++ b/Tests/ZipTests.swift @@ -151,6 +151,8 @@ class ZipTests: XCTestCase { } XCTAssertEqual(try? entries[0].data(), answerData) + // Test repeat of getting entry data (there was a problem with it). + XCTAssertEqual(try? entries[0].data(), answerData) } func testZipBZip2() {