Search code examples
objective-cxcodefilelogfileobjectivezip

Getting exception while creating zip file


I want to convert my log file into zip file. So for that I'm using Objective-Zip. But I'm getting exception

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

This is my code

 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];

This exception occurs just after code

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

I don't know why this exception occurs.Why it is trying to open that file instead of creating it?


Solution

  • I think that it can't find the path where create the zip file.

    In the example files, it creates the archive with:

    NSString *documentsDir= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath= [documentsDir stringByAppendingPathComponent:@"test32_64.zip"];
    
    OZZipFile *zipFile32= [[OZZipFile alloc] initWithFileName:filePath mode:OZZipFileModeCreate legacy32BitMode:YES];
    

    You can see that the filePath is the zip Path and with this it calls the alloc init.

    Then it adds the file:

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

    And finally it writes and closes the stream and the file:

    NSMutableData *writeData= [NSMutableData dataWithLength:4096];
    int result= SecRandomCopyBytes(kSecRandomDefault, [writeData length], [writeData mutableBytes]);
    
    XCTAssertEqual(0, result);
    
    [stream writeData:writeData];
    
    [stream finishedWriting];
    
    [zipFile32 close];
    

    The answer to your last question is that in all the OZZipFileMode case (unzip, create and append) it prints the same log in the exceptions:

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