Uncompressing folders in Swift
Questions
These are some questions you will need to ask yourself before properly doing this in the way I lay out.
- Where is my compressed folder and where do I want the decompressed folder?
- How do I handle access to the necessary files and data streams?
- How do I process these files and data streams?
Answers
Where is my compressed folder and where do I want the decompressed folder?
import AppleArchive import System let compressedFolderURL =
Bundle.main.url(forResource: "compressed", withExtension: "aar") let
compressedFolderFilePath = FilePath(url!.relativePath)
let decompressedPath = Bundle .main.resourceURL! .relativePath +
"/decompressed/" if !FileManager .default .fileExists(atPath:
decompressedPath) { do { try FileManager .default.createDirectory(
atPath: decompressedPath, withIntermediateDirectories: false) } catch {
fatalError("Unable to create destination directory.") } } let
decompressedFilePath = FilePath(decompressedPath)
How do I handle access to the necessary files and data streams?
let readFileStream = ArchiveByteStream.fileStream( path:
archiveFilePath, mode: .readOnly, options: [], permissions:
FilePermissions(rawValue: 0o644) ) let decompressStream =
ArchiveByteStream.decompressionStream( readingFrom: readFileStream! )
let decodeStream = ArchiveStream.decodeStream( readingFrom:
decompressStream! ) let extractStream = ArchiveStream.extractStream(
extractingTo: decompressedFilePath, flags:
[.ignoreOperationNotPermitted] )
How do I process these files and data streams?
do { try? ArchiveStream.process(readingFrom: decodeStream!, writingTo:
extractStream! ) } catch { print(error) }
Conclusion
There you have it a all in one guide to opening up folders in swift. I thought this was going to more straight forward, but I guess this isn't bad. Also, heads up you can't do .zip with native swift code non-trivally.