Zip Archiving files in iOS App
ShowNote creates show files with a .snt
extension. This file contains the presentation database and resources. So, is it a new file format? No.
I don’t have time to create a new format. Therefore, I used a simple and easy way — a .zip
file, not zip function. It is a good solution to contain a number of files and reduce size as well.
How did I archive files with Zip? Apple doesn’t provide an official method for zipping.
Installation
I chose the ZipArchive
Pods library. Although it had not been maintained since 2015, I began developing ShowNote in 2015.
pod 'ZipArchive'
Now I can import ZipArchive
with some settings. Fortunately, building the old library succeeds even on iOS 17.
Common
To use this library, we need to create an instance of ZipArchive.
import ZipArchive
...
let zip = ZipArchive();
Archiving
We can start a zip file with createZipFile2
. It supports only the path, not the URL. For ShowNote, I named the file extension snt
. The append
parameter specifies whether to add a file to an existing zip file.
zip.createZipFile2(zipFileUrl.path(), append: false)
And I chose the compression type.
zip.compression = .best
createZipFile2
needs to be followed by a call to closeZipFile2
when you have finished adding files to it. Therefore, I appended a defer
statement to ensure it is called automatically.
defer {
zip.closeZipFile2()
}
Now that we have created an empty zip file, the next step is to append files to the archive. We need to specify which file to add to the zip and provide a new name for the archived file. If addFile
fails, it will return false.
zip.addFile(toZip: originalFileUrl.path(), newname: {new path under zip file});
In this case, where I need to archive a directory containing a number of files, I created a new extension addDirToZip
. However, it is too long to discuss here. It creates paths for all files under the directory and calls addFile
for each path. To notify progress, the progress handler will be invoked for each file appended. For the full source code, please check this repository.
zip.addDirToZip(baseUrl, progressHandler: progressHandler);
To check if the zip file is created, you can download the container. However, if the app is running on simulators, you can print/copy the path and open
the path from the terminal.
If you double tap the zip file, you can see the extracted file.
Unarchiving
Now we have our own file format. How can we extract it later?
Unzipping is very simple, just like archiving. First, open the zip file with unzipOpenFile
instead of createZipFile2
.
var result = zip.unzipOpenFile(zipFileUrl.path());
Second, create a new directory with the zip file name without the file extension.
let unzipDirUrl = zipFileURL.deletingPathExtension();
try? manager.createDirectory(at: unzipDirUrl,
withIntermediateDirectories: true)
Lastly, unzip the zip file to the created directory.
zip.unzipFile(to: unzipDirUrl.path(), overWrite: true)
Security
If your zip file requires security, you can specify a password.
Zipping:
zip.createZipFile2(zipFileUrl.path(),
append: false,
password: {password})
Unzipping:
zip.unzipOpenFile(zipFileUrl.path(),
password: {password})
Conclusion
Using zip, I succeed both reducing file size and encrypting document. I created this in 2015~2016, so there might be better library for archieving. However I introduced only the way I used for ShowNote here. Let me know and share more better libraries by commenting. Thanks.
The full source is in this example repository.
If you found this post helpful, please give it a round of applause 👏. Explore more iOS-related content in my other posts.
For additional insights and updates, check out my LinkedIn profile. Thank you for your support!
Troubleshootings
SDK does not contain ‘libarclite’ at the path
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '13.0'
end
end
end