Skip to content

Commit

Permalink
Try renaming files in Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
fpseverino committed Oct 4, 2024
1 parent 76f1c5a commit 66ef006
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
22 changes: 20 additions & 2 deletions Sources/Zip/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public class Zip {
if unzGoToFirstFile(zip) != UNZ_OK {
throw ZipError.unzipFail
}

#if os(Windows)
let fileNames = Set<String>()
#endif

repeat {
if let cPassword = password?.cString(using: String.Encoding.ascii) {
ret = unzOpenCurrentFilePassword(zip, cPassword)
Expand All @@ -96,8 +101,21 @@ public class Zip {
var pathString = String(cString: fileName)

#if os(Windows)
guard pathString.rangeOfCharacter(from: ["<", ">", ":", "\"", "|", "?", "*"]) == nil else {
throw ZipError.unzipFail
// Windows Reserved Characters
let reservedCharacters: CharacterSet = ["<", ">", ":", "\"", "|", "?", "*"]

if pathString.rangeOfCharacter(from: reservedCharacters) != nil {
for character in reservedCharacters {
pathString = pathString.replacingOccurrences(of: character, with: "_")
}

var counter = 1
while fileNames.contains(pathString) {
pathString = pathString.replacingOccurrences(of: ".", with: "_\(counter).")
counter += 1
}

fileNames.insert(pathString)
}
#endif

Expand Down
3 changes: 0 additions & 3 deletions Tests/ZipTests/ZipTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,6 @@ final class ZipTests: XCTestCase {
try XCTAssertGreaterThan(Data(contentsOf: destinationPath.appendingPathComponent("kYkLkPf.gif")).count, 0)
}

// This Zip file contains reserved characters that are not allowed on Windows.
#if !os(Windows)
// Tests if https://github.com/vapor-community/Zip/issues/4 does not occur anymore.
func testRoundTripping() throws {
// "prod-apple-swift-metrics-main-e6a00d36.zip" is the original zip file from the issue.
Expand Down Expand Up @@ -403,5 +401,4 @@ final class ZipTests: XCTestCase {
let newUnzippedFiles = try FileManager.default.contentsOfDirectory(atPath: newDestinationFolder.path)
XCTAssertEqual(unzippedFiles, newUnzippedFiles)
}
#endif
}

0 comments on commit 66ef006

Please sign in to comment.