Results 1 to 5 of 5

Thread: Copy a File over the Network

  1. #1
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Copy a File over the Network

    Hi everybody,

    i am trying to copy a File over the Network and i would like to show in a progressbar the status of copy..

    I have tried this example but it takes to long.

    i heard about QUrlOperator, but under QT 4 its not working..

    Could somebody show me how to copy a big file over the network and show the status in a progressbar?

    Qt Code:
    1. QFile src("W:\\1Data\\Public\\Transfer\\Notes.exe");
    2. QFile dst("notes.exe");
    3. if(!src.open(QFile::ReadOnly) || !dst.open(QFile::WriteOnly)) return;
    4.  
    5. qint64 len = src.bytesAvailable();
    6. ui.dlg->setRange(0,len);
    7. ui.dlg->show();
    8. char ch;
    9. while(!src.atEnd()){
    10. src.getChar(&ch);
    11. dst.putChar(ch);
    12. ui.dlg->setValue(ui.dlg->value()+1);
    13. qApp->processEvents();}
    To copy to clipboard, switch view to plain text mode 
    Thanks
    Think DigitalGasoline

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Copy a File over the Network

    It is slow because you're copying byte by byte... What if the file is 1Mb in size?
    Try using QDataStream::readRawData and QDataSteam::writeBytes to read and write bigger chunks.

  3. #3
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Unhappy Re: Copy a File over the Network

    Hi,

    Data between 50-300 MB

    I search here and in the docs a example but i didint find it

    Could somebody help me to use my code, but for big files?
    Think DigitalGasoline

  4. #4
    Join Date
    Jul 2007
    Location
    BY.Minsk
    Posts
    90
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Copy a File over the Network

    If use Windows - fastest metod:

    Qt Code:
    1. 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 :

    Qt Code:
    1. static DWORD WINAPI CopyProgressRoutine(LARGE_INTEGER TotalFileSize,
    2. LARGE_INTEGER TotalBytesTransferred,
    3. LARGE_INTEGER StreamSize,
    4. LARGE_INTEGER StreamBytesTransferred,
    5. DWORD dwStreamNumber,
    6. DWORD dwCallbackReason,
    7. HANDLE hSourceFile,
    8. HANDLE hDestinationFile,
    9. LPVOID lpData )
    10. {
    11. CWorkFiles &progressInfo = *(CWorkFiles *)lpData;
    12. qint16 nPrc = (int)((TotalBytesTransferred.QuadPart * 100) / TotalFileSize.QuadPart);
    13.  
    14. if ((nPrc - progressInfo.nPrc_old) >= 5)
    15. {
    16. //For Example: write copy progress in database
    17. //db_work db;
    18. //db.SetPercent(nPrc, progressInfo.nId);
    19. //db.CloseDB();
    20. //progressInfo.nPrc_old = nPrc;
    21. }
    22.  
    23. return PROGRESS_CONTINUE;
    24. }
    To copy to clipboard, switch view to plain text mode 

    if you can use only QT:

    Qt Code:
    1. bool CWorkFiles::copyFile (QString source, QString &destination, bool overwrite, bool move,qint16 nSysID)
    2. {
    3. db_work db;
    4.  
    5. QFileInfo sourceInfo = QFileInfo(source);
    6. QFileInfo destinationInfo = QFileInfo(destination);
    7.  
    8. if(!sourceInfo.exists())
    9. {
    10. qDebug("File Copy Failed - Source File does not exists.");
    11. return false;
    12. }
    13. else if(!sourceInfo.isFile())
    14. {
    15. qDebug("File Copy Failed - Source is not a file.");
    16. return false;
    17. }
    18. else if(!destinationInfo.exists())
    19. {
    20. qDebug("File Copy Failed - Destination cannot be found.");
    21. return false;
    22. }
    23.  
    24. if(destinationInfo.isDir())
    25. destination = (QFileInfo(QDir(destinationInfo.absoluteFilePath()), sourceInfo.fileName())).filePath();
    26. else if(!destinationInfo.isFile())
    27. {
    28. qDebug("File Copy failed - Destination is neither a file or directory.");
    29. return false;
    30. }
    31.  
    32. if(!overwrite && QFile::exists(destination))
    33. {
    34. qDebug("File Copy failed - Destination file exists, overwrite disabled.");
    35. return false;
    36. }
    37.  
    38. QFile sourceFile(source);
    39. QFile destinationFile(destination);
    40.  
    41. if(sourceFile.open(QIODevice::ReadOnly) && destinationFile.open(QIODevice::WriteOnly))
    42. {
    43. // show progress
    44. double nPercent = 0.00;
    45. double nNewPercent = 0.00;
    46. uint dataLength = 32 * 1024;
    47. char *data = new char[dataLength];
    48. double completed = 0;
    49. double nFsize;
    50. nFsize = sourceFile.size();
    51. double dMn;
    52.  
    53. if (nFsize > 0)
    54. {
    55. db.SetPercent(0, nSysID);
    56. db.CloseDB();
    57. dMn = (100 / nFsize);
    58. qint16 nCount = 0;
    59. while(!sourceFile.atEnd())
    60. {
    61. qint64 nReaded = sourceFile.read(data, dataLength);
    62. if (nReaded <= 0)
    63. continue;
    64. completed += nReaded;
    65. destinationFile.write(data, nReaded);
    66. destinationFile.flush();
    67. nNewPercent = nPercent;
    68. nCount++;
    69.  
    70. if (nCount > 100 || dataLength !=nReaded)
    71. {
    72. nPercent = completed * dMn;
    73. db.SetPercent((qint64)nPercent, nSysID);
    74. db.CloseDB();
    75. nCount = 0;
    76. }
    77. }
    78. }
    79. else
    80. {
    81. delete[] data;
    82. return false;
    83. }
    84. delete[] data;
    85.  
    86. db.SetPercent(100, nSysID);
    87. db.CloseDB();
    88.  
    89. if(move)
    90. {
    91. if(!sourceFile.remove())
    92. {
    93. destinationFile.remove();
    94. sourceFile.close();
    95. destinationFile.close();
    96. qDebug("File Copy failed - Source file could not be removed.");
    97. return false;
    98. }
    99. }
    100. sourceFile.close();
    101. destinationFile.close();
    102. }
    103. else
    104. {
    105. sourceFile.close();
    106. destinationFile.close();
    107. qDebug("DBG: Error - Source or destination file could not be opened.");
    108. qDebug("File Copy failed - Source or destination file could not be opened.");
    109. return false;
    110. }
    111. return true;
    112. }
    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.
    Last edited by Fastman; 17th June 2008 at 10:14.

  5. #5
    Join Date
    Jan 2006
    Posts
    273
    Thanks
    42
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: Copy a File over the Network

    Hi,

    Thank you very much, it works :=)
    Think DigitalGasoline

Similar Threads

  1. File Dialog / Network Protocols in QT4
    By themolecule in forum Qt Programming
    Replies: 6
    Last Post: 10th September 2007, 07:40
  2. copy and run a .exe file in another system
    By sabeesh in forum Installation and Deployment
    Replies: 3
    Last Post: 22nd August 2007, 10:05
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. file copy in Qt 3.3.4 ?
    By npc in forum Newbie
    Replies: 6
    Last Post: 31st March 2006, 14:43
  5. Replies: 4
    Last Post: 24th February 2006, 10:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.