Add export function

This commit is contained in:
Christoph Hagen
2025-01-31 13:06:11 +01:00
parent 740f776af6
commit 00e4da3f21
10 changed files with 189 additions and 13 deletions

View File

@@ -2,6 +2,7 @@ import Foundation
import Combine
import BinaryCodable
import SwiftUI
import ZIPFoundation
final class PersistentStorage: ObservableObject {
@@ -353,6 +354,34 @@ final class PersistentStorage: ObservableObject {
defer { updateTransferCount() }
return save(data: data, date: date, in: "transfers")
}
// MARK: Export
private var zipArchive: URL {
FileManager.default.temporaryDirectory.appendingPathComponent("data.zip")
}
func createZip() throws -> URL {
// Define the destination zip file path
let archive = zipArchive
// Create the zip file
try FileManager.default.zipItem(
at: PersistentStorage.documentDirectory,
to: archive,
shouldKeepParent: false,
compressionMethod: .deflate)
return archive
}
func removeZipArchive() throws {
let archive = zipArchive
// Remove existing zip file if it exists
if FileManager.default.fileExists(atPath: archive.path) {
print("Removing archive file")
try FileManager.default.removeItem(at: archive)
}
}
}
private extension Array where Element == TemperatureMeasurement {