Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newDataWithError is nil #169

Open
passingwind opened this issue Apr 27, 2018 · 0 comments
Open

newDataWithError is nil #169

passingwind opened this issue Apr 27, 2018 · 0 comments

Comments

@passingwind
Copy link

Open ZipArchive is OK, but when I create data from ZZEntry, I just get nil, Please help.
PS: The zip file created by ZipZap is OK, however, the other zip files created by other software will be error.
Code like this:

    bool UnzipFileAtPath(char* srcFile, char* destPath)
    {
        NSFileManager* fileManager = [NSFileManager defaultManager];
        NSString* zipFilePath = [NSString stringWithUTF8String:srcFile];
        NSString* destDirPath = [NSString stringWithUTF8String:destPath];
        if(![destDirPath hasSuffix:@"/"])
        {
            destDirPath = [NSString stringWithFormat:@"%@/", destDirPath];
        }
        NSLog(@"[ZipZap] Begin Unzip File:%@", zipFilePath);
        
        NSError * error;
        NSData* rawZipData = [fileManager contentsAtPath:zipFilePath];
        NSLog(@"[ZipZap] Raw Zip Data Length: %li", rawZipData.length);
        ZZArchive* zipArchive = [ZZArchive archiveWithData:rawZipData error:&error];
        if (error)
        {
            NSLog(@"[ZipZap] Error happened while open zip file: %@ Error: %@", zipFilePath, [error localizedDescription]);
            return NO;
        }
        else
        {
            NSLog(@"[ZipZap] Zip Entries: %lui", zipArchive.entries.count);
        }
        
        for (ZZArchiveEntry* entry in zipArchive.entries)
        {
            NSString* entryFilePath = [destDirPath stringByAppendingString:entry.fileName];
            if(entry.fileMode & S_IFDIR)
            {
                [fileManager createDirectoryAtPath:entryFilePath withIntermediateDirectories:YES attributes:nil error:nil];
            }
            else
            {
                NSError* createDataError;
                NSData* newData = [entry newDataWithError:&createDataError];
                if(createDataError || newData == nil || newData.length == 0)
                {
                    NSLog(@"[ZipZap] Create Data from ZZEntry Error: %lui", createDataError.code);
                    return NO;
                }
                else
                {
                    NSUInteger length = [newData length];
                    NSLog(@"[ZipZap] File: %@, Length: %lu", entry.fileName, length);
                }
                NSError* writeDataError;
                BOOL ret = [fileManager createFileAtPath:entryFilePath contents:newData attributes:nil];
                if(!ret)
                {
                    NSLog(@"[ZipZap] Write Data Error: %@, Error: %li", entry.fileName, [writeDataError code]);
                }
            }
        }
        NSLog(@"[ZipZap] Unzip File OK!");
        return YES;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant