gpt4 book ai didi

ios - ZipArchive 跳过文件 49-100

转载 作者:行者123 更新时间:2023-11-30 14:09:23 25 4
gpt4 key购买 nike

我有一个函数,应该创建一批文件,将它们写入一个目录,压缩该目录,然后将其附加到电子邮件中。

当我运行它时,它告诉我它已经写入了 299 个文件,但是当我打开 zip 文件夹时,正好缺少 50 个文件

这是代码。我想过删减它,但我害怕删除一些可能帮助别人发现问题的东西:

func exportAllData() {
var totalExports = 0

// fetch all the data first
// 1 Describe what you want
let fetchRequest = NSFetchRequest(entityName: "Fish")
let sortDescriptor = NSSortDescriptor(key: "time", ascending: false)
fetchRequest.sortDescriptors = [sortDescriptor]

// 2 Get it!
do {

let allCatches = try managedContext!.executeFetchRequest(fetchRequest) as? [NSManagedObject]
totalExports = allCatches!.count

// Create a folder to store the catches in
// path to where data will be written
let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let applicationDocumentsDirectory:NSURL = urls[urls.count-1]
let destinationURL = applicationDocumentsDirectory.URLByAppendingPathComponent("filesToExport", isDirectory: true)
do { try NSFileManager.defaultManager().createDirectoryAtURL(destinationURL, withIntermediateDirectories: false, attributes: nil)
print("created directory")

var currentFile = 0

// Write each fish to file
for fish in allCatches! {

currentFile+= 1

// convert NSManagedObject into a Dictionary, serialize it to NSData, and write it to file
let keys = fish.entity.propertiesByName.keys.array
let dictionary = fish.dictionaryWithValuesForKeys(keys)
let data = NSKeyedArchiver.archivedDataWithRootObject(dictionary)

let fileName = "catchNumber\(currentFile)"
let fileURL = destinationURL.URLByAppendingPathComponent(fileName)

let success = data.writeToFile(fileURL.path!, atomically: false)
if success { print("successfully exported: "+fileName) } else { print("FAILED to export: "+fileName) }
}

// DEBUG
do {
let contents = try NSFileManager.defaultManager().contentsOfDirectoryAtPath(destinationURL.path!)
print(contents)
} catch {
print(error)
}

// After all the fish have been written to file, zip the file
let zipURL = applicationDocumentsDirectory.URLByAppendingPathComponent("exported.zip")
Main.createZipFileAtPath(zipURL.path!, withContentsOfDirectory: destinationURL.path!)

// Load the zip file up as data and send it out in an email
let zipData = NSData(contentsOfURL: zipURL)

let picker = MFMailComposeViewController()
picker.mailComposeDelegate = self
picker.setSubject("Shared Catch Log! (\(totalExports) entries)")
picker.setMessageBody("Open the attachment to this email on an iOS device with Catch Stats installed on it to import all catch into your Catch Log!", isHTML: false)
picker.addAttachmentData(zipData!, mimeType: "applcation/CatchStats", fileName: "CatchStatsLog.csl")
presentViewController(picker, animated: true, completion: nil)

// If failed to created directory
} catch {
print("directory already existed")
}

// If failed to execute fetch request
} catch {
print(error)
self.navigationController?.popViewControllerAnimated(true)
}
}

现在,在 //Debug 中,当我检查要压缩的文件夹的内容时,它有 299 个文件,编号从 1 到 299。

但是当我通过电子邮件发送文件并打开它时,50-99 全部丢失。有人可以给我一些建议吗?

我从 ZipArchive 的 Main.m 文件中提取了这段代码。这就是它正在做的事情。

    + (BOOL)createZipFileAtPath:(NSString *)path
withContentsOfDirectory:(NSString *)directoryPath {
return [self createZipFileAtPath:path withContentsOfDirectory:directoryPath keepParentDirectory:NO];
}

+ (BOOL)createZipFileAtPath:(NSString *)path
withContentsOfDirectory:(NSString *)directoryPath
keepParentDirectory:(BOOL)keepParentDirectory {
BOOL success = NO;
NSFileManager *fileManager = nil;
Main *zipArchive = [[Main alloc] initWithPath:path];

if ([zipArchive open]) {
// Use a local file manager (queue/thread compatibility)
fileManager = [[NSFileManager alloc] init];

NSDirectoryEnumerator *directoryEnumerator = [fileManager enumeratorAtPath:directoryPath];
NSString *fileName;

while ((fileName = [directoryEnumerator nextObject])) {
BOOL isDirectory;
NSString *fullFilePath = [directoryPath stringByAppendingPathComponent:fileName];
[fileManager fileExistsAtPath:fullFilePath isDirectory:&isDirectory];

if (!isDirectory) {
if (keepParentDirectory) {
fileName = [[directoryPath lastPathComponent] stringByAppendingPathComponent:fileName];
}

[zipArchive writeFileAtPath:fullFilePath withFileName:fileName];
} else {
if (0 == [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:fullFilePath error:nil].count) {
NSString *temporaryName = [fullFilePath stringByAppendingPathComponent:@".DS_Store"];
[@"" writeToFile:temporaryName atomically:YES encoding:NSUTF8StringEncoding error:nil];
[zipArchive writeFileAtPath:temporaryName withFileName:[fileName stringByAppendingPathComponent:@".DS_Store"]];
[[NSFileManager defaultManager] removeItemAtPath:temporaryName error:nil];
}
}
}

success = [zipArchive close];
}

return success;

}

最佳答案

我找到了一个解决方案,但它更多的是一种解决方法。经过大量测试后,我意识到 ZipArchive 无法压缩包含超过 250 个文件的文件夹。文件的名称或顺序并不重要。

我没有序列化每个 NSDictionary 并将其写入文件,而是创建了一个 NSDictionaries 数组,然后将其序列化。这样 ZipArchive 只需要压缩一个文件,而且现在似乎工作正常。

关于ios - ZipArchive 跳过文件 49-100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31925011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com