ZipFileContainer.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import Foundation
  21. import ZipArchive
  22. import CocoaLumberjackSwift
  23. class ZipFileContainer: NSObject {
  24. private var zipFilePath: String
  25. private var password: String
  26. private var zipFile: SSZipArchive
  27. static let directoryPath = NSTemporaryDirectory() + "Export/"
  28. init(password: String, name: String) {
  29. self.zipFilePath = ZipFileContainer.getZipFilePath(name: name)
  30. self.password = password
  31. self.zipFile = SSZipArchive.init(path: self.zipFilePath)
  32. self.zipFile.open()
  33. super.init()
  34. }
  35. /// Adds data to the zip file in _zipfile and encrypts it with _password and AES.
  36. /// Does not return an error if it does not succeed
  37. /// - Parameters:
  38. /// - data: The data that will be added to the zipFile
  39. /// - filename: The filename with which data is added to the zip file
  40. func addData(data: Data, filename: String) -> Bool {
  41. let success = self.zipFile.write(data,
  42. filename: filename,
  43. compressionLevel: 0,
  44. password: self.password,
  45. aes: true)
  46. return success
  47. }
  48. func addMediaData(mediaData: BlobData) -> Bool {
  49. return self.addData(data: mediaData.blobGet(), filename: mediaData.blobGetFilename())
  50. }
  51. func deleteFile() {
  52. self.zipFile.close()
  53. let fileManager = FileManager.default
  54. if fileManager.isDeletableFile(atPath: self.zipFilePath) {
  55. do {
  56. try fileManager.removeItem(atPath: self.zipFilePath)
  57. } catch {
  58. DDLogError("Unable to delete chat export from temporary files.")
  59. }
  60. }
  61. }
  62. static func cleanFiles() {
  63. let fileManager = FileManager.default
  64. if fileManager.fileExists(atPath: directoryPath) {
  65. do {
  66. try fileManager.removeItem(atPath: directoryPath)
  67. } catch let error as NSError {
  68. DDLogError("Error: \(error.localizedDescription)")
  69. }
  70. }
  71. }
  72. func getUrlWithFileName(fileName: String) -> URL? {
  73. self.zipFile.close()
  74. let path = ZipFileContainer.getZipFilePath(name: fileName)
  75. let fileManager = FileManager.default
  76. do {
  77. try fileManager.moveItem(atPath: self.zipFilePath, toPath: path)
  78. return NSURL.fileURL(withPath: path)
  79. } catch {
  80. DDLogError("An error occurred when moving the zip file within temporary files.")
  81. DDLogError("Unexpected error: \(error).")
  82. }
  83. return nil
  84. }
  85. private static func getZipFilePath(name: String) -> String {
  86. return ZipFileContainer.getDirectoryPath() + name + ".zip"
  87. }
  88. private static func getDirectoryPath() -> String {
  89. let fileManager = FileManager.default
  90. if !fileManager.fileExists(atPath: directoryPath) {
  91. do {
  92. try fileManager.createDirectory(atPath: directoryPath,
  93. withIntermediateDirectories: true, attributes: nil)
  94. } catch let error as NSError {
  95. DDLogError("Error: \(error.localizedDescription)")
  96. }
  97. }
  98. return directoryPath
  99. }
  100. }