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.
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;
}
Acctually i couldn't find function in zipArchive that detects if the file is encrypted so i checked the file header to check if it's password protected or not as stated in the following link:
http://secureartisan.wordpress.com/2008/11/04/analysis-of-encrypted-zip-files/
-(BOOL) IsEncrypted:(NSString*)path
{
NSData* fileData = [NSData dataWithContentsOfFile:path];
NSData* generalBitFlag = [fileData subdataWithRange:NSMakeRange(6, 2)];
NSString* genralBitFlgStr = [generalBitFlag description];
if ([genralBitFlgStr characterAtIndex:2]!='0')
{
return true;
}
else
{
return false;
}
}
Thanks for all
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With