pixelglow / ZipZap

zip file I/O library for iOS, macOS and tvOS
BSD 2-Clause "Simplified" License
1.22k stars 199 forks source link

newDataWithError is nil #169

Open passingwind opened 6 years ago

passingwind commented 6 years ago

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