aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuard/WireGuard/ZipArchive/ZipArchive.swift
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-10-29 22:56:25 +0530
committerRoopesh Chander <roop@roopc.net>2018-10-29 23:06:55 +0530
commit6fe0eb0bcf892e0abcf20c493c60370edc0d588d (patch)
tree8837a8d4f0d57f4f6137f6890f43f5d0522bb90b /WireGuard/WireGuard/ZipArchive/ZipArchive.swift
parentmv Parsing/ ConfigFile/ (diff)
downloadwireguard-apple-6fe0eb0bcf892e0abcf20c493c60370edc0d588d.tar.xz
wireguard-apple-6fe0eb0bcf892e0abcf20c493c60370edc0d588d.zip
Export: Exporting config files
Signed-off-by: Roopesh Chander <roop@roopc.net>
Diffstat (limited to 'WireGuard/WireGuard/ZipArchive/ZipArchive.swift')
-rw-r--r--WireGuard/WireGuard/ZipArchive/ZipArchive.swift18
1 files changed, 18 insertions, 0 deletions
diff --git a/WireGuard/WireGuard/ZipArchive/ZipArchive.swift b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift
index ea6df174..06815889 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<UInt8>) -> 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)] = []