I am trying to zip files at destination path. Everything works perfectly. My files are zipped at the destination URL. But the problem is when I unzip, my files are inside the directory. I don't want my files inside a directory. When I unzip, I want to see my files.
This is my code:
func zipData() {
let path=NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true).first!
let fileManager = FileManager()
var sourceURL = URL(fileURLWithPath: path)
sourceURL.appendPathComponent("/cropsapdb_up_\(useridsaved)")
var destinationURL = URL(fileURLWithPath: path)
destinationURL.appendPathComponent("/cropsapdb_up_\(useridsaved).zip")
do {
let fm = FileManager.default
let items = try fm.contentsOfDirectory(atPath: sourceURL.path)
guard let archive = Archive(url: destinationURL, accessMode: .create) else {
print("returning")
return
}
for item in items {
sourceURL = sourceURL.appendingPathComponent("/\(item)")
try archive.addEntry(with: sourceURL.lastPathComponent, relativeTo: sourceURL.deletingLastPathComponent())
guard let archive = Archive(url: destinationURL, accessMode: .update) else {
print("returning")
return
}
sourceURL.deleteLastPathComponent()
}
} catch {
}
I am the author of ZIP Foundation, the library you are using.
If I understand your code correctly you want to recursively add the contents of a directory to a ZIP archive.
To achieve this, you can use the convenience method
zipItemwhich is implemented as extension toFileManagerin ZIP Foundation.By default, it behaves like the Archive Utility on macOS and includes the last directory name of the
sourceURLas root directory of the archive. To alter that behaviour (as pointed out by Leo Dabus in the comments), you can pass the optionalshouldKeepParent: falseparameter:(I added a fictional
let useridsaved = 1local variable to make your sample compilable)To verify that the archive does indeed not include a root directory, you can use the
zipinfocommand line utility shipping with macOS.It is also possible, that the ZIP code on your server implicitly creates a directory when unpacking your archive.