Skip to content
Open
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
16 changes: 14 additions & 2 deletions Zip/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,10 @@ public class Zip {
throw ZipError.unzipFail
}

//Set file permissions from current fileInfo
// Set file permissions from current fileInfo
if fileInfo.external_fa != 0 {
let permissions = (fileInfo.external_fa >> 16) & 0x1FF
//We will devifne a valid permission range between Owner read only to full access
// We will define a valid permission range between Owner read only to full access
if permissions >= 0o400 && permissions <= 0o777 {
do {
try fileManager.setAttributes([.posixPermissions : permissions], ofItemAtPath: fullPath)
Expand Down Expand Up @@ -348,6 +348,18 @@ public class Zip {
if let fileSize = fileAttributes[FileAttributeKey.size] as? Double {
currentPosition += fileSize
}
if let permissions = fileAttributes[FileAttributeKey.posixPermissions] as? UInt {
// We will store a valid permission range between Owner read only to full access
if permissions >= 0o400 && permissions <= 0o777 {
var new_external_fa: UInt = zipInfo.external_fa
let permissionsBits: UInt = 0o777 << 16
// set bits responsible for permissions
new_external_fa = new_external_fa & ~permissionsBits
new_external_fa = new_external_fa | (permissions << UInt(16))

zipInfo.external_fa = new_external_fa
}
}
}
catch {}
guard let buffer = malloc(chunkSize) else {
Expand Down
Empty file added ZipTests/Resources/execution
Empty file.
1 change: 1 addition & 0 deletions ZipTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extension ZipTests {
("testUnzipPermissions", testUnzipPermissions),
("testUnzipWithUnsupportedPermissions", testUnzipWithUnsupportedPermissions),
("testZip", testZip),
("testZipPermissions", testZipPermissions),
("testZipUnzipPassword", testZipUnzipPassword),
]
}
Expand Down
20 changes: 20 additions & 0 deletions ZipTests/ZipTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,24 @@ class ZipTests: XCTestCase {
XCTAssertTrue(Zip.isValidFileExtension("zip"))
XCTAssertTrue(Zip.isValidFileExtension("cbz"))
}

func testZipPermissions() throws {
let executionURL = url(forResource: "execution")!
let zipDestination = try Zip.quickZipFiles([executionURL], fileName: "execution.zip")
addTeardownBlock {
try? FileManager.default.removeItem(at: zipDestination)
}
let unzipDestination = try Zip.quickUnzipFile(zipDestination)
addTeardownBlock {
try? FileManager.default.removeItem(at: unzipDestination)
}

let fileManager = FileManager.default
let unzippedFile = unzipDestination.appendingPathComponent("execution")
let unzippedFileAttributes = try fileManager.attributesOfItem(atPath: unzippedFile.path)
let permissions = unzippedFileAttributes[.posixPermissions] as? Int ?? 0
// Git stores only the owner execution bit - observe only that
let ownerExecutionBit = 0o100
XCTAssertEqual(permissions & ownerExecutionBit, ownerExecutionBit)
}
}