Skip to content

Commit 2d2e186

Browse files
authored
Fix unzip bug (#7)
* Fix unzip bug
1 parent 5ca8997 commit 2d2e186

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

Sources/Zip/Zip.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ public class Zip {
112112
}
113113

114114
let fullPath = destination.appendingPathComponent(pathString).standardized.path
115-
// `.standardized` removes any ".. to move a level up".
116-
// If we then check that the `fullPath` starts with the destination directory we know we are not extracting "outside" te destination.
115+
// `.standardized` removes any `..` to move a level up.
116+
// If we then check that the `fullPath` starts with the destination directory we know we are not extracting "outside" the destination.
117117
guard fullPath.starts(with: destination.standardized.path) else {
118118
throw ZipError.unzipFail
119119
}
@@ -146,17 +146,18 @@ public class Zip {
146146
}
147147

148148
var writeBytes: UInt64 = 0
149-
while let filePointer = fopen(fullPath, "wb") {
150-
defer { fclose(filePointer) }
149+
let filePointer: UnsafeMutablePointer<FILE>? = fopen(fullPath, "wb")
150+
while let filePointer {
151151
let readBytes = unzReadCurrentFile(zip, &buffer, bufferSize)
152-
if readBytes > 0 {
153-
guard fwrite(buffer, Int(readBytes), 1, filePointer) == 1 else {
154-
throw ZipError.unzipFail
155-
}
156-
writeBytes += UInt64(readBytes)
157-
} else { break }
152+
guard readBytes > 0 else { break }
153+
guard fwrite(buffer, Int(readBytes), 1, filePointer) == 1 else {
154+
throw ZipError.unzipFail
155+
}
156+
writeBytes += UInt64(readBytes)
158157
}
159158

159+
if let filePointer { fclose(filePointer) }
160+
160161
crc_ret = unzCloseCurrentFile(zip)
161162
if crc_ret == UNZ_CRCERROR {
162163
throw ZipError.unzipFail
@@ -182,7 +183,7 @@ public class Zip {
182183

183184
// Update progress handler
184185
if let progressHandler = progress {
185-
progressHandler((currentPosition/totalSize))
186+
progressHandler((currentPosition / totalSize))
186187
}
187188

188189
if let fileHandler = fileOutputHandler,

Tests/ZipTests/ZipTests.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ final class ZipTests: XCTestCase {
4545
try? FileManager.default.removeItem(at: destinationURL)
4646
}
4747
XCTAssertTrue(FileManager.default.fileExists(atPath: destinationURL.path))
48+
try XCTAssertGreaterThan(Data(contentsOf: destinationURL.appendingPathComponent("3crBXeO.gif")).count, 0)
49+
try XCTAssertGreaterThan(Data(contentsOf: destinationURL.appendingPathComponent("kYkLkPf.gif")).count, 0)
4850
}
4951

5052
func testQuickUnzipNonExistingPath() {
@@ -59,12 +61,15 @@ final class ZipTests: XCTestCase {
5961

6062
func testQuickUnzipProgress() throws {
6163
let filePath = url(forResource: "bb8", withExtension: "zip")!
62-
let destinationURL = try Zip.quickUnzipFile(filePath, progress: { progress in
64+
let destinationURL = try Zip.quickUnzipFile(filePath) { progress in
6365
XCTAssertFalse(progress.isNaN)
64-
})
66+
}
6567
addTeardownBlock {
6668
try? FileManager.default.removeItem(at: destinationURL)
6769
}
70+
XCTAssertTrue(FileManager.default.fileExists(atPath: destinationURL.path))
71+
try XCTAssertGreaterThan(Data(contentsOf: destinationURL.appendingPathComponent("3crBXeO.gif")).count, 0)
72+
try XCTAssertGreaterThan(Data(contentsOf: destinationURL.appendingPathComponent("kYkLkPf.gif")).count, 0)
6873
}
6974

7075
func testQuickUnzipOnlineURL() {
@@ -79,6 +84,8 @@ final class ZipTests: XCTestCase {
7984
XCTAssertNoThrow(try Zip.unzipFile(filePath, destination: destinationPath, overwrite: true, password: "password", progress: nil))
8085

8186
XCTAssertTrue(FileManager.default.fileExists(atPath: destinationPath.path))
87+
try XCTAssertGreaterThan(Data(contentsOf: destinationPath.appendingPathComponent("3crBXeO.gif")).count, 0)
88+
try XCTAssertGreaterThan(Data(contentsOf: destinationPath.appendingPathComponent("kYkLkPf.gif")).count, 0)
8289
}
8390

8491
func testImplicitProgressUnzip() throws {
@@ -113,7 +120,8 @@ final class ZipTests: XCTestCase {
113120
let imageURL1 = url(forResource: "3crBXeO", withExtension: "gif")!
114121
let imageURL2 = url(forResource: "kYkLkPf", withExtension: "gif")!
115122
let destinationURL = try Zip.quickZipFiles([imageURL1, imageURL2], fileName: "archive")
116-
XCTAssertTrue(FileManager.default.fileExists(atPath:destinationURL.path))
123+
XCTAssertTrue(FileManager.default.fileExists(atPath: destinationURL.path))
124+
try XCTAssertGreaterThan(Data(contentsOf: destinationURL).count, 0)
117125
addTeardownBlock {
118126
try? FileManager.default.removeItem(at: destinationURL)
119127
}
@@ -126,6 +134,7 @@ final class ZipTests: XCTestCase {
126134
XCTAssertFalse(progress.isNaN)
127135
}
128136
XCTAssertTrue(FileManager.default.fileExists(atPath:destinationURL.path))
137+
try XCTAssertGreaterThan(Data(contentsOf: destinationURL).count, 0)
129138
addTeardownBlock {
130139
try? FileManager.default.removeItem(at: destinationURL)
131140
}
@@ -337,6 +346,7 @@ final class ZipTests: XCTestCase {
337346
XCTAssert(FileManager.default.fileExists(atPath: destinationFolder.appendingPathComponent("metadata.json").path))
338347
XCTAssert(FileManager.default.fileExists(atPath: destinationFolder.appendingPathComponent("main/index.html").path))
339348
XCTAssert(FileManager.default.fileExists(atPath: destinationFolder.appendingPathComponent("main/index/index.json").path))
349+
try XCTAssertGreaterThan(Data(contentsOf: destinationFolder.appendingPathComponent("metadata.json")).count, 0)
340350

341351
let unzippedFiles = try FileManager.default.contentsOfDirectory(atPath: destinationFolder.path)
342352

@@ -350,6 +360,7 @@ final class ZipTests: XCTestCase {
350360
XCTAssert(FileManager.default.fileExists(atPath: newDestinationFolder.appendingPathComponent("metadata.json").path))
351361
XCTAssert(FileManager.default.fileExists(atPath: newDestinationFolder.appendingPathComponent("main/index.html").path))
352362
XCTAssert(FileManager.default.fileExists(atPath: newDestinationFolder.appendingPathComponent("main/index/index.json").path))
363+
try XCTAssertGreaterThan(Data(contentsOf: newDestinationFolder.appendingPathComponent("metadata.json")).count, 0)
353364

354365
let newUnzippedFiles = try FileManager.default.contentsOfDirectory(atPath: newDestinationFolder.path)
355366
XCTAssertEqual(unzippedFiles, newUnzippedFiles)

0 commit comments

Comments
 (0)