Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 488
Eman.H Avatar asked Dec 27 '22 23:12

Eman.H


2 Answers

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;
}
like image 137
IPaPa Avatar answered Jan 08 '23 13:01

IPaPa


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

like image 44
Eman.H Avatar answered Jan 08 '23 13:01

Eman.H