I followed vahancho's suggestion of using QProcess::setStandardOutputFile() and it works fine. The code is:
#include <QObject>
#include <QString>
class TestZip : public QObject
{
Q_OBJECT
public:
TestZip(const QString file, const QString zip);
private slots:
void onZipReadyReadStandardOutput();
void onZipReadyReadStandardError();
void onZipFinished(int exitCode, QProcess::ExitStatus exitStatus);
private:
QProcess *_pZip;
};
#include <QFileInfo>
TestZip::TestZip(const QString file, const QString zip)
{
QFileInfo fileInfoZip(zip);
_pZip = new QProcess(this);
QString cmd = QString("gzip -c %1").arg(file);
_pZip->setStandardOutputFile(fileInfoZip.filePath());
Connect(_pZip, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(onZipFinished(int, QProcess::ExitStatus)));
Connect(_pZip, SIGNAL(readyReadStandardOutput()), this, SLOT(onZipReadyReadStandardOutput()));
Connect(_pZip, SIGNAL(readyReadStandardError()), this, SLOT(onZipReadyReadStandardError()));
_pZip->start(cmd);
if (!_pZip->waitForStarted())
{
qDebug() << "Zip failed to start: " << cmd;
_pZip->deleteLater();
_pZip = NULL;
}
}
void TestZip::onZipReadyReadStandardOutput()
{
qDebug() << __FUNCTION__ << _pZip->readAllStandardOutput();
}
void TestZip::onZipReadyReadStandardError()
{
qDebug() << __FUNCTION__ << _pZip->readAllStandardError();
}
void TestZip::onZipFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
qDebug() << __FUNCTION__ << "(" << exitCode << ", " << exitStatus << ")";
_pZip->deleteLater();
_pZip = NULL;
}