iOS で Zip を展開する

はじめに

ZipArchive ライブラリを使用すると iOS で Zip を展開(圧縮も)できる。 これは macOS も tvOS にも対応している。

ここでは Carthage での導入について書く。

Carthage でのライブラリ導入について基本的なことは https://daisuke-t-jp.hatenablog.com/entry/2019/08/14/123730 に書いてある。

Cartfile を準備する

Cartfile に以下を記載する

github "ZipArchive/ZipArchive"

Carthage でライブラリをインストールする

Cartfile があるディレクトリで carthage update を実行する。

$ carthage update
*** Cloning ZipArchive
*** Checking out ZipArchive at "v2.2.2"
*** xcodebuild output can be found in /var/folders/x7/rtyldl812l3f40rb6bpvx8nc0000gp/T/carthage-xcodebuild.GyPErm.log
*** Downloading ZipArchive.framework binary at "v2.2.2"

ZipArchive.framework ができれば成功。

展開する

ZipArchive.framework をプロジェクトにリンクした状態で

import ZipArchive

// 展開する
//   zipPath ... Zip ファイルのパス
//   toDestination ... 展開した Zip の内容の出力先
SSZipArchive.unzipFileAtPath(zipPath, toDestination: toDestinationPath)

これで展開できるはず。

関連