创建zip文件时获取异常

时间:2018-08-02 06:31:10

标签: objective-c xcode file logfile objectivezip

我想将我的日志文件转换为zip文件。因此,我正在使用Objective-Zip。但是我遇到了异常

2018-08-02 11:53:57.901192+0530 Aglive[1076:511096] *** Terminating app due to uncaught exception 'OZZipException', reason: 'Can't open 'test.zip''
*** First throw call stack:
(0x18204b164 0x181294528 0x101371724 0x101371414 0x101109318 0x101108234 0x18b71cb20 0x18b7c5760 0x18b873aa8 0x18b866e5c 0x18b5f8464 0x181ff2cdc 0x181ff0694 0x181ff0c50 0x181f10c58 0x183dbcf84 0x18b6695c4 0x100f93f80 0x181a3056c)
libc++abi.dylib: terminating with uncaught exception of type OZZipException

这是我的代码

 NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSString *filePath = [docDir stringByAppendingPathComponent:@"Logfile.txt"];
        OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:@"test.zip"
                                                                      mode:OZZipFileModeCreate];
        OZZipWriteStream *stream= [zipFile writeFileInZipWithName:@"Logfile.txt"
                                                 compressionLevel:OZZipCompressionLevelBest];

        [stream writeData:filePath];
        [stream finishedWriting];

此异常仅在代码之后

发生
OZZipFile *zipFile= [[OZZipFile alloc] initWithFileName:@"test.zip"
                                                                          mode:OZZipFileModeCreate];

我不知道为什么会发生这种异常。为什么它试图打开该文件而不是创建它?

1 个答案:

答案 0 :(得分:1)

我认为它找不到创建zip文件的路径。

在示例文件中,它使用以下内容创建档案:

NSString *documentsDir= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath= [documentsDir stringByAppendingPathComponent:@"test32_64.zip"];

OZZipFile *zipFile32= [[OZZipFile alloc] initWithFileName:filePath mode:OZZipFileModeCreate legacy32BitMode:YES];

您可以看到filePath是zip路径,并以此命名为alloc初始化。

然后添加文件:

OZZipWriteStream *stream= [zipFile32 writeFileInZipWithName:@"abc.txt" fileDate:[NSDate dateWithTimeIntervalSinceNow:-86400.0] compressionLevel:OZZipCompressionLevelDefault];

最后,它写入并关闭流和文件:

NSMutableData *writeData= [NSMutableData dataWithLength:4096];
int result= SecRandomCopyBytes(kSecRandomDefault, [writeData length], [writeData mutableBytes]);

XCTAssertEqual(0, result);

[stream writeData:writeData];

[stream finishedWriting];

[zipFile32 close];

最后一个问题的答案是,在所有OZZipFileMode情况下(解压缩,创建和附加),在例外情况下,它都将打印相同的日志:

[OZZipException zipExceptionWithError:OZ_ERROR_NO_SUCH_FILE reason:@"Can't open '%@'", _fileName];