Swift ZIPFoundation extract string in memory is not working - ios

I am using ZipFoundation in Swift from https://github.com/weichsel/ZIPFoundation
My requirement is unzip the file contents in memory and directly convert into String.
let archive = Archive(url: fileUrl, accessMode: .read, preferredEncoding: .utf8)
do{
try archive?.extract(entry, consumer: {data in
print(data.count)
})
}catch{}
The archive object is always null its not reading zip file. Also what is the entry object to pass to extract method?

ZIP Foundation archives support subscripting. This allows you to obtain an Entry by subscripting into an Archive via archive["path/to/file.txt"].
To get access to the contents of the obtained file, you use the closure-based version of extract as follows:
guard let archiveURL = Bundle.main.url(forResource: "archive", withExtension: "zip"),
let archive = Archive(url: archiveURL, accessMode: .read),
let entry = archive["test/data.random"]
else { return }
_ = try? archive.extract(entry) { data in
print(data.count)
}

Related

Create zip file without third party libraries and parent directory in swift

I need to create a simple zip file with files included without parent directory in swift.
I found a good solution from Robin Kunde https://recoursive.com/2021/02/25/create_zip_archive_using_only_foundation/ but the files have a parent directory in the zip file.
So when you unzip the files, there is a directory with the name e.g. "Documents".
I don't need real compression or other zip features. Our backend server checks the content and the folder must be removed.
I don't think, that this is even possible without a C-library :(
I tried to decode the ZIP-file to UTF-8, delete the folder names and save the data to a file. The file was corrupt.
let fm = FileManager.default
let baseDirectoryUrl = fm.urls(for: .documentDirectory, in: .userDomainMask).first!
let data = try! Data(contentsOf: baseDirectoryUrl.appendingPathComponent("archive.zip"))
let str = String(decoding: data, as: UTF8.self)
let newstr = str.replacingOccurrences(of: "Documents/", with: "")
do {
try newstr.write(to: baseDirectoryUrl.appendingPathComponent("archive_without_parent_name.zip"), atomically: true, encoding: .utf8)
} catch {
print(error.localizedDescription)
}

Swift Unzipping zip file and find xml file from base64 data from Gmail API

This question is regarding a DMARC report viewer app in iOS 13 using SwiftUI and Gmail API. The reports are mailed to our admin email id by google in xml format which will be zipped. So basically it is a zip attachment. So here, GMail API is used to access those specific mail using filter and got all the base64 encoded data from API. Also decoded it to Data type data. That far is OK. Next part is where data of zip file in byte format is decompressed and extract xml file inside in String type. Then I need to parse XML. I think I can figure out parsing with XMLParser.
Question: how to decompress zip file in Data type and get xml file from it as String type?
INPUT: String in Base64 format from GMail API fetch (A zip file attachment with only 1 xml file inside)
OUTPUT: String in XML format
PLATFORM: iOS 13/Swift 5.2/SwiftUI/Xcode 11.4
ACTION:
(INPUT)
base64: String | Decode -> Data
attachment.zip: Data | Decompress -> [Data]
ListOfFiles: [Data] | FirstIndex -> Data
dmarc.xml: Data | ContentOfXML -> String
(OUTPUT)
Update: I have tried an external package called Zip and it also failed.
let path = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let url = path.appendingPathComponent(messageId+".zip")
do {
try data.write(to: url)
} catch {
print("Error while writing: "+error.localizedDescription)
}
do {
let unzipDirectory = try Zip.quickUnzipFile(url)
print(unzipDirectory)
} catch let error as NSError {
print("Error while unzipping: "+error.localizedDescription)
}
This code resulted in following error
Error while unzipping: The operation couldn’t be completed. (Zip.ZipError error 1.)
Finally I found it. As it is mentioned in Ref 1,The email bodies are encoded in 7-bit US-ASCII data. So this is why the base64 decoding did not work.
As defined in the rfc1341:
An encoding type of 7BIT requires that the body is already in a
seven-bit mail- ready representation. This is the default value --
that is, "Content-Transfer-Encoding: 7BIT" is assumed if the
Content-Transfer-Encoding header field is not present.
The whole code worked after adding the following.
let edata: String = result.data.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
As it is mentioned in Ref 2, it just need character replacement on '-' with '+' and '_' with '/' inside base64 data received from gmail api.
func getAttachedData(messageId: String, attachmentId: String) {
decode(self.urlBase+messageId+"/attachments/"+attachmentId+"?"+self.urlKey) { (result: Attachment) in
let edata: String = result.data.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")
if let data = Data(base64Encoded: edata, options: .ignoreUnknownCharacters) {
let filemanager = FileManager.default
let path = try! filemanager.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let url = path.appendingPathComponent(messageId+".zip")
do {
try data.write(to: url)
} catch {
print("Error while writing: "+error.localizedDescription)
}
do {
let unzipDirectory = try Zip.quickUnzipFile(url)
print("Unzipped")
do {
let filelist = try filemanager.contentsOfDirectory(at: unzipDirectory, includingPropertiesForKeys: [], options: [])
for filename in filelist {
print(filename.lastPathComponent)
print(filename.relativeString)
do {
let text = try String(contentsOf: filename, encoding: .utf8)
print(text)
DispatchQueue.main.async {
self.attachments.append(text)
}
} catch let error as NSError {
print("Error: \(error.localizedDescription)")
}
}
} catch let error {
print("Error: \(error.localizedDescription)")
}
} catch let error as NSError {
print("Error while unzipping: "+error.localizedDescription)
}
}
}
}
Ref 1: https://stackoverflow.com/a/58590759/2382813
Ref 2: https://stackoverflow.com/a/24986452/2382813

Saving to xml document Swift

I'm using this code to save a string to an xml file in my project
let saveTo = statisticsI.toXMLString()
let filepath = Bundle.main.url(forResource: "statistics", withExtension: "xml")
let filepathAlt = Bundle.main.path(forResource: "statistics", ofType: "xml")
print(filepathAlt!)
do {
try saveTo.write(to: filepath!, atomically: false, encoding: String.Encoding.utf8)
let contents = try String(contentsOfFile: filepathAlt!)
print("FILE CONTENTS \(contents)")
}
catch let error as NSError {
print("Error writing values \(error)")
}
Printing the file contents returns the xml correctly, but when I stop running the application, the file hasn't been updated
When the above code is run, the file has already been read by a seperate function. Is the fact that the file has already been accessed (and its path still stored in a variable) the issue?
when I stop running the application, the file hasn't been updated
You can't write into the application bundle in iOS. Try using a path into the Documents folder or some other place within the app's sandbox.

Multiple folder with Json file of same name swift 3

I'm facing difficulty to fetch the Json File.In my project there is one folder,in that folder multiple sub folders are there.And in every sub folder there is two json file.Json file name is common in every sub folder.Then how I fetch json file with subfolder name? Which means i want to differentiate json file through sub folder name.I'm new in Swift.Thank you.
(NS)Bundle provides a very convenient way to address subfolders, the optional parameter subdirectory of url(forResource: withExtension:)
if let jsonFileURL = Bundle.main.url(forResource: "jsonFile",
withExtension: "json",
subdirectory: "Subfolder1/SubFolder2") {
let json = try! String(contentsOf: jsonFileURL, encoding: .utf8)
print(json)
} else { fatalError("Check the file paths") }
You can get the file from different folders by providing the actual folder name in the document path.
Make sure that your files are added to resources.
if let docsPath = Bundle.main.resourcePath! + "/Resources/subfolder" {
let fileManager = FileManager.default
do {
let docsArray = try fileManager.contentsOfDirectory(atPath: docsPath)
//get the required json file from the documents array
} catch {
print(error)
}
}

Invalid filename/path when getting JSON

In my project Navigator I have this structure
-MyApp
--ViewController.swift
--AppDelegate.Swift
--Main.StoryBoard
--info.plist
--JSONFiles
---test.json
-MyAppUITests
As you can see, this is the very basic structure that happens when you create a new single view application.
I created a new group called JSONFiles and added in a JSON files called test.
When I try to get the file using:
if let path = Bundle.main.path(forResource: "JSONFiles/test", ofType: "json") {
do {
let data = try NSData(contentsOf: URL(fileURLWithPath: path), options: NSData.ReadingOptions.mappedIfSafe)
let jsonData : NSData = NSData(contentsOfFile: path)!
allEntries = (try! JSONSerialization.jsonObject(with: jsonData as Data, options: JSONSerialization.ReadingOptions.mutableContainers)) as! NSArray
print(allEntries)
} catch let error as NSError {
print(error.localizedDescription)
}
} else {
print("Invalid filename/path.")
}
I get the error:
Invalid filename/path.
If I move the JSON file our of the group folder and change the forResource to just "test" it works fine and prints in the console.
Can anyway tell me how to make it read from the folder? I could have all my JSON files in the root but I am wanting to tidy it up slightly.
Thanks
You must include your json file into "Copy Bundle Ressources". Go to your project target -> Build phases -> Copy Bundle ressources and there, add your json file.
Then, you should be able to retrieve the path using the function: Bundle.main.path(forResource: "test", ofType: "json") .

Resources