Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess + 7zip how to Extract file
Forum Updated to NodeBB v4.3 + New Features

QProcess + 7zip how to Extract file

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 908 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • TheCipo76T Offline
    TheCipo76T Offline
    TheCipo76
    wrote on last edited by TheCipo76
    #1

    Hi to all,
    i've to extract all file in a subdirectory with the same name of archive from a bvc file
    using 7zip console

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QProcess>
    #include <QDir>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    
    void MainWindow::on_pushButton_Estrai_clicked()
    {
        QString programma = "comando.cmd";
        QString comando = "7z x -y -o" + QDir::currentPath() + "/* " + QDir::currentPath() + "/*.bvc";
        QProcess UnZip;
        UnZip.setProgram(programma);
        UnZip.setArguments(QStringList(comando));
        UnZip.setWorkingDirectory(QDir::currentPath());
        UnZip.start();
        UnZip.waitForFinished();
        int S = UnZip.exitStatus();
        qDebug() << "Exit Status" << S;
        int C = UnZip.exitCode();
        qDebug() << "Exit Code" << C;
        int E = UnZip.error();
        qDebug() << "Error" << E;
    }
    

    comando.cmd (located in: C:\Users\tecni\Desktop\TaglioLaser )

    C:\Program Files\7-Zip\7z.exe
    

    Application Output

    08:34:02: Starting C:\Users\tecni\Documents\ProgettiQt\build-prova-Desktop_Qt_6_5_0_MinGW_64_bit-Debug\debug\prova.exe...
    argomenti "7z x -y -oC:/Users/tecni/Desktop/TaglioLaser/* C:/Users/tecni/Desktop/TaglioLaser/*.bvc"
    Working Directory "C:/Users/tecni/Desktop/TaglioLaser"
    Exit Status 0
    Exit Code 1
    Error 5
    08:34:24: C:\Users\tecni\Documents\ProgettiQt\build-prova-Desktop_Qt_6_5_0_MinGW_64_bit-Debug\debug\prova.exe exited with code 0

    7zip exit code:

    Exit Codes from 7-Zip
    7-Zip returns the following exit codes:

    Code Meaning
    0 No error
    1 Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed.
    2 Fatal error
    7 Command line error
    8 Not enough memory for operation
    255 User stopped the process

    QProcess Error

    enum QProcess::ProcessError

    This enum describes the different types of errors that are reported by QProcess.
    Constant Value Description
    QProcess::FailedToStart 0 The process failed to start. Either the invoked program is missing, or you may have insufficient permissions or resources to invoke the program.
    QProcess::Crashed 1 The process crashed some time after starting successfully.
    QProcess::Timedout 2 The last waitFor...() function timed out. The state of QProcess is unchanged, and you can try calling waitFor...() again.
    QProcess::WriteError 4 An error occurred when attempting to write to the process. For example, the process may not be running, or it may have closed its input channel.
    QProcess::ReadError 3 An error occurred when attempting to read from the process. For example, the process may not be running.
    QProcess::UnknownError 5 An unknown error occurred. This is the default return value of error().

    Any ideas??
    Thanks in advance

    JonBJ B 2 Replies Last reply
    0
    • TheCipo76T TheCipo76

      Hi to all,
      i've to extract all file in a subdirectory with the same name of archive from a bvc file
      using 7zip console

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QProcess>
      #include <QDir>
      #include <QDebug>
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      
      
      void MainWindow::on_pushButton_Estrai_clicked()
      {
          QString programma = "comando.cmd";
          QString comando = "7z x -y -o" + QDir::currentPath() + "/* " + QDir::currentPath() + "/*.bvc";
          QProcess UnZip;
          UnZip.setProgram(programma);
          UnZip.setArguments(QStringList(comando));
          UnZip.setWorkingDirectory(QDir::currentPath());
          UnZip.start();
          UnZip.waitForFinished();
          int S = UnZip.exitStatus();
          qDebug() << "Exit Status" << S;
          int C = UnZip.exitCode();
          qDebug() << "Exit Code" << C;
          int E = UnZip.error();
          qDebug() << "Error" << E;
      }
      

      comando.cmd (located in: C:\Users\tecni\Desktop\TaglioLaser )

      C:\Program Files\7-Zip\7z.exe
      

      Application Output

      08:34:02: Starting C:\Users\tecni\Documents\ProgettiQt\build-prova-Desktop_Qt_6_5_0_MinGW_64_bit-Debug\debug\prova.exe...
      argomenti "7z x -y -oC:/Users/tecni/Desktop/TaglioLaser/* C:/Users/tecni/Desktop/TaglioLaser/*.bvc"
      Working Directory "C:/Users/tecni/Desktop/TaglioLaser"
      Exit Status 0
      Exit Code 1
      Error 5
      08:34:24: C:\Users\tecni\Documents\ProgettiQt\build-prova-Desktop_Qt_6_5_0_MinGW_64_bit-Debug\debug\prova.exe exited with code 0

      7zip exit code:

      Exit Codes from 7-Zip
      7-Zip returns the following exit codes:

      Code Meaning
      0 No error
      1 Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed.
      2 Fatal error
      7 Command line error
      8 Not enough memory for operation
      255 User stopped the process

      QProcess Error

      enum QProcess::ProcessError

      This enum describes the different types of errors that are reported by QProcess.
      Constant Value Description
      QProcess::FailedToStart 0 The process failed to start. Either the invoked program is missing, or you may have insufficient permissions or resources to invoke the program.
      QProcess::Crashed 1 The process crashed some time after starting successfully.
      QProcess::Timedout 2 The last waitFor...() function timed out. The state of QProcess is unchanged, and you can try calling waitFor...() again.
      QProcess::WriteError 4 An error occurred when attempting to write to the process. For example, the process may not be running, or it may have closed its input channel.
      QProcess::ReadError 3 An error occurred when attempting to read from the process. For example, the process may not be running.
      QProcess::UnknownError 5 An unknown error occurred. This is the default return value of error().

      Any ideas??
      Thanks in advance

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @TheCipo76 said in QProcess + 7zip how to Extract file:

      UnZip.setArguments(QStringList(comando));

      You have to pass the arguments as a list of strings for each element. You have simply created a single element holding all the arguments. Hence it won't work.

      If after the command finishes you printed out what arrived on standard output/error, you would presumably a suitable error message from the command.

      You do not say and I don't know what comando.cmd is as a program or what it does with its arguments, so actually behaviour depends on that. Answer this first, as it determines how you must pass arguments to it correctly.

      Oh:

      comando.cmd (located in: C:\Users\tecni\Desktop\TaglioLaser )

      C:\Program Files\7-Zip\7z.exe

      OK, I don't understand what you are trying to do. If comando.cmd contains only the 7z.exe line you show then where are you passing any arguments to it to get it do the unzipping??

      Furthermore it won't be correct anyway: because that path contains a space character it will need to be quoted:

      "C:\Program Files\7-Zip\7z.exe"

      And what is the 7z doing at the start of the arguments??

      You need to get yourself to a point where you have a command which works right outside of QProcess::start() before you can get that right/working.

      1 Reply Last reply
      2
      • TheCipo76T TheCipo76

        Hi to all,
        i've to extract all file in a subdirectory with the same name of archive from a bvc file
        using 7zip console

        mainwindow.cpp

        #include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include <QProcess>
        #include <QDir>
        #include <QDebug>
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        
        
        
        void MainWindow::on_pushButton_Estrai_clicked()
        {
            QString programma = "comando.cmd";
            QString comando = "7z x -y -o" + QDir::currentPath() + "/* " + QDir::currentPath() + "/*.bvc";
            QProcess UnZip;
            UnZip.setProgram(programma);
            UnZip.setArguments(QStringList(comando));
            UnZip.setWorkingDirectory(QDir::currentPath());
            UnZip.start();
            UnZip.waitForFinished();
            int S = UnZip.exitStatus();
            qDebug() << "Exit Status" << S;
            int C = UnZip.exitCode();
            qDebug() << "Exit Code" << C;
            int E = UnZip.error();
            qDebug() << "Error" << E;
        }
        

        comando.cmd (located in: C:\Users\tecni\Desktop\TaglioLaser )

        C:\Program Files\7-Zip\7z.exe
        

        Application Output

        08:34:02: Starting C:\Users\tecni\Documents\ProgettiQt\build-prova-Desktop_Qt_6_5_0_MinGW_64_bit-Debug\debug\prova.exe...
        argomenti "7z x -y -oC:/Users/tecni/Desktop/TaglioLaser/* C:/Users/tecni/Desktop/TaglioLaser/*.bvc"
        Working Directory "C:/Users/tecni/Desktop/TaglioLaser"
        Exit Status 0
        Exit Code 1
        Error 5
        08:34:24: C:\Users\tecni\Documents\ProgettiQt\build-prova-Desktop_Qt_6_5_0_MinGW_64_bit-Debug\debug\prova.exe exited with code 0

        7zip exit code:

        Exit Codes from 7-Zip
        7-Zip returns the following exit codes:

        Code Meaning
        0 No error
        1 Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed.
        2 Fatal error
        7 Command line error
        8 Not enough memory for operation
        255 User stopped the process

        QProcess Error

        enum QProcess::ProcessError

        This enum describes the different types of errors that are reported by QProcess.
        Constant Value Description
        QProcess::FailedToStart 0 The process failed to start. Either the invoked program is missing, or you may have insufficient permissions or resources to invoke the program.
        QProcess::Crashed 1 The process crashed some time after starting successfully.
        QProcess::Timedout 2 The last waitFor...() function timed out. The state of QProcess is unchanged, and you can try calling waitFor...() again.
        QProcess::WriteError 4 An error occurred when attempting to write to the process. For example, the process may not be running, or it may have closed its input channel.
        QProcess::ReadError 3 An error occurred when attempting to read from the process. For example, the process may not be running.
        QProcess::UnknownError 5 An unknown error occurred. This is the default return value of error().

        Any ideas??
        Thanks in advance

        B Offline
        B Offline
        Bonnie
        wrote on last edited by
        #3

        Should be something like this

        QString programma = "C:/Program Files/7-Zip/7z.exe";
        QStringList args{"x", "-y", "-o", "./*",  "./*.bvc"};
        QProcess UnZip;
        UnZip.setProgram(programma);
        UnZip.setArguments(args);
        
        1 Reply Last reply
        2
        • TheCipo76T Offline
          TheCipo76T Offline
          TheCipo76
          wrote on last edited by
          #4

          Ok, now i've fixed it
          Thanks to all

          1 Reply Last reply
          0
          • TheCipo76T TheCipo76 has marked this topic as solved on

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved