我让我的应用创建一个文件,将其压缩成.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];
答案 0 :(得分:0)
问题是你在说:
writeFileInZipWithName:filePath
变量filePath
包含您抱怨的巨大长名称。它是从顶部一直到实际文件的整个路径。
您的文件名是@"data.txt"
,所以您要说的是:
writeFileInZipWithName:@"data.txt"