我让我的应用程序创建一个文件,将其压缩为 .zip 文件,然后将其附加到电子邮件中。但是 .zip 文件在实际文件之前包含许多目录。从“/”开始,它是一个无名文件夹,对接收它的人来说是不可见的。顶级目录是'/',下一级是'com'...
/var/mobile/Applications/CDA16BB231BDABABBA/Documents/data.txt
如何删除所有目录并让 .zip 文件仅包含该文件?
这是我使用的代码...
NSError *error;
NSString *documentsDirectory = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [documentsDirectory
stringByAppendingPathComponent:@"data.txt"];
NSLog(@"string to write:%@",printString);
[printString writeToFile:filePath atomically:YES
encoding:NSUTF8StringEncoding error:&error];
////ZIP FILE/////
NSString *zipfilePath = [documentsDirectory stringByAppendingPathComponent:@"data.zip"];
[[NSFileManager defaultManager] removeItemAtPath: zipfilePath error: &error];
ZipFile *zipFile = [[ZipFile alloc]initWithFileName:zipfilePath mode:ZipFileModeCreate];
NSDictionary *attributes = [[NSFileManager defaultManager]attributesOfItemAtPath:zipfilePath error:&error];
NSDate *Date = [attributes objectForKey:NSFileCreationDate];
ZipWriteStream *stream = [zipFile writeFileInZipWithName:filePath fileDate:Date compressionLevel:ZipCompressionLevelBest];
NSData *data = [NSData dataWithContentsOfFile:filePath];
[stream writeData:data];
[stream finishedWriting];
[zipFile close];