+bool ThemeLoader::extractZip( const string &zipFile, const string &rootDir )
+{
+ // Try to open the ZIP file
+ unzFile file = unzOpen( zipFile.c_str() );
+ unz_global_info info;
+
+ if( unzGetGlobalInfo( file, &info ) != UNZ_OK )
+ {
+ return false;
+ }
+ // Extract all the files in the archive
+ for( unsigned long i = 0; i < info.number_entry; i++ )
+ {
+ if( !extractFileInZip( file, rootDir ) )
+ {
+ msg_Warn( getIntf(), "Error while unzipping %s",
+ zipFile.c_str() );
+ return false;
+ }
+
+ if( i < info.number_entry - 1 )
+ {
+ // Go the next file in the archive
+ if( unzGoToNextFile( file ) !=UNZ_OK )
+ {
+ msg_Warn( getIntf(), "Error while unzipping %s",
+ zipFile.c_str() );
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+
+bool ThemeLoader::extractFileInZip( unzFile file, const string &rootDir )
+{
+ // Read info for the current file
+ char filenameInZip[256];
+ unz_file_info fileInfo;
+ if( unzGetCurrentFileInfo( file, &fileInfo, filenameInZip,
+ sizeof( filenameInZip), NULL, 0, NULL, 0 )
+ != UNZ_OK )
+ {
+ return false;
+ }
+
+ // Allocate the buffer
+ void *pBuffer = malloc( ZIP_BUFFER_SIZE );
+ if( !pBuffer )
+ {
+ msg_Err( getIntf(), "Failed to allocate memory" );
+ return false;
+ }
+
+ // Get the path of the file
+ string fullPath = rootDir + "/" + filenameInZip;
+ string basePath = getFilePath( fullPath );
+
+ // Extract the file if is not a directory
+ if( basePath != fullPath )
+ {
+ if( unzOpenCurrentFile( file ) )
+ {
+ free( pBuffer );
+ return false;
+ }
+ makedir( basePath.c_str() );
+ FILE *fout = fopen( fullPath.c_str(), "wb" );
+ if( fout == NULL )
+ {
+ msg_Err( getIntf(), "Error opening %s", fullPath.c_str() );
+ free( pBuffer );
+ return false;
+ }
+
+ // Extract the current file
+ int n;
+ do
+ {
+ n = unzReadCurrentFile( file, pBuffer, ZIP_BUFFER_SIZE );
+ if( n < 0 )
+ {
+ msg_Err( getIntf(), "Error while reading zip file" );
+ free( pBuffer );
+ return false;
+ }
+ else if( n > 0 )
+ {
+ if( fwrite( pBuffer, n , 1, fout) != 1 )
+ {
+ msg_Err( getIntf(), "Error while writing %s",
+ fullPath.c_str() );
+ free( pBuffer );
+ return false;
+ }
+ }
+ } while( n > 0 );
+
+ fclose(fout);
+
+ if( unzCloseCurrentFile( file ) != UNZ_OK )
+ {
+ free( pBuffer );
+ return false;
+ }
+ }
+
+ free( pBuffer );
+ return true;
+}
+
+