Results 1 to 6 of 6

Thread: QFtp: downloading a whole folder from a remote server.

  1. #1
    Join Date
    Jul 2007
    Location
    Cluj Napoca, Romania
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QFtp: downloading a whole folder from a remote server.

    So I managed to write a function to upload a whole folder to an ftp server, here is the code if anyone is searching this forum (like I did) in hope of finding a function like this:

    Qt Code:
    1. void sendFolder(const QString &path,const QString &dirPath)
    2. {
    3. QTextStream cout(stdout, QIODevice::WriteOnly);
    4. QFileInfo fileInfo(path);
    5. if (fileInfo.isDir())
    6. {
    7. QString dirName = path;
    8. dirName.remove(0,dirPath.size());
    9. ftp->mkdir(dirName);
    10. QDir dir(path);
    11. QStringList entries = dir.entryList(QDir::AllEntries|QDir::NoDotAndDotDot,QDir::DirsFirst);
    12. foreach (QString entry, entries)
    13. {
    14. sendFolder(dir.filePath(entry),dirPath);
    15. }
    16. }
    17. else
    18. {
    19. QFile *file = new QFile(path);
    20. if(file->open(QIODevice::ReadOnly))
    21. {
    22. QString filename =path;
    23. filename.remove(0,dirPath.size());
    24. ftp->put(file,filename);
    25. file->close();
    26. }
    27. else cout <<"Could not open file:"<<path<<"\n";
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    The dirPath parameter contains the path to the local directory that contains the folder you want to upload.
    If the directory you are trying to upload is: /home/user/mydir then dirPath should be /home/user/
    Because the dirPath substring will be removed to create the directory mydir on the server.

    Ok.
    So now that I've got that working, I am trying to figure out how to download a whole folder from a remote ftp server into a local folder.
    If anyone has some code that does that I would be very grateful to have it.
    I searched this forum but couldn't find what I need.
    Also the qt4 ftp example doesn't contain downloading a whole folder, only files.

    Can anyone help me ?
    Thanks.
    May the source be with you!

  2. #2
    Join Date
    May 2007
    Posts
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QFtp: downloading a whole folder from a remote server.

    I'll look around and see if I can find the code I wrote to do this, however I can tell you that my implementation used two member functions in QFtp.

    QFtp::list()
    signal QFtp::listInfo(const QUrlInfo& i)

  3. The following user says thank you to daracne for this useful post:

    balazsbela (4th August 2007)

  4. #3
    Join Date
    Jul 2007
    Location
    Cluj Napoca, Romania
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFtp: downloading a whole folder from a remote server.

    I started writing one using those two.
    It would be great if you could find it.
    It is getting late, I think I'll continue to write that tomorrow.
    Thanks for replying.
    May the source be with you!

  5. #4
    Join Date
    Jul 2007
    Location
    Cluj Napoca, Romania
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFtp: downloading a whole folder from a remote server.

    So if I'm right this should list all folders and files(even in subfolders) in the folder I want to download.

    Here is the code:

    Qt Code:
    1. void getFolder(QString path)
    2. {
    3. ftp->list(path);
    4. connect(ftp,SIGNAL(listInfo(QUrlInfo)),this,SLOT(processFolder(QUrlInfo)));
    5. }
    6.  
    7. void processFolder(const QUrlInfo &i)
    8. {
    9. QTextStream cout(stdout, QIODevice::WriteOnly);
    10. if (i.isDir())
    11. {
    12. cout <<"Directory:"<< i.name()<<"\n";
    13. ftp->list(i.name());
    14. }
    15. else
    16. {
    17. cout <<"File:"<<i.name()<<"\n";
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Unfortunately it doesn't.
    It lists all files for one subfolder but not for every subfolder.
    Why? What am I doing wrong ?

    Is there any way to tell QFtp -> list() to list folders first ?
    Like in
    Qt Code:
    1. QDir::DirsFirst
    To copy to clipboard, switch view to plain text mode 
    .
    Also I would like to list them as /subfolder/file or /subfolder/subfolder/file but QUrlInfo doesn't have a function to return the path to a file.
    Is there any way to convert it to QUrl?
    Any help is appreciated.
    May the source be with you!

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFtp: downloading a whole folder from a remote server.

    Quote Originally Posted by balazsbela View Post
    So if I'm right this should list all folders and files(even in subfolders) in the folder I want to download.
    [...]
    Unfortunately it doesn't.
    The problem is that you don't change the current directory.

    Try something like:
    Qt Code:
    1. void getFolder(QString path)
    2. {
    3. pendingPaths.append( path );
    4. connect(ftp,SIGNAL(listInfo(QUrlInfo)),this,SLOT(processFolder(QUrlInfo)));
    5. connect(ftp,SIGNAL(commandStarted(int)),this,SLOT(commandStarted(int)));
    6. ftp->list(path);
    7. }
    8.  
    9. void processFolder(const QUrlInfo &i)
    10. {
    11. QTextStream cout(stdout, QIODevice::WriteOnly);
    12. if (i.isDir())
    13. {
    14. cout <<"Directory:"<< i.name()<<"\n";
    15. QString dir = currentPath + QDir::separator() + i.name();
    16. pendingPaths.append( dir );
    17. ftp->list( dir );
    18. }
    19. ...
    20. }
    21.  
    22. void commandStarted( int id )
    23. {
    24. Q_UNUSED( id );
    25. if( ftp->currentCommand() == QFtp::List ) {
    26. currentPath = pendingPaths.first();
    27. pendingPaths.removeFirst();
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jul 2007
    Location
    Cluj Napoca, Romania
    Posts
    15
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QFtp: downloading a whole folder from a remote server.

    Nevermind I got it working, but it was a lot more complicated:
    Here is the code:

    Qt Code:
    1. void qOrganizer::getFolder(QString path,QString localPath)
    2. {
    3. QTextStream cout(stdout, QIODevice::WriteOnly);
    4. QDir localDir(localPath);
    5. listCommandVector.append(ftp->list(path));
    6. stack.push(path);
    7. localDir.mkdir(path);
    8. connect(ftp,SIGNAL(listInfo(QUrlInfo)),this,SLOT(processFolder(QUrlInfo)));
    9. connect(ftp,SIGNAL(commandFinished(int,bool)),this,SLOT(processCommand(int,bool)));
    10. ftpEntryes = new QStringList;
    11. lpath=localPath;
    12. }
    13.  
    14. void qOrganizer::processFolder(const QUrlInfo &i)
    15. {
    16. if(i.isDir())
    17. {
    18. entryList.push_front(i);
    19. }
    20. else
    21. if(i.isFile())
    22. entryList.push_back(i);
    23. }
    24.  
    25. static int nrcomfin=0;
    26. void qOrganizer::processCommand(int com,bool err)
    27. {
    28. nrcomfin++;
    29. QString path;
    30. QTextStream cout(stdout, QIODevice::WriteOnly);
    31. bool containsSubfolders=0;
    32. bool entryExists=0;
    33. QDir localDir(lpath);
    34.  
    35. if(listCommandVector.indexOf(com)!=-1)
    36. {
    37. path = stack.top();
    38. foreach(QUrlInfo entry,entryList)
    39. {
    40. if((entry.isFile())&&(listedFiles.indexOf(entry)==-1))
    41. {
    42. QString foldPath = path+"/"+entry.name();
    43. ftpEntryes->append(foldPath);
    44. listedFiles.append(entry);
    45. cout <<"File:"<<foldPath<<"\n";
    46. entryExists=1;
    47. }
    48. else
    49. if((entry.isDir())&&(listedFolders.indexOf(entry)==-1))
    50. {
    51. QString foldPath = path+"/"+entry.name();
    52. // cout <<"Folder:"<<foldPath<<"\n";
    53. stack.push(foldPath);
    54. localDir.mkdir(foldPath);
    55. // cout << stack.top() <<"\n";
    56. listCommandVector.append(ftp->list(foldPath));
    57. listedFolders.append(entry);
    58. containsSubfolders=1;
    59. entryExists=1;
    60. break;
    61. }
    62. }
    63. if((stack.size()>1)&&(nrcomfin%2!=0)) stack.pop();
    64. entryList.clear();
    65. //cout << stack.size() << stack.top() <<" "<<nrcomfin<<"\n";
    66. if((!containsSubfolders)&&(entryExists))
    67. listCommandVector.append(ftp->list(stack.top()));
    68. if((!containsSubfolders) && (!entryExists)) downloadFiles();
    69. }
    70. else
    71. if(getCommandVector.indexOf(com)!=-1)
    72. {
    73. ftpFile=fileQueue.dequeue();
    74. ftpFile-> close();
    75. cout <<"Completed:"<<ftpFile->fileName()<<" "<<"Errors:"<<err<<"\n";
    76. ftpFile->deleteLater();
    77. };
    78. }
    79.  
    80. void qOrganizer::downloadFiles()
    81. {
    82. QTextStream cout(stdout, QIODevice::WriteOnly);
    83. cout <<"Downloading"<<"\n";
    84. foreach(QString entry,*ftpEntryes)
    85. {
    86. QFile *file = new QFile(lpath+entry);
    87. cout << "get " << entry <<" "<<lpath+entry<<"\n";
    88. fileQueue.enqueue(file);
    89. file->open(QIODevice::WriteOnly);
    90. getCommandVector.append(ftp -> get(entry,file));
    91. }
    92. }
    To copy to clipboard, switch view to plain text mode 

    Not too pretty but it works!
    May the source be with you!

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.