Uncompressing folders in Swift

So imagine you need to get multiple files and folders from an API. One option for doing so is to get all the file names and request them from what ever file server you are using. This is terrible don't do this. The optimal way is to bundle the entire directory into a compressed format and distribute that one file. Okay great, say you needed these files in an iOS/iPadOS or MacOS application. That means you will need to decompress the files that you received in swift.

Questions

These are some questions you will need to ask yourself before properly doing this in the way I lay out.

  1. Where is my compressed folder and where do I want the decompressed folder?
  2. How do I handle access to the necessary files and data streams?
  3. 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.