How to Create and Encrypt a ZIP File in Swift

If you have large files you need to share, compressing them into a zip archive can be a helpful solution to save storage space and ensure successful email delivery. However, if the files contain sensitive information, you may wish to encrypt and protect them with a password as well. The following API will allow you to achieve both of those goals via a single process in Swift; all you need for the operation are the input file(s), password for the output file, and encryption algorithm.

To run the API, simply add the above parameters into the following code, along with your API key:

import Foundation
#if canImport(FoundationNetworking)
import FoundationNetworking
#endif
var semaphore = DispatchSemaphore (value: 0)let parameters = [
[
"key": "inputFile1",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile2",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile3",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile4",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile5",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile6",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile7",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile8",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile9",
"src": "/path/to/file",
"type": "file"
],
[
"key": "inputFile10",
"src": "/path/to/file",
"type": "file"
]] as [[String : Any]]
let boundary = "Boundary-\(UUID().uuidString)"
var body = ""
var error: Error? = nil
for param in parameters {
if param["disabled"] == nil {
let paramName = param["key"]!
body += "--\(boundary)\r\n"
body += "Content-Disposition:form-data; name=\"\(paramName)\""
if param["contentType"] != nil {
body += "\r\nContent-Type: \(param["contentType"] as! String)"
}
let paramType = param["type"] as! String
if paramType == "text" {
let paramValue = param["value"] as! String
body += "\r\n\r\n\(paramValue)\r\n"
} else {
let paramSrc = param["src"] as! String
let fileData = try NSData(contentsOfFile:paramSrc, options:[]) as Data
let fileContent = String(data: fileData, encoding: .utf8)!
body += "; filename=\"\(paramSrc)\"\r\n"
+ "Content-Type: \"content-type header\"\r\n\r\n\(fileContent)\r\n"
}
}
}
body += "--\(boundary)--\r\n";
let postData = body.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://api.cloudmersive.com/convert/archive/zip/create/encrypted")!,timeoutInterval: Double.infinity)
request.addValue("<string>", forHTTPHeaderField: "password")
request.addValue("<string>", forHTTPHeaderField: "encryptionAlgorithm")
request.addValue("multipart/form-data", forHTTPHeaderField: "Content-Type")
request.addValue("YOUR-API-KEY-HERE", forHTTPHeaderField: "Apikey")
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()

Process complete! Your brand new and fully secure zip archive will be ready to go.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store