If use Windows - fastest metod:
qint16 nRet = CopyFileEx(source.utf16(), destination.utf16(), CopyProgressRoutine, (LPVOID)this, false, COPY_FILE_RESTARTABLE);
qint16 nRet = CopyFileEx(source.utf16(), destination.utf16(), CopyProgressRoutine, (LPVOID)this, false, COPY_FILE_RESTARTABLE);
To copy to clipboard, switch view to plain text mode
CallBack :
static DWORD WINAPI CopyProgressRoutine(LARGE_INTEGER TotalFileSize,
LARGE_INTEGER TotalBytesTransferred,
LARGE_INTEGER StreamSize,
LARGE_INTEGER StreamBytesTransferred,
DWORD dwStreamNumber,
DWORD dwCallbackReason,
HANDLE hSourceFile,
HANDLE hDestinationFile,
LPVOID lpData )
{
CWorkFiles &progressInfo = *(CWorkFiles *)lpData;
qint16 nPrc = (int)((TotalBytesTransferred.QuadPart * 100) / TotalFileSize.QuadPart);
if ((nPrc - progressInfo.nPrc_old) >= 5)
{
//For Example: write copy progress in database
//db_work db;
//db.SetPercent(nPrc, progressInfo.nId);
//db.CloseDB();
//progressInfo.nPrc_old = nPrc;
}
return PROGRESS_CONTINUE;
}
static DWORD WINAPI CopyProgressRoutine(LARGE_INTEGER TotalFileSize,
LARGE_INTEGER TotalBytesTransferred,
LARGE_INTEGER StreamSize,
LARGE_INTEGER StreamBytesTransferred,
DWORD dwStreamNumber,
DWORD dwCallbackReason,
HANDLE hSourceFile,
HANDLE hDestinationFile,
LPVOID lpData )
{
CWorkFiles &progressInfo = *(CWorkFiles *)lpData;
qint16 nPrc = (int)((TotalBytesTransferred.QuadPart * 100) / TotalFileSize.QuadPart);
if ((nPrc - progressInfo.nPrc_old) >= 5)
{
//For Example: write copy progress in database
//db_work db;
//db.SetPercent(nPrc, progressInfo.nId);
//db.CloseDB();
//progressInfo.nPrc_old = nPrc;
}
return PROGRESS_CONTINUE;
}
To copy to clipboard, switch view to plain text mode
if you can use only QT:
bool CWorkFiles
::copyFile (QString source,
QString &destination,
bool overwrite,
bool move,qint16 nSysID
) {
db_work db;
if(!sourceInfo.exists())
{
qDebug("File Copy Failed - Source File does not exists.");
return false;
}
else if(!sourceInfo.isFile())
{
qDebug("File Copy Failed - Source is not a file.");
return false;
}
else if(!destinationInfo.exists())
{
qDebug("File Copy Failed - Destination cannot be found.");
return false;
}
if(destinationInfo.isDir())
destination
= (QFileInfo(QDir(destinationInfo.
absoluteFilePath()), sourceInfo.
fileName())).
filePath();
else if(!destinationInfo.isFile())
{
qDebug("File Copy failed - Destination is neither a file or directory.");
return false;
}
if(!overwrite
&& QFile::exists(destination
)) {
qDebug("File Copy failed - Destination file exists, overwrite disabled.");
return false;
}
QFile sourceFile
(source
);
QFile destinationFile
(destination
);
{
// show progress
double nPercent = 0.00;
double nNewPercent = 0.00;
uint dataLength = 32 * 1024;
char *data = new char[dataLength];
double completed = 0;
double nFsize;
nFsize = sourceFile.size();
double dMn;
if (nFsize > 0)
{
db.SetPercent(0, nSysID);
db.CloseDB();
dMn = (100 / nFsize);
qint16 nCount = 0;
while(!sourceFile.atEnd())
{
qint64 nReaded = sourceFile.read(data, dataLength);
if (nReaded <= 0)
continue;
completed += nReaded;
destinationFile.write(data, nReaded);
destinationFile.flush();
nNewPercent = nPercent;
nCount++;
if (nCount > 100 || dataLength !=nReaded)
{
nPercent = completed * dMn;
db.SetPercent((qint64)nPercent, nSysID);
db.CloseDB();
nCount = 0;
}
}
}
else
{
delete[] data;
return false;
}
delete[] data;
db.SetPercent(100, nSysID);
db.CloseDB();
if(move)
{
if(!sourceFile.remove())
{
destinationFile.remove();
sourceFile.close();
destinationFile.close();
qDebug("File Copy failed - Source file could not be removed.");
return false;
}
}
sourceFile.close();
destinationFile.close();
}
else
{
sourceFile.close();
destinationFile.close();
qDebug("DBG: Error - Source or destination file could not be opened.");
qDebug("File Copy failed - Source or destination file could not be opened.");
return false;
}
return true;
}
bool CWorkFiles::copyFile (QString source, QString &destination, bool overwrite, bool move,qint16 nSysID)
{
db_work db;
QFileInfo sourceInfo = QFileInfo(source);
QFileInfo destinationInfo = QFileInfo(destination);
if(!sourceInfo.exists())
{
qDebug("File Copy Failed - Source File does not exists.");
return false;
}
else if(!sourceInfo.isFile())
{
qDebug("File Copy Failed - Source is not a file.");
return false;
}
else if(!destinationInfo.exists())
{
qDebug("File Copy Failed - Destination cannot be found.");
return false;
}
if(destinationInfo.isDir())
destination = (QFileInfo(QDir(destinationInfo.absoluteFilePath()), sourceInfo.fileName())).filePath();
else if(!destinationInfo.isFile())
{
qDebug("File Copy failed - Destination is neither a file or directory.");
return false;
}
if(!overwrite && QFile::exists(destination))
{
qDebug("File Copy failed - Destination file exists, overwrite disabled.");
return false;
}
QFile sourceFile(source);
QFile destinationFile(destination);
if(sourceFile.open(QIODevice::ReadOnly) && destinationFile.open(QIODevice::WriteOnly))
{
// show progress
double nPercent = 0.00;
double nNewPercent = 0.00;
uint dataLength = 32 * 1024;
char *data = new char[dataLength];
double completed = 0;
double nFsize;
nFsize = sourceFile.size();
double dMn;
if (nFsize > 0)
{
db.SetPercent(0, nSysID);
db.CloseDB();
dMn = (100 / nFsize);
qint16 nCount = 0;
while(!sourceFile.atEnd())
{
qint64 nReaded = sourceFile.read(data, dataLength);
if (nReaded <= 0)
continue;
completed += nReaded;
destinationFile.write(data, nReaded);
destinationFile.flush();
nNewPercent = nPercent;
nCount++;
if (nCount > 100 || dataLength !=nReaded)
{
nPercent = completed * dMn;
db.SetPercent((qint64)nPercent, nSysID);
db.CloseDB();
nCount = 0;
}
}
}
else
{
delete[] data;
return false;
}
delete[] data;
db.SetPercent(100, nSysID);
db.CloseDB();
if(move)
{
if(!sourceFile.remove())
{
destinationFile.remove();
sourceFile.close();
destinationFile.close();
qDebug("File Copy failed - Source file could not be removed.");
return false;
}
}
sourceFile.close();
destinationFile.close();
}
else
{
sourceFile.close();
destinationFile.close();
qDebug("DBG: Error - Source or destination file could not be opened.");
qDebug("File Copy failed - Source or destination file could not be opened.");
return false;
}
return true;
}
To copy to clipboard, switch view to plain text mode
For Windiws i'am use first example, because the sizes of files with which I work 1-15Gb the second example too works not bad - but more slowly.
Bookmarks