PDA

View Full Version : how to unzip the file in qt using gzip



iswaryasenthilkumar
23rd March 2015, 05:43
am having zip file in server while i downloading i have to unzip the file using qt..still i dont get proper solution for this :(
qt experts can any one give me suggestion for this:confused:
Tahnks in advance:o

jefftee
23rd March 2015, 06:05
Use QProcess to run gunzip.

anda_skoa
23rd March 2015, 09:00
Or use KArchive http://inqlude.org/libraries/karchive.html

Cheers,
_

iswaryasenthilkumar
23rd March 2015, 10:59
can you give some examples plss.
Use QProcess to run gunzip.

i have to unzip the files using gzip.. :(
Or use KArchive http://inqlude.org/libraries/karchive.html

Cheers,
_

jefftee
23rd March 2015, 14:38
can you give some examples plss.
Not until you have read the docs for QProcess and attempted to implement. :)

iswaryasenthilkumar
24th March 2015, 05:00
i read and implemented in my code bt i dont get any result from my code
Not until you have read the docs for QProcess and attempted to implement. :)

jefftee
24th March 2015, 05:08
Did the QProcess start/run?
Did you connect the started(), finished(), and error() signals? Did any of the signals fire?
What was the exit code and exit status for your QProcess?

Answer these questions and post some code using
tags please.

iswaryasenthilkumar
24th March 2015, 05:18
this was my code


#include "widget.h"
#include "ui_widget.h"
#include<QtGui>

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{

QString program = "/home/digital_images/Desktop/zipfile.zip.gz";//path to zip file
QStringList arguments;
myProcess = new QProcess(this);
connect (myProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(printOut()));
connect (myProcess, SIGNAL(readyReadStandardError()), this, SLOT(printError()));
myProcess->start(program, arguments);
myProcess->waitForFinished();
ui->setupUi(this);
}

Widget::~Widget()
{
delete ui;
}
void Widget::printOut()
{
QByteArray byteArray = myProcess->readAllStandardOutput();
QStringList strLines = QString(byteArray).split("\n");

foreach (QString line, strLines)
{
qDebug()<<line<<"Outputline";
}

}
void Widget::printError()
{
QByteArray byteArray = myProcess->readAllStandardError();
QStringList strLines = QString(byteArray).split("\n");

foreach (QString line, strLines)
{
qDebug()<<"Errorline"<<line;
}
}
what am doing wrong please guide me

Did the QProcess start/run?
Did you connect the started(), finished(), and error() signals? Did any of the signals fire?
What was the exit code and exit status for your QProcess?

Answer these questions and post some code using
tags please.

jefftee
24th March 2015, 05:24
First, you are setting the program to the zip file. Aren't you attempting to run program gunzip instead? The file name of your zip file should be one of the arguments you pass to the gunzip program.

What is the return code from waitForFinished? If you are not going to use the started, finished, and error signals, then you should also waitForStarted() after you start the QProcess and check its return code as well.

Edit: The waitFor* methods will block until they either timeout or the condition is satisfied, so if you are doing this from your GUI thread, your GUI will freeze, which is why I recommend you use the started, finished, and error signals instead.

iswaryasenthilkumar
24th March 2015, 05:34
QString program = " " //this should contain path of the program??
QStringList arguments;
arguments=" " // this should contain path of the zip file??

Added after 5 minutes:


QString program = "./path/to/Qt/examples/widgets/analogclock";
QStringList arguments;
arguments << "-style" << "motif";

QProcess *myProcess = new QProcess(parent);
myProcess->start(program, arguments);
i have doubt in above i mentioned code.. what is meaning for this line arguments << "-style" << "motif";
waht ia have to mention in QString program = "./path/to/Qt/examples/widgets/analogclock";

jefftee
24th March 2015, 05:36
Yes, program is the executable you wish to run and the arguments contain the arguments you are going to pass to the program. In the example below, /usr/bin/gunzip is the program and the file path is the argument:



/usr/bin/gunzip /some/path/to/your/zip/file

You might want to pass other arguments to gunzip like:


/usr/bin/gunzip -v /some/path/to/your/zip/file

Where -v is the first argument and the file path is the 2nd agrument, etc. Pretty much exactly what you would type on the command line.

Edit: The example you cited from the QProcess documentation runs a program called analogclock and passes arguments -style and motif, which for Qt GUI programs, tells Qt which style to use for the GUI. Since you are trying to run gunzip, you need to pass arguments to gunzip that make sense to gunzip. Run gunzip -h from the command line and see what arguments it understands, etc.

iswaryasenthilkumar
24th March 2015, 05:50
Thank you so much jthomps..soon i will implement the Qprocess concept
/usr/bin/gunzip...i checked in my terminal.. i getting error no file or directory for gunzip..is there any files to download for gunzip

jefftee
24th March 2015, 05:59
try "which gunzip".... if it's installed and in your PATH, it will be found. If not, you'll need to figure out how to install gunzip and make it available for your operating system.

Radek
24th March 2015, 06:55
Try unzip instead of "gunzip". "gunzip" is, most likely, a Gnome utility (guessing from the prefix "g"). If you have KDE or LXDE then you have no "gunzip". First, open your package downloader and check whether unzip is installed. If it is, replace "gunzip" by "unzip" in your code.

If you have KDE, the better solution is using KZip and KArchiveEntry objects from libkdecore. libkdecore manual is at api.kde.org

anda_skoa
24th March 2015, 08:53
If you have KDE, the better solution is using KZip and KArchiveEntry objects from libkdecore. libkdecore manual is at api.kde.org

KArchive is available as a stand-alone library.
It is a so-called Tier 1 Framework of KDE Frameworks, i.e. one without any other dependencies than Qt itself.

Cheers,
_

Lesiok
24th March 2015, 09:11
Or try this library (http://quazip.sourceforge.net/)

Radek
24th March 2015, 09:31
AFAIK, libkarchive wasn't released as a stand-alone library. You need source (available at GIT) and compile. Or my information is slightly outdated :) Nevertheless, assuming KDE, libkdecore is already in use so that linking libkdecore is almost no overhead. libkarchive is a part of libkdecore.

Naturally, if we speak a KDE-independent Qt app, then downloading libkarchive and compiling is the right solution. It solves all problems with unzip, gunzip, what was installed and what wasn't, and so on.

iswaryasenthilkumar
24th March 2015, 10:51
gzip was installed in my linux bt i couldn't find out were it was placed

anda_skoa
24th March 2015, 11:25
AFAIK, libkarchive wasn't released as a stand-alone library.

First release was July 7th, 2014
https://www.kde.org/announcements/kde-frameworks-5.0.php

The main questions is: why on earth is a ZIP file again gzipped?

Cheers,
_

iswaryasenthilkumar
24th March 2015, 11:25
i wriiten code

QString program="/home/digital_images/todaytomrowcheck";//this was my qt program path
QStringList arguments;
arguments<<" /home/digital_images/Desktop/zipfile.zip.gz"; // this was my zipfile path
myProcess=new QProcess(this);
myProcess->start(program,arguments);
QByteArray result=myProcess->readAll();
qDebug()<<result;
the program executed bt i dont get any result i get " " this output..actually i missing something i need after reading this zipfile i need to extract the zip file.. give some idea to implement :(
Thanks in advance:)

anda_skoa
24th March 2015, 11:28
How do you expect and output when the process has not run when you try to read the output?
There is no time machine in QProcess, it can only deliver output when the program it runs has written some.

A program needs to run in order to generate output.

Also, program refers to, well a program.
Your "program" variable has way more than just the program.
Maybe you should consult a dictionary between English and your native language on "program" and "argument"

Btw, why is your ZIP file again compressed?

Cheers,
_

Lesiok
24th March 2015, 11:37
QProcess::start only starts the process - don't wait for finish.

iswaryasenthilkumar
24th March 2015, 11:43
thanks i will try to improve ..can you please give some examples for Qprocess in start,finished,error..

jefftee
24th March 2015, 17:11
Try unzip instead of "gunzip". "gunzip" is, most likely, a Gnome utility (guessing from the prefix "g").
The OP said he needed to uncompress a file using gzip. The counterpart of gzip is gunzip where the "g" stands for GNU and these are standard command line programs available for every OS.

Added after 9 minutes:


i wriiten code

QString program="/home/digital_images/todaytomrowcheck";//this was my qt program path
QStringList arguments;
arguments<<" /home/digital_images/Desktop/zipfile.zip.gz"; // this was my zipfile path
myProcess=new QProcess(this);
myProcess->start(program,arguments);
QByteArray result=myProcess->readAll();
qDebug()<<result;
the program executed bt i dont get any result i get " " this output..actually i missing something i need after reading this zipfile i need to extract the zip file.. give some idea to implement :(
Thanks in advance:)
Perhaps I was not explicit enough in prior posts but the program variable needs to specify the binary executable you want to execute in a separate process with QProcess. Even more explicitly:



QString program="gunzip";
QStringList arguments;
arguments << "/home/digital_images/Desktop/zipfile.zip.gz"; // this was my zipfile path (note that I removed the leading space you had coded)


If gunzip isn't in your path in a standard location like /usr/bin, then you will need to specify the full path to the gunzip executable or modify the PATH environmental variable used by QProcess, but that is a more advanced step and I'm not going to go there now until you comprehend the basics for QProcess.

Go to your terminal and type the command "which gunzip". If it is found in your path statement, the fully qualified path will be shown. If it is not found, then you don't have gzip/gunzip properly installed and will need to install the gzip/gunzip package for your OS.

Edit: if QProcess proves to be too hard for you to implement, then you can go down the path others have identified by using a library to uncompress your file, but that will require a different set of skills and a slew of new challenges for you to implement.

iswaryasenthilkumar
25th March 2015, 05:10
finally i got idea to implement how to use Qprocess bt i dont get proper result
my code below


QProcess OProcess;
QString Command;
QStringList args;
Command = "bash -c \"gzip -c file1.doc > file1.zip\"";
args<<"/home/digital_images";
OProcess.start(Command,args,QIODevice::ReadOnly);
OProcess.waitForFinished();
QString StdOut = OProcess.readAllStandardOutput();
QString StdError = OProcess.readAllStandardError();
std::cout<<"\n Printing the standard output..........\n";
std::cout<<endl<<StdOut.toStdString();
std::cout<<"\n Printing the standard error..........\n";
std::cout<<endl<<StdError.toStdString();
here my file was not converted to zip,,bt while terminal it was executing properly what am doing wrong here

jefftee
25th March 2015, 05:36
finally i got idea to implement how to use Qprocess bt i dont get proper result
Sorry to disagree, but it's obvious you still don't understand what you're doing... :)

First off, are you trying to compress a file or uncompress a file? Your original post said you were trying to uncompress a file, but your [still] incorrect example, well, I'm no longer sure what you are trying to accomplish actually.

Secondly, you don't need to execute the bash shell, so no clue why you have added that. Even if you wanted to execute bash, the program variable would be bash and everything after bash in your example would be arguments to the bash program.

To be very clear, the program is the executable binary you are trying to execute and all of the arguments that need to be passed to that program need to be supplied as program arguments.

Even then, the example you posted attempts to compress file1.doc, sending the output to stdout (gzip -c), which you then direct (>) to file file1.zip and then you are supplying what, a random directory for some unknown reason.

I had already posted exactly what your program and program arguments variable should be. I don't know how to help you if you keep ignoring what I say and then you cobble together some incomprehensible code from what I can only imagine are the result of some mashup of different Google searches!

Use the example I already posted and come back with actual results, return codes, etc or else I literally don't know how to help you.

Edit: I hate myself for doing this :), but here's a working example for what you're trying to accomplish:



bool success;
QProcess gunzip;
gunzip.setProcessChannelMode(QProcess::MergedChann els);
QString program = "gunzip";
QStringList arguments = {"-v","/Users/jefft/test.dat.gz"};
gunzip.start(program, arguments);
success = gunzip.waitForStarted();
if (!success)
{
qDebug("waitForStarted() error: %s", gunzip.errorString().toUtf8().constData());
return;
}
success = gunzip.waitForFinished();
if (!success)
{
qDebug("waitForFinished() error: %s", gunzip.errorString().toUtf8().constData());
return;
}
QByteArray buffer = gunzip.readAll();
QString output = buffer;
qDebug("%s", output.toUtf8().constData());


The code above will uncompress file test.dat.gz and produces the following output, which you would want to parse to get the uncompressed file name:



/Users/jefft/test.dat.gz: -99.9% -- replaced with /Users/jefft/test.dat


The code above uses QProcess::waitFor*, which I don't recommend because your GUI will hang. Not a big deal if your files are small, but uncompressing a large file would noticeably hang your GUI. Start with this and once you have working, replace with started() and finished() signals connected to slots in your program.

iswaryasenthilkumar
25th March 2015, 06:08
sorry i forget to mention in last post i triying nw Qprocess concept .. first let me try to convert zip file tats y i tried that code..soryy if i hurted you jthomps..am very slow pick bt am sure i will reach my destiny i dnt know when.

jefftee
25th March 2015, 06:17
You didn't hurted me, I'll be OK... :)

If you want help in the future, you'll find that people are much more willing to help if you don't ignore what they suggest and come back with the same questions or a completely different code sample. i.e. If you are trying to solve a different problem than the one you state in your original post, then start a new thread.

Hopefully you can take the example I gave you in my prior post and modify that to accomplish whatever it is now that you're trying to accomplish. It shows the proper way to use the program and program arguments passed to the program. It shows proper checking of return codes, etc.

Good luck.

Radek
25th March 2015, 07:10
AFAIK, gzip both compresses and uncompresses: "gzip -d file(s)". I haven't heard about "gunzip" so far but now I understand better :) The "gunzip" is needed because of "double zipping" .zip.gz Therefore:
(1) first "ungz" by something, for example by "gzip"
(2) then unzip by libkarchive (if you are using Qt5), libkdecore or "unzip"

... and do not "double zip". You cannot gain anything by the second zipping.

iswaryasenthilkumar
25th March 2015, 09:45
i used your code its working and it resolved my problem i have one more doubt,,nw i want to unzip the folder not file am having zip folder(folder1.zip).i have to unzip this folder i used with terminal command unzip folder1.zip by terminal its executing when am using in qt i getting error.the error below
waitForFinished() error: Process operation timed out
QProcess: Destroyed while process is still running.
my code below

QString program = "unzip";
arguments<<"/home/digital_images/Desktop/folder1.zip";
gunzip.start(program, arguments);
Sorry to disagree, but it's obvious you still don't understand what you're doing... :)

First off, are you trying to compress a file or uncompress a file? Your original post said you were trying to uncompress a file, but your [still] incorrect example, well, I'm no longer sure what you are trying to accomplish actually.

Secondly, you don't need to execute the bash shell, so no clue why you have added that. Even if you wanted to execute bash, the program variable would be bash and everything after bash in your example would be arguments to the bash program.

To be very clear, the program is the executable binary you are trying to execute and all of the arguments that need to be passed to that program need to be supplied as program arguments.

Even then, the example you posted attempts to compress file1.doc, sending the output to stdout (gzip -c), which you then direct (>) to file file1.zip and then you are supplying what, a random directory for some unknown reason.

I had already posted exactly what your program and program arguments variable should be. I don't know how to help you if you keep ignoring what I say and then you cobble together some incomprehensible code from what I can only imagine are the result of some mashup of different Google searches!

Use the example I already posted and come back with actual results, return codes, etc or else I literally don't know how to help you.

Edit: I hate myself for doing this :), but here's a working example for what you're trying to accomplish:



bool success;
QProcess gunzip;
gunzip.setProcessChannelMode(QProcess::MergedChann els);
QString program = "gunzip";
QStringList arguments = {"-v","/Users/jefft/test.dat.gz"};
gunzip.start(program, arguments);
success = gunzip.waitForStarted();
if (!success)
{
qDebug("waitForStarted() error: %s", gunzip.errorString().toUtf8().constData());
return;
}
success = gunzip.waitForFinished();
if (!success)
{
qDebug("waitForFinished() error: %s", gunzip.errorString().toUtf8().constData());
return;
}
QByteArray buffer = gunzip.readAll();
QString output = buffer;
qDebug("%s", output.toUtf8().constData());


The code above will uncompress file test.dat.gz and produces the following output, which you would want to parse to get the uncompressed file name:



/Users/jefft/test.dat.gz: -99.9% -- replaced with /Users/jefft/test.dat


The code above uses QProcess::waitFor*, which I don't recommend because your GUI will hang. Not a big deal if your files are small, but uncompressing a large file would noticeably hang your GUI. Start with this and once you have working, replace with started() and finished() signals connected to slots in your program.

Lesiok
25th March 2015, 11:20
Dear iswaryasenthilkumar read the documentation it does not hurt. Read about QProcess::waitForFinished and everything will be clear.

iswaryasenthilkumar
25th March 2015, 11:35
i found command tar its used to zip the folder and unzip the folder..i executed in terminal its working properly for both zip and unzip,,while in qt i used to unzip the folder (Remote.tar.gz) the output executed properly bt its not extract the Remote folder.


QString program = "tar";
arguments<<"-zxvf"<<"/home/digital_images/Desktop/Remote.tar.gz";
gunzip.start(program, arguments);

i getting the output


Remote/
Remote/0/
Remote/0/light.jpg

after the execution complete while in desktop my remote.tar.gz not extracted what am doing wrong here :confused:

yeye_olive
25th March 2015, 13:23
What you are doing wrong is that you just want other people to solve your problems for you. You ignore the advice given to you, you do not read the documentation, you do not check (and tell us about) any error/return codes. I will only say this about your last question:
* what is the exit code of the tar process?
* what directory do you expect tar to extract the files to?
These are just the points I would start with. Good luck, and please try seriously to solve your problem before posting again.

jefftee
25th March 2015, 15:41
waitForFinished() error: Process operation timed out
QProcess: Destroyed while process is still running.


The error is shown above, QProcess::waitForFinished() reached the default timeout value (30,000 milliseconds or 30 seconds). Read the documentation to see if there's a way to prevent it from timing out.

iswaryasenthilkumar
26th March 2015, 04:58
first i tried for unzip the file now am trying to unzip the folder.thats why i choosed tar linux command
to compress
tar -zcvf digitalimages.tar.gz<directory> and to uncompress
tar -zxvf digitalimages.tar.gz
while i using this command in terminal its executing properly while am using in qt umcompress command its not extracted,the zip folder contains images of folder inside

What you are doing wrong is that you just want other people to solve your problems for you. You ignore the advice given to you, you do not read the documentation, you do not check (and tell us about) any error/return codes. I will only say this about your last question:
* what is the exit code of the tar process?
* what directory do you expect tar to extract the files to?
These are just the points I would start with. Good luck, and please try seriously to solve your problem before posting again.

jefftee
26th March 2015, 05:26
while i using this command in terminal its executing properly while am using in qt umcompress command its not extracted,the zip folder contains images of folder inside
Your posts stating it doesn't work are of no value if you don't show your code and output.

iswaryasenthilkumar
26th March 2015, 05:51
my code


bool success;
QProcess gunzip;
QStringList arguments;
gunzip.setProcessChannelMode(QProcess::MergedChann els);
QString program = "tar";
arguments<<"-zxvf"<<"/home/digital_images/Remote1.tar.gz";
gunzip.start(program, arguments);
success = gunzip.waitForStarted();
if (!success)
{
qDebug("waitForStarted() error: %s", gunzip.errorString().toUtf8().constData());
return;
}
success = gunzip.waitForFinished();
if (!success)
{
qDebug("waitForFinished() error: %s", gunzip.errorString().toUtf8().constData());
return;
}
QByteArray buffer = gunzip.readAll();
QString output = buffer;
qDebug("%s", output.toUtf8().constData());


output:


Remote1/ //folder
Remote1/0/ //0 folder
Remote1/0/light.jpg //inside 0 foler one image i putted

What am doing wrong here please help me to get proper result
Your posts stating it doesn't work are of no value if you don't show your code and output.

jefftee
26th March 2015, 05:59
Looks like it works to me. Why do you say it's not working?

iswaryasenthilkumar
26th March 2015, 06:01
bt i cant c my extracted folder,??

jefftee
26th March 2015, 06:04
What is the current working directory when your app runs? I am sure it's creating the folders and extracting the file, but that's being done in the current working directory.

If you want to control where the files are extracted to, look at "man tar" and see if the "-C" program argument might be of benefit to you. Your other option would be to change your working directory when your app is running.

iswaryasenthilkumar
26th March 2015, 06:42
i will try and update my result thank you for guiding me to get result

Added after 34 minutes:

Thanks for every one those who guided me to get final solution i got result what am expected thank you to all
jthomps i checked with man tar and executed -C in my code to specify the path, thank you its working this was i changed

QString program = "tar";
arguments<<"-zxvf"<<"/home/digital_images/Remote1.tar.gz"<<"-C"<<"/home/digital_images/Remote";
What is the current working directory when your app runs? I am sure it's creating the folders and extracting the file, but that's being done in the current working directory.

If you want to control where the files are extracted to, look at "man tar" and see if the "-C" program argument might be of benefit to you. Your other option would be to change your working directory when your app is running.