Zip Files from directory without creating a directory inside zip directory

I'm working with Swift 5.0 and I want to zip 3 files without installing additional dependencies.

Currently I'm able to zip 3 Files, which all are in the same directory, called "root". But if I open the created and exported zip-file (named: final.zip), than I get the folder (root) in which the 3 files were saved. I don't want this "root" folder inside my final.zip. I want to see my data directly inside the final.zip Below is the code.

let fm = FileManager.default
let baseDirectoryUrl = fm.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("root", isDirectory: true)

if !fm.fileExists(atPath: baseDirectoryUrl.relativePath) {
  try! fm.createDirectory(
    at: baseDirectoryUrl,
    withIntermediateDirectories: true,
    attributes: nil
  )
}

// path of data
let data1_path = baseDirectoryUrl.appendingPathComponent("data1.vec3")
let data2_path = baseDirectoryUrl.appendingPathComponent("data2.vec3")
let data3_path = baseDirectoryUrl.appendingPathComponent("data3.json")

// write wData1 to data1_path
try! wData1.write(to: data1_path)

// write wData2 to data2_path
try! wData2.write(to: data2_path)

// write wData3 to data3_path
try! wData3.write(to: data3_path)


// this will hold the URL of the zip file
var archiveUrl: URL?
// if we encounter an error, store it here
var error: NSError?

let coordinator = NSFileCoordinator()
// zip up the root directory
// this method is synchronous and the block will be executed before it returns
// if the method fails, the block will not be executed though
// if you expect the archiving process to take long, execute it on another queue

coordinator.coordinate(readingItemAt: baseDirectoryUrl, options: [.forUploading], error: &error) { (zipUrl) in
  // zipUrl points to the zip file created by the coordinator
  // zipUrl is valid only until the end of this block, so we move the file to a temporary folder
  let tmpUrl = try! fm.url(
    for: .itemReplacementDirectory,
    in: .userDomainMask,
    appropriateFor: zipUrl,
    create: true
  ).appendingPathComponent("export.zip", isDirectory: false)
  try! fm.copyItem(at: zipUrl, to: tmpUrl)

  // store the URL so we can use it outside the block
  archiveUrl = tmpUrl
}


if let archiveUrl = archiveUrl {
  // bring up the share sheet so we can send the archive with AirDrop for example
  let avc = UIActivityViewController(activityItems: [archiveUrl], applicationActivities: nil)
  avc.popoverPresentationController?.barButtonItem = navigationItem.rightBarButtonItem
  present(avc, animated: true)
} else {
  print(error)
}

I'm glad for everybody who can help

Zip Files from directory without creating a directory inside zip directory

The .forUploading trick always create a zip archive with paths that includes your root directory. If you don’t want this, you’ll need to use a proper zip archive API. There is not, alas, such an API on Apple platforms, so you’ll have to either write this code yourself or use a third-party library.

If you’d like to see us add a zip archive API in the future, I encourage you to file an enhancement request describing your requirements. While we may have seen similar requests many times before — indeed, I’ve filed my own bug about this (r. 22151959) — a fresh bug report will allow you to express your needs in your own terms, and allow iOS engineering to gauge the level of demand.

Please post your bug number, just for the record.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I'm not sure if you have tried the Zip framework from GitHub: https://github.com/marmelroy/Zip You may wish to give it a shot if you have not already.

Zip Files from directory without creating a directory inside zip directory
 
 
Q