⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 krarchandler.cpp

📁 LINUX 下, 以 QT/KDE 写的档案管理员
💻 CPP
📖 第 1 页 / 共 2 页
字号:
  if( type == "-ace" && QFile( "/dev/ptmx" ).exists() ) // Don't remove, unace crashes if missing!!!    proc << "<" << "/dev/ptmx";    // tell the user to wait  krApp->startWaiting( i18n( "Testing Archive" ), count, true );  if ( count != 0 ) connect( &proc, SIGNAL( receivedStdout( KProcess*, char*, int ) ),                               krApp, SLOT( incProgress( KProcess*, char*, int ) ) );  // start the unpacking process  proc.start( KProcess::NotifyOnExit, KProcess::AllOutput );  while ( proc.isRunning() ) {    usleep( 1000 );    qApp->processEvents();    if( krApp->wasWaitingCancelled() )      proc.kill();    }  ; // busy wait - need to find something better...  krApp->stopWait();  // check the return value  if ( !proc.normalExit() || !checkStatus( type, proc.exitStatus() ) )    return false;  return true; // SUCCESS  }bool KRarcHandler::pack( QStringList fileNames, QString type, QString dest, long count, QMap<QString,QString> extraProps ) {  // set the right packer to do the job  QString packer;  if      ( type == "zip" ) { packer = KrServices::fullPathName( "zip" ) + " -ry"; type = "-zip"; }   else if ( type == "tar" ) { packer = KrServices::fullPathName( "tar" ) + " -cvf"; type = "-tar"; }   else if ( type == "tar.gz" ) { packer = KrServices::fullPathName( "tar" ) + " -cvzf"; type = "-tgz"; }   else if ( type == "tar.bz2" ) { packer = KrServices::fullPathName( "tar" ) + " -cvjf"; type = "-tbz"; }   else if ( type == "rar" ) { packer = KrServices::fullPathName( "rar" ) + " -r a"; type = "-rar"; }   else if ( type == "lha" ) { packer = KrServices::fullPathName( "lha" ) + " a"; type = "-lha"; }   else if ( type == "arj" ) { packer = KrServices::fullPathName( "arj" ) + " -r -y a"; type = "-arj"; }   else if ( type == "7z" ) {  packer = KrServices::fullPathName( "7z" ) + " -y a"; type = "-7z"; }   else return false;  QString password = QString::null;    if( extraProps.count( "Password" ) > 0 ) {    password = extraProps[ "Password" ];    if ( !password.isNull() ) {      if ( type == "-zip" )         packer += " -P '" + password + "'";      else if ( type == "-arj" )        packer += " -g'" + password + "'";      else if ( type == "-ace" || type == "-7z" )        packer += " -p'" + password + "'";      else if ( type == "-rar" ) {        if( extraProps.count( "EncryptHeaders" ) > 0 )          packer += " -hp'" + password + "'";        else          packer += " -p'" + password + "'";      }      else        password = QString::null;    }  }  if( extraProps.count( "VolumeSize" ) > 0 ) {     QString sizeStr = extraProps[ "VolumeSize" ];     KIO::filesize_t size = sizeStr.toLongLong();     if( size >= 10000 ) {       if( type == "-arj" || type == "-rar" )           packer += QString( " -v%1b" ).arg( sizeStr );     }  }  if( extraProps.count( "CompressionLevel" ) > 0 ) {     int level = extraProps[ "CompressionLevel" ].toInt() - 1;     if ( level < 0 )       level = 0;     if ( level > 8 )       level = 8;     if( type == "-rar" ) {       static const int rarLevels[] = { 0, 1, 2, 2, 3, 3, 4, 4, 5 };       packer += QString( " -m%1" ).arg( rarLevels[ level ] );     }     else if( type == "-arj" ) {       static const int arjLevels[] = { 0, 4, 4, 3, 3, 2, 2, 1, 1 };       packer += QString( " -m%1" ).arg( arjLevels[ level ] );     }     else if( type == "-zip" ) {       static const int zipLevels[] = { 0, 1, 2, 4, 5, 6, 7, 8, 9 };       packer += QString( " -%1" ).arg( zipLevels[ level ] );     }     else if( type == "-7z" ) {       static const int sevenZipLevels[] = { 0, 1, 2, 4, 5, 6, 7, 8, 9 };       packer += QString( " -mx%1" ).arg( sevenZipLevels[ level ] );     }  }  if( extraProps.count( "CommandLineSwitches" ) > 0 )     packer += QString( " %1" ).arg( extraProps[ "CommandLineSwitches" ] );    // prepare to pack  KrShellProcess proc;  proc << packer << KrServices::quote( dest );  for ( QStringList::Iterator file = fileNames.begin(); file != fileNames.end(); ++file ) {    proc << KrServices::quote( *file );    }  // tell the user to wait  krApp->startWaiting( i18n( "Packing File(s)" ), count, true );  if ( count != 0 )    connect( &proc, SIGNAL( receivedStdout( KProcess*, char*, int ) ),             krApp, SLOT( incProgress( KProcess*, char*, int ) ) );  // start the packing process  proc.start( KProcess::NotifyOnExit, KProcess::AllOutput );  while ( proc.isRunning() ) {    usleep( 1000 );    qApp->processEvents();    if( krApp->wasWaitingCancelled() )      proc.kill();    }  ; // busy wait - need to find something better...  krApp->stopWait();  // check the return value  if ( !proc.normalExit() || !checkStatus( type, proc.exitStatus() ) ) {    KMessageBox::detailedError (krApp, i18n( "Failed to pack %1!" ).arg( dest ),                                 krApp->wasWaitingCancelled() ? i18n( "User cancelled." ) : proc.getErrorMsg(),                                 i18n("Error" ) );    return false;    }  krConfig->setGroup( "Archives" );  if ( krConfig->readBoolEntry( "Test Archives", _TestArchives ) &&       !test( dest, type, password, count ) ) {    KMessageBox::error( krApp, i18n( "Failed to pack: " ) + dest, i18n( "Error" ) );    return false;    }  return true; // SUCCESS  }QString KRarcHandler::getPassword( QString path ) {	QString password;		QString key = "krarc-" + path;		if( !KWallet::Wallet::keyDoesNotExist(KWallet::Wallet::NetworkWallet(), KWallet::Wallet::PasswordFolder(), key ) ) {		if( !KWallet::Wallet::isOpen( KWallet::Wallet::NetworkWallet() )  && wallet != 0 ) {			delete wallet;			wallet = 0;		}		if( wallet == 0 )			wallet = KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet() );		if ( wallet && wallet->hasFolder( KWallet::Wallet::PasswordFolder() ) ) {			wallet->setFolder( KWallet::Wallet::PasswordFolder() );			QMap<QString,QString> map;			if ( wallet->readMap( key, map ) == 0 ) {				QMap<QString, QString>::ConstIterator it = map.find( "password" );				if ( it != map.end() )					password = it.data();			}		}	}		bool keep = true;	QString user = "archive";	KIO::PasswordDialog passDlg( i18n("This archive is encrypted, please supply the password:"),	                             user, true );	passDlg.setPassword( password );	if (passDlg.exec() == KIO::PasswordDialog::Accepted) {		password = passDlg.password();		if ( keep ) {			if( !KWallet::Wallet::isOpen( KWallet::Wallet::NetworkWallet() ) && wallet != 0 ) {				delete wallet;				wallet = 0;			}			if ( !wallet )				wallet = KWallet::Wallet::openWallet( KWallet::Wallet::NetworkWallet() );			if ( wallet ) {				bool ok = true;				if ( !wallet->hasFolder( KWallet::Wallet::PasswordFolder() ) )					ok = wallet->createFolder( KWallet::Wallet::PasswordFolder() );				if ( ok ) {					wallet->setFolder( KWallet::Wallet::PasswordFolder() );					QMap<QString,QString> map;					map.insert( "login", "archive" );					map.insert( "password", password );					wallet->writeMap( key, map );				}			}		}		return password;	}		return "";}bool KRarcHandler::isArchive(const KURL& url) {	QString protocol = url.protocol();	if (arcProtocols.find(protocol) != arcProtocols.end())		return true;	else return false;	}QString KRarcHandler::getType( bool &encrypted, QString fileName, QString mime, bool checkEncrypted ) {	QString result = detectArchive( encrypted, fileName, checkEncrypted );	if( result.isNull() )		result = mime;	else		result = "-" + result;		if( result.endsWith( "-7z" ) )		result = "-7z";		return result.right( 4 );}bool KRarcHandler::checkStatus( QString type, int exitCode ) {	if( type == "-zip" || type == "-rar" || type == "-7z" )		return exitCode == 0 || exitCode == 1;	else if( type == "-ace" || type == "zip2" || type == "-lha" || type == "-rpm" || type == "cpio" ||	         type == "-tar" || type == "tarz" || type == "-tbz" || type == "-tgz" || type == "-arj" ||	         type == "-deb" )		return exitCode == 0;	else if( type == "gzip" )		return exitCode == 0 || exitCode == 2;	else		return exitCode == 0;}struct AutoDetectParams {	QString type;	int location;	QString detectionString;};QString KRarcHandler::detectArchive( bool &encrypted, QString fileName, bool checkEncrypted ) {	static AutoDetectParams autoDetectParams[] = {{"zip",  0, "PK\x03\x04"},	                                              {"rar",  0, "Rar!\x1a" },	                                              {"arj",  0, "\x60\xea" },	                                              {"rpm",  0, "\xed\xab\xee\xdb"},	                                              {"ace",  7, "**ACE**" },	                                              {"bzip2",0, "\x42\x5a\x68\x39\x31" },	                                              {"gzip", 0, "\x1f\x8b"}, 	                                              {"deb",  0, "!<arch>\ndebian-binary   " },	                                              {"7z",   0, "7z\xbc\xaf\x27\x1c" } };	static int autoDetectElems = sizeof( autoDetectParams ) / sizeof( AutoDetectParams );		encrypted = false;		QFile arcFile( fileName );	if ( arcFile.open( IO_ReadOnly ) ) {		char buffer[ 1024 ];		long sizeMax = arcFile.readBlock( buffer, sizeof( buffer ) );		arcFile.close();				for( int i=0; i < autoDetectElems; i++ ) {			QString detectionString = autoDetectParams[ i ].detectionString;			int location = autoDetectParams[ i ].location;						int endPtr = detectionString.length() + location;			if( endPtr > sizeMax )				continue;						unsigned int j=0;			for(; j != detectionString.length(); j++ ) {				if( detectionString[ j ] == '?' )					continue;				if( buffer[ location + j ] != detectionString[ j ] )					break;			}						if( j == detectionString.length() ) {				QString type = autoDetectParams[ i ].type;				if( type == "bzip2" || type == "gzip" ) {					KTar tapeArchive( fileName );					if( tapeArchive.open( IO_ReadOnly ) ) {						tapeArchive.close();						if( type == "bzip2" )							type = "tbz";						else							type = "tgz";					}				}				else if( type == "zip" )					encrypted = (buffer[6] & 1);				else if( type == "arj" ) {					if( sizeMax > 4 ) {						long headerSize = ((unsigned char *)buffer)[ 2 ] + 256*((unsigned char *)buffer)[ 3 ];						long fileHeader = headerSize + 10;						if( fileHeader + 9 < sizeMax && buffer[ fileHeader ] == (char)0x60 && buffer[ fileHeader + 1 ] == (char)0xea )							encrypted = (buffer[ fileHeader + 8 ] & 1 );					}				}				else if( type == "rar" ) {					if( sizeMax > 13 && buffer[ 9 ] == (char)0x73 ) {						if( buffer[ 10 ] & 0x80 ) { // the header is encrypted?							encrypted = true;						} else {							long offset = 7;							long mainHeaderSize = ((unsigned char *)buffer)[ offset+5 ] + 256*((unsigned char *)buffer)[ offset+6 ];							offset += mainHeaderSize;							while( offset + 10 < sizeMax ) {								long headerSize = ((unsigned char *)buffer)[ offset+5 ] + 256*((unsigned char *)buffer)[ offset+6 ];								bool isDir = (buffer[ offset+7 ] == '\0' ) && (buffer[ offset+8 ] == '\0' ) &&								             (buffer[ offset+9 ] == '\0' ) && (buffer[ offset+10 ] == '\0' );							             								if( buffer[ offset + 2 ] != (char)0x74 )									break;								if( !isDir ) {									encrypted = ( buffer[ offset + 3 ] & 4 ) != 0;									break;								}								offset += headerSize;							}						}					}				}				else if( type == "ace" ) {						long offset = 0;						long mainHeaderSize = ((unsigned char *)buffer)[ offset+2 ] + 256*((unsigned char *)buffer)[ offset+3 ] + 4;						offset += mainHeaderSize;						while( offset + 10 < sizeMax ) {							long headerSize = ((unsigned char *)buffer)[ offset+2 ] + 256*((unsigned char *)buffer)[ offset+3 ] + 4;							bool isDir = (buffer[ offset+11 ] == '\0' ) && (buffer[ offset+12 ] == '\0' ) &&							             (buffer[ offset+13 ] == '\0' ) && (buffer[ offset+14 ] == '\0' );							             							if( buffer[ offset + 4 ] != (char)0x01 )								break;							if( !isDir ) {								encrypted = ( buffer[ offset + 6 ] & 64 ) != 0;								break;							}							offset += headerSize;						}				}				else if( type == "7z" ) {					if( checkEncrypted ) { // encryption check is expensive					                       // check only if it's necessary						Kr7zEncryptionChecker proc;						proc << KrServices::fullPathName( "7z" ) << " -y t";						proc << KrServices::quote( fileName );						proc.start(KProcess::Block,KProcess::AllOutput);						encrypted = proc.isEncrypted();					}				}				return type;			}		}				if( sizeMax >= 512 ) {			/* checking if it's a tar file */			unsigned checksum = 32*8;			char chksum[ 9 ];			for( int i=0; i != 512; i++ )				checksum += ((unsigned char *)buffer)[ i ];			for( int i=148; i != 156; i++ )				checksum -= ((unsigned char *)buffer)[ i ];			sprintf( chksum, "0%o", checksum );			if( !memcmp( buffer + 148, chksum, strlen( chksum ) ) ) {				int k = strlen( chksum );				for(; k < 8; k++ )					if( buffer[148+k] != 0 && buffer[148+k] != 32 )						break;				if( k==8 )					return "tar";			}		}	}	return QString::null;}#include "krarchandler.moc"

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -