[swift] 파일 압축 및 해제

이 글에서는 Swift 프로그래밍 언어를 사용하여 파일을 압축하고 해제하는 방법에 대해 알아보겠습니다.

파일 압축

Swift에서 파일을 압축하기 위해서는 FileManagerSSZipArchive 라이브러리를 사용할 수 있습니다.

먼저, SSZipArchive 라이브러리를 프로젝트에 추가하고, 다음과 같이 파일을 압축할 수 있습니다.

import SSZipArchive

let inputPath = "/path/to/input_directory"
let zipPath = "/path/to/output_zipfile.zip"

SSZipArchive.createZipFile(atPath: zipPath, withContentsOfDirectory: inputPath)

위 코드는 inputPath에서 지정한 디렉토리를 zipPath에 지정한 이름의 ZIP 파일로 압축합니다.

파일 해제

압축된 파일을 해제하는 방법은 다음과 같습니다.

let zipPath = "/path/to/input_zipfile.zip"
let destinationPath = "/path/to/output_directory"

SSZipArchive.unzipFile(atPath: zipPath, toDestination: destinationPath)

zipPath에서 지정한 ZIP 파일을 destinationPath에 지정한 디렉토리로 해제합니다.

이제, Swift를 사용하여 파일을 쉽게 압축하고 해제할 수 있게 되었습니다.

참고 자료

이제 부터는 Swift에서 파일을 압축하고 해제하는 방법에 대해 자세히 알아볼 것입니다.