Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: unzip and untar into new directory

  1. #1
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default unzip and untar into new directory

    I have a file that is file.tar.gz and I would like to implement an open function from GUI menu that allows the user to select the tar.gz file and the application unzip, untar, and place those files in a specified directory. I don't think there is a Qt function that can do all of the above (but maybe some) but I was curious on any suggestions on how to implement this functionality.

    Thanks for any help!

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unzip and untar into new directory

    use QProcess to start an unzip utility e.g. 7z or equivalent.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unzip and untar into new directory

    Is there anyway to do it without using QProcess? I am trying to avoid needing to install any third party software with my application.

  4. #4
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unzip and untar into new directory

    Ok so I have decided to use peazip for the QProcess but I do not understand how to use QProcess very well. I downloaded the peazip portable and placed the executable in the same directory as my application. I tried to do the following:

    Qt Code:
    1. MainWindow::open()
    2. {
    3. QProcess *unarchive = new QProcess(this);
    4. QStringList arguments;
    5. arguments << "-ext2here" << "file.tar.gz";
    6. unarchive->start("peazip", arguments);
    7.  
    8. // other things happen
    9. }
    To copy to clipboard, switch view to plain text mode 

    Unfortunately nothing seems to be happening so I am curious what I am missing. Thanks for any help!

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: unzip and untar into new directory

    What sort of 'nothing' is happening? Is the QProcess object emitting an error() signal? Where is the peazip executable in relation to the current working directory of your process?

  6. #6
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unzip and untar into new directory

    how are you testing? by debugging through an IDE or just running your program 'manually'? If the former, you probably just have a problem where your app is not run from the same location as the unzip utility
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #7
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unzip and untar into new directory

    Duh! I was running it in debug mode and didn't put the peazip executable in the correct directory... It seems to be working fine now!!

    I do have another question that might be answered here, can you do an OS check with Qt? I don't see a Linux version of the peazip portable so I am thinking I will need a #if statement to check for Windows OS and use peazip but if it is Linux use standard unix unarchiving/archiving commands. How would you call the unix terminal to unzip and untar a file?

    Thanks again for any help!

  8. #8
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unzip and untar into new directory

    So I am able to have the QProcess execute the unzip and untar but when I try and run something after the untar nothing appears to be happening.

    Qt Code:
    1. QMainWindow::open()
    2. {
    3. if(okToContinue())
    4. {
    5. QString fileName = QFileDialog::getOpenFileName(this, tr("Open file.tar.gz file"), ".", tr("gzip tarball"
    6. "(*.tar.gz)"));
    7.  
    8. if(!fileName.isEmpty())
    9. {
    10. QProcess *unarchive = new QProcess(this);
    11.  
    12. QStringList arguments;
    13. arguments << "-ext2here" << fileName;
    14. unarchive->start("peazip", arguments);
    15.  
    16. if(unarchive->waitForFinished())
    17. {
    18. fileName.remove(".gz");
    19. arguments.clear();
    20. arguments << "-ext2here" << fileName;
    21. int exitCode = unarchive->execute("peazip", arguments);
    22.  
    23. if(unarchive->waitForFinish())
    24. {
    25. fileName.remove("file.tar");
    26. // this is the path that is extracted from tar and the .csv is what I am after
    27. fileName.append(("tmp/file/A.csv"));
    28.  
    29. loadFile(fileName);
    30. }
    31. }
    32. }
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

    It never seems to make it inside the second waitForFinish if statement. What am I doing wrong?

    Thanks for help!

  9. #9
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unzip and untar into new directory

    why are you mixing execute/start? Why are you allocating on the heap when you only want a local instance?

    I believe the problem is that you are using execute, which is a static method, therefore the finished state required for 'waitForFinished' is not modified.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  10. #10
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unzip and untar into new directory

    I was just experimenting with how QProcess works that is why the execute/start mixture is being used. The second question was again just me mimicking an exmaple I saw in the Qt Assitant manual. I went ahead and switched QProcess to allocate on the stack instead of the heap (which is definitely a better idea). Anyway I can't seem to figure out how to make the unzip and untar work and then have the newly created directory pick up the new file created in there.

    I keep getting the following error:
    Cannot read File:
    C:/.../tmp/file/A.csv
    The system cannot find path specified..

    I am a little confused as to why it can't find it when I go to the file directory and everything is as it should be.

    Here is the new code I am using:

    Qt Code:
    1. void MainWindow::open()
    2. {
    3. if(okToContinue())
    4. {
    5. QString fileName = QFileDialog::getOpenFileName(this, tr("Open file.tar.gz file"), ".", tr("gzip tarball"
    6. "(*.tar.gz)"));
    7.  
    8. if(!fileName.isEmpty())
    9. {
    10. QProcess unarchive(this);
    11.  
    12. QStringList arguments;
    13. arguments << "-ext2here" << fileName;
    14. unarchive.start("peazip", arguments);
    15.  
    16. if(unarchive.waitForFinished())
    17. {
    18. unarchive.close();
    19.  
    20. fileName.remove(".gz");
    21. arguments.clear();
    22. arguments << "-ext2here" << fileName;
    23. unarchive.start("peazip", arguments);
    24.  
    25. if(unarchive.waitForFinished())
    26. {
    27. unarchive.close();
    28.  
    29. fileName.remove("file.tar");
    30. cout << fileName.toStdString() << endl;
    31. fileName.append(("tmp/file/A.csv"));
    32. cout << fileName.toStdString() << endl;
    33. }
    34. }
    35.  
    36. loadFile(fileName);
    37. }
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

    My problem is when loadFile is being called the file is not being found and I am not sure why that is. FYI, I wasn't unziping and untaring automatically at first so I know if the fileName being passed to loadFile is correct it works as it should.

    Thanks for any help!


    Added after 20 minutes:


    It appears to be a permission problem maybe... I am still unsure but this code should help a little bit I think.

    Qt Code:
    1. if(!fileName.contains("global_urn_r.csv"))
    2. {
    3. QMessageBox::warning(this, tr("Loading Error"), tr("Must open a global_urn_r.csv file in order to load properly"));
    4.  
    5. return false;
    6. }
    7.  
    8. QFile inFile(fileName);
    9.  
    10. if(!inFile.open(QIODevice::ReadOnly))
    11. {
    12. QMessageBox::warning(this, tr("Unable to Load File"), tr("Cannot read file %1:\n%2.").arg(inFile.fileName()).arg(inFile.errorString()));
    13.  
    14.  
    15. statusBar()->showMessage(tr("Loading canceled"), 2000);
    16.  
    17. return false;
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jshafferman; 18th July 2012 at 20:52.

  11. #11
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unzip and untar into new directory

    Figured out that the loadFile function is running when the file is being untared... Not sure why it doesn't wait until untar is done?

  12. #12
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unzip and untar into new directory

    probably down to you re-using the same QProcess instance. Create a new instance for the second run.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  13. #13
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unzip and untar into new directory

    Tried that theory out as well and unfortunately doesn't seem to matter. But I was able to just make a while loop to wait until I find the directory I am looking for. This appears to be working but then I get completely lost as to why I can't traverse the directory. The code seems to find the directory then the very next statement says that it didn't find it, very confusing so I will just show it to you.

    Qt Code:
    1. QProcess untar(this);
    2. untar.start("peazip", arguments);
    3.  
    4. if(untar.waitForFinished())
    5. {
    6. untar.close();
    7.  
    8. fileName.remove("file.tar");
    9.  
    10. QDir dir(fileName);
    11.  
    12. while(!dir.cd("tmp"))
    13. cout << "Waiting for tmp to be created\n";
    14. }
    15.  
    16. QDir dir(fileName);
    17.  
    18. if(dir.cd("tmp"))
    19. cout << "tmp has been created and found\n";
    20.  
    21. if(!dir.cd("tmp"))
    22. qWarning("Cannot find the \"/tmp\" directory");
    23. else
    24. {
    25. if(!dir.cd("file"))
    26. qWarning("Cannot find the \"/file\" directory");
    27. else
    28. {
    29. QFile file(dir.filePath("A.csv"));
    30.  
    31. if(!file.exists())
    32. qWarning("A.csv");
    33. else
    34. loadFile(file.fileName());
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

    I don't understand why the output is the following:
    Waiting for tmp to be created
    ...
    tmp has been created and found
    Cannot find "/tmp" directory

    I am really confused as to why one line finds it the other doesn't...

  14. #14
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unzip and untar into new directory

    do you have directory tmp/tmp ? If not, what are you thinking?


    And also, what on earth are you doing with infinite loops waiting for directories? It just such a hack! If you really need to wait for file system things, then use qfilesystemwatcher
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  15. #15
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unzip and untar into new directory

    The directory path is correct it is C:\...\tmp\file\A.csv. Anyway on to your second question, I am still very new to Qt (just been using it about a year or so) and I haven't worked with traversing directories on my own. The infinite loops, in theory aren't infinite loops but if the files weren't extracted correctly then it could potentially have some serious issues. I know it is a hack but for now that is the only way I know how to keep checking directories. I have read some of the stuff on QFileSystemWatcher but I don't know how to use it properly although I will try. Could you maybe show me a sample of how that class would be used for what I am trying to do?

  16. #16
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unzip and untar into new directory

    if the directory is c:\...\tmp\a.csv, you cannot say you are also correct to look for c:\...\tmp\tmp, which is what you are doing.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  17. #17
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unzip and untar into new directory

    Oh I see what I did there, that was just me not understanding that cd took it to that directory when it found it. So when I successfully cd to temp, then the next call to cd tmp doesn't exst. I understand I made a mistake there. However I am still not sure how to use QFileSystemWatcher, I tried using a connect statement like the following:

    Qt Code:
    1. connect(&watcher, SIGNAL(directoryChanged(QString), this, SLOT(directoryUpdated(QString));
    To copy to clipboard, switch view to plain text mode 

    Where 'this' is the QMainWindow and the slot is a newly created slot that should be picking when the directory changes. In the slot function I currently have a simple cout statement so that I know it was making the connection however it doesn't appear that it is ever making it to the slot function. Basically I took out the first while loop while(!dir.cd("tmp")) inside of the if statement and got ride of all the other code and just have the following:

    Qt Code:
    1. watcher.addPath(fileName); // path where the tarball is being extracted
    2.  
    3. // connection statement here
    To copy to clipboard, switch view to plain text mode 

    Any Ideas what I am doing wrong?

  18. #18
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unzip and untar into new directory

    Quote Originally Posted by jshafferman View Post
    Where 'this' is the QMainWindow and the slot is a newly created slot that should be picking when the directory changes. In the slot function I currently have a simple cout statement so that I know it was making the connection however it doesn't appear that it is ever making it to the slot function. Basically I took out the first while loop while(!dir.cd("tmp")) inside of the if statement and got ride of all the other code and just have the following:

    Qt Code:
    1. watcher.addPath(fileName); // path where the tarball is being extracted
    2.  
    3. // connection statement here
    To copy to clipboard, switch view to plain text mode 

    Any Ideas what I am doing wrong?
    why don't you just show some complete code. Instead everybody has to code and compile in their head. It's not very endearing...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  19. #19
    Join Date
    Dec 2010
    Posts
    76
    Thanks
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: unzip and untar into new directory

    No problem here is the full chunk of code that I am trying to execute...

    Qt Code:
    1. void MainWindow::open()
    2. {
    3. if(okToContinue())
    4. {
    5. QString fileName = QFileDialog::getOpenFileName(this, tr("Open file.tar.gz file or A.csv"), ".", tr("Data Files (*.tar.gz *.tar *.csv)"));
    6.  
    7. if(!fileName.isEmpty() && fileName.contains("tar.gz"))
    8. {
    9. QProcess unarchive(this);
    10.  
    11. QStringList arguments;
    12. arguments << "-ext2here" << fileName;
    13. unarchive.start("peazip", arguments);
    14.  
    15. if(unarchive.waitForFinished())
    16. {
    17. unarchive.close();
    18.  
    19. fileName.remove(".gz");
    20. arguments.clear();
    21. arguments << "-ext2here" << fileName;
    22. }
    23.  
    24. QProcess untar(this);
    25. untar.start("peazip", arguments);
    26.  
    27. if(untar.waitForFinished())
    28. {
    29. untar.close();
    30.  
    31. fileName.remove("file.tar");
    32.  
    33. QDir dir(fileName);
    34.  
    35. while(!dir.cd("tmp"))
    36. cout << "Waiting for tmp to be created\n";
    37. }
    38.  
    39. QDir dir(fileName);
    40.  
    41. if(dir.cd("tmp"))
    42. {
    43. cout << "tmp has been created and found\n";
    44.  
    45. if(dir.cd("file"))
    46. {
    47. cout << "file has been created and found\n";
    48. cout << "Absolute path: " << dir.absolutePath().toStdString() << endl;
    49.  
    50. // says 11 when it should be 40...
    51. while(dir.count() < 40)
    52. {
    53. cout << "Number of files: " << dir.count() << endl;
    54. dir.refresh();
    55. }
    56.  
    57. QFile file(dir.filePath("A.csv"));
    58.  
    59. if(file.exists())
    60. {
    61. cout << "A.csv has been created and found\n";
    62. cout << "Filename is: " << file.fileName().toStdString() << endl;
    63. loadFile(file.fileName());
    64. }
    65. else
    66. {
    67. cout << "Cannot find A.csv\n";
    68. }
    69. }
    70. }
    71. }
    72. else if(!fileName.isEmpty() && fileName.contains("A.csv"))
    73. {
    74. loadFile(fileName);
    75. }
    76. }
    77. }
    To copy to clipboard, switch view to plain text mode 

    That is my 'hack' way of doing it right now. This is me trying to figure out how QFileSystemWatcher works in order to make it work without the nasty hacks.

    Qt Code:
    1. void MainWindow::open()
    2. {
    3. if(okToContinue())
    4. {
    5. QString fileName = QFileDialog::getOpenFileName(this, tr("Open file.tar.gz file or A.csv"), ".", tr("Data Files (*.tar.gz *.tar *.csv)"));
    6.  
    7. if(!fileName.isEmpty() && fileName.contains("tar.gz"))
    8. {
    9. QProcess unarchive(this);
    10.  
    11. QStringList arguments;
    12. arguments << "-ext2here" << fileName;
    13. unarchive.start("peazip", arguments);
    14.  
    15. if(unarchive.waitForFinished())
    16. {
    17. unarchive.close();
    18.  
    19. fileName.remove(".gz");
    20. arguments.clear();
    21. arguments << "-ext2here" << fileName;
    22. }
    23.  
    24. QProcess untar(this);
    25. untar.start("peazip", arguments);
    26.  
    27. if(untar.waitForFinished())
    28. {
    29. untar.close();
    30.  
    31. fileName.remove("file.tar");
    32. }
    33.  
    34. QFileSystem watcher;
    35. watcher.addPath(fileName);
    36.  
    37. connect(&watcher, SIGNAL(directoryChanged(String), this, SLOT(directoryModified(QString));
    38. }
    39. }
    40.  
    41. void MainWindow::directoryModified(const QString& path)
    42. {
    43. QMessageBox::information(this,"Directory Modified", "Your Directory is modified");
    44. // not sure what I am going to do yet...
    45. }
    To copy to clipboard, switch view to plain text mode 

    When I run the 'correct' way of doing things I never see the information box pop up... So I am assuming I am somehow not using QFileSystemWatcher class correctly. Any help is greatly appreciated.
    Last edited by jshafferman; 21st July 2012 at 00:08.

  20. #20
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: unzip and untar into new directory

    you put the watcher on the stack...
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. How to Zip/Unzip with QT for symbian???
    By baka3k in forum Newbie
    Replies: 2
    Last Post: 22nd June 2011, 08:24
  2. No such file or directory
    By Bertbeeg in forum Newbie
    Replies: 4
    Last Post: 24th October 2010, 23:04
  3. get .pro directory
    By Alnitak in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2010, 18:43
  4. Specify the DLL directory
    By Nyphel in forum Newbie
    Replies: 6
    Last Post: 27th April 2007, 14:29
  5. OpenOffice file to QTextEdit (Unzip Problem)
    By patrik08 in forum Qt Programming
    Replies: 6
    Last Post: 27th November 2006, 10:32

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.