Copying QFile contents to another QFile, what's the fastest way?
-
I need to copy a QFile to another QFile in chunks, so I can't use QFile::copy. Here's the most primitive implementation:
@bool CFile::copyChunk(int64_t chunkSize, const QString &destFolder)
{
if (!_thisFile.isOpen())
{
// Initializing - opening files
_thisFile.setFileName(_absoluteFilePath);
if (!_thisFile.open(QFile::ReadOnly))
return false;_destFile.setFileName(destFolder + _thisFileName); if (!_destFile.open(QFile::WriteOnly)) return false; } if (chunkSize < (_thisFile.size() - _thisFile.pos())) { QByteArray data (chunkSize, 0); _thisFile.read(data.data(), chunkSize); return _destFile.write(data) == chunkSize; }
}
@
It's not clear from this fragment, but I only intend to copy a binary file as a whole into another location, just in chunks so I can provide progress callbacks and cancellation facility for large files.Another idea is to use memory mapping. Should I? If so, then should I only map source file and still use _destFile.write, or should I map both and use memcpy?
-
You know about the "static copy method?":http://qt-project.org/doc/qt-5.0/qtcore/qfile.html#copy-2
Probably it will be hard to get faster, would be my assumption. -
Like I've meantioned, I can't use that method. No doubt it's the fastest but it's blocking so I can't cancel a long copy if I use it.