Search code examples
iphoneiosipadzipphp-ziparchive

How to check if a zip file is password protected in IOS?


I am using ZipArchive to extract zip files in an iOS application, but I want to know before openning the file if it's password protected or not so that I can pass the password to the UnZipOpenFile function.


Solution

  • password of a zip file is not record in header it is recorded in individual file entries in zip

    so you need to check all files in zip

    add this function to ZipArchive

    -(BOOL) UnzipIsEncrypted {
    
        int ret = unzGoToFirstFile( _unzFile );
        if (ret == UNZ_OK) {
            do {
                ret = unzOpenCurrentFile( _unzFile );
                if( ret!=UNZ_OK ) {
                    return NO;
                }
                unz_file_info   fileInfo ={0};
                ret = unzGetCurrentFileInfo(_unzFile, &fileInfo, NULL, 0, NULL, 0, NULL, 0);
                if (ret!= UNZ_OK) {
                    return NO;
                }
                else if((fileInfo.flag & 1) == 1) {
                    return YES;
                }
    
                unzCloseCurrentFile( _unzFile );
                ret = unzGoToNextFile( _unzFile );
            } while( ret==UNZ_OK && UNZ_OK!=UNZ_END_OF_LIST_OF_FILE );
    
        }
    
        return NO;
    }