ZIPFoundation lib를 사용하여 Swift의 압축 파일에 파일을 추가하려고합니다.ZIPFoundation을 사용하여 원하는 경로의 아카이브에 파일을 추가하는 방법은 무엇입니까?
/ /folder1/ /folder2/ <-- Move file here.
는 현재 아카이브에 내 파일을 추가하려면이 옵션을 사용하여,하지만 난 매개 변수의 논리를하지 않는 :
아카이브는 다음과 같습니다
public func addEntry(with path: String, relativeTo baseURL: URL)
Archive
개체를 만들고 addEntry()
을 사용하여 파일을 추가하는 방법은 파일을 보관 경로의 루트 경로에 추가하지 않는 방법이 있습니까?
코드 편집 : ZIP 아카이브의
internal func moveLicense(from licenseUrl: URL, to publicationUrl: URL) throws {
guard let archive = Archive(url: publicationUrl, accessMode: .update) else {
return
}
// Create local folder to have the same path as in the archive.
let fileManager = FileManager.default
var urlMetaInf = licenseUrl.deletingLastPathComponent()
urlMetaInf.appendPathComponent("META-INF", isDirectory: true)
try fileManager.createDirectory(at: urlMetaInf, withIntermediateDirectories: true, attributes: nil)
let uuu = URL(fileURLWithPath: urlMetaInf.path, isDirectory: true)
// move license in the meta-inf folder.
try fileManager.moveItem(at: licenseUrl, to: uuu.appendingPathComponent("license.lcpl"))
// move dir
print(uuu.lastPathComponent.appending("/license.lcpl"))
print(uuu.deletingLastPathComponent())
do {
try archive.addEntry(with: uuu.lastPathComponent.appending("license.lcpl"), // Missing '/' before license
relativeTo: uuu.deletingLastPathComponent())
} catch {
print(error)
}
}
// This is still testing code, don't mind the names :)
마지막 catch 블록에 오류가 인쇄되면 ZIPFoundation이 'url'에서 파일에 액세스 할 수 없음을 의미합니다. 앱에 내장 API가있는 특정 파일에 대한 액세스 권한이 있는지 확인할 수 있습니까? 예 : guard let content = 시도 하시겠습니까? 데이터 (contentsOf : url) else {return} 이것도 실패하면 단순히 권한 문제 일뿐입니다. 앱의 컨테이너 밖에있는 파일을 가리키는 URL입니까? –
예이 콘텐츠에 대한 액세스 권한이 있습니다. 위 코드를 사용하면 'content'가 제대로 초기화됩니다. –
입력 경로에 경로 구분 기호가 없습니다. print 문에서 .appending ("/ license.lcpl")을 사용하는 동안 addEntry의 첫 번째 인수에 "/"를 추가하지 마십시오. –