快速解压缩zip文件并从Gmail API的base64数据中查找xml文件



这个问题是关于iOS 13中使用SwiftUI和Gmail API的DMARC报告查看器应用程序。报告由谷歌以xml格式邮寄到我们的管理员电子邮件ID,该格式将被压缩。所以基本上它是一个拉链附件。所以在这里,GMail API用于使用过滤器访问这些特定的邮件,并从API获取所有base64编码数据。还将其解码为数据类型数据。那远是可以的。下一部分是解压缩字节格式的zip文件数据并以字符串类型提取xml文件。然后我需要解析 XML。我想我可以弄清楚用XMLParser解析。

问题:如何解压缩Data类型的zip文件并从中获取String类型的xml文件?

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)

更新:我尝试了一个名为Zip的外部软件包,它也失败了。

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)
}

此代码导致以下错误

Error while unzipping: The operation couldn’t be completed. (Zip.ZipError error 1.)

终于找到了。正如参考文献 1 中提到的,电子邮件正文以 7 位 US-ASCII 数据编码。所以这就是base64解码不起作用的原因。

如 RFC1341 中所定义:

7BIT 的编码类型要求主体已经处于 七位邮件就绪表示形式。这是默认值 -- 也就是说,如果 内容传输编码标头字段不存在。

添加以下内容后,整个代码工作。

let edata: String = result.data.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/")

正如 Ref 2 中提到的,它只需要在从 gmail api 接收的 base64 数据中将"-"上的字符替换为"+",将"_"替换为"/"。

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)
}
}
}
}

参考文献 1: https://stackoverflow.com/a/58590759/2382813

参考文献 2: https://stackoverflow.com/a/24986452/2382813

最新更新