From 6fe0eb0bcf892e0abcf20c493c60370edc0d588d Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Mon, 29 Oct 2018 22:56:25 +0530 Subject: Export: Exporting config files Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/ZipArchive/ZipArchive.swift | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'WireGuard/WireGuard/ZipArchive/ZipArchive.swift') diff --git a/WireGuard/WireGuard/ZipArchive/ZipArchive.swift b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift index ea6df17..0681588 100644 --- a/WireGuard/WireGuard/ZipArchive/ZipArchive.swift +++ b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift @@ -5,11 +5,29 @@ import Foundation enum ZipArchiveError: Error { case cantOpenInputZipFile + case cantOpenOutputZipFileForWriting case badArchive } class ZipArchive { + static func archive(inputs: [(fileName: String, contents: Data)], to destinationURL: URL) throws { + let destinationPath = destinationURL.path + guard let zipFile = zipOpen(destinationPath, APPEND_STATUS_CREATE) else { + throw ZipArchiveError.cantOpenOutputZipFileForWriting + } + for input in inputs { + let fileName = input.fileName + let contents = input.contents + zipOpenNewFileInZip(zipFile, fileName.cString(using: .utf8), nil, nil, 0, nil, 0, nil, Z_DEFLATED, Z_DEFAULT_COMPRESSION) + contents.withUnsafeBytes { (ptr: UnsafePointer) -> Void in + zipWriteInFileInZip(zipFile, UnsafeRawPointer(ptr), UInt32(contents.count)) + } + zipCloseFileInZip(zipFile) + } + zipClose(zipFile, nil) + } + static func unarchive(url: URL, requiredFileExtensions: [String]) throws -> [(fileName: String, contents: Data)] { var results: [(fileName: String, contents: Data)] = [] -- cgit v1.2.3-59-g8ed1b