PDA

View Full Version : Self-extracting archive



atomic
7th April 2015, 12:59
Hi,
In my application I used qCompress to compress some files and now I would like to create self-extracting archive. It is possible to create self-extracting archive using Qt Installer Framework? I do not know how I can use function like qUncompress from Qt Installer Framework to uncompress my data.
Thanks,

wysota
7th April 2015, 14:44
qCompress() and qUncompress() work on data, they do not create any files.

atomic
7th April 2015, 15:47
Apparently my question was not clear.
I have function which compresses file using qCompress , QFile and QByteArray



void compress(QString sourceFile , QString compressFile)
{
QFile file( sourceFile );
QFile outfile( compressFile );
file.open(QIODevice::ReadOnly);
outfile.open(QIODevice::WriteOnly);
QByteArray data = file.readAll();
QByteArray compressedData = qCompress( data, 9 );
outfile.write( compressedData );
file.close();
outfile.close();
}


And now I would like to create self-extracting option for this file and I think about Qt Installer framework. Maybe you know if I can use it for this purpose?

wysota
7th April 2015, 22:02
How would an installer framework create a self-extracting archive? It's an installer framework and not a self-extracting archiver, it creates installers and not self-extracting archives.

And I have no idea how your code snippet is related to the question.

atomic
7th April 2015, 23:09
And I have no idea how your code snippet is related to the question.


For a compressFile which I use in this code I need create a self-extracting archive.
And I saw that Qt Installer Framework can be customizable so I ask if I can use it for this purpose but ok I understood that not.
So how I can write this tool using Qt and C++?

wysota
7th April 2015, 23:32
You can write a program that will uncompress a file embedded into a Qt resource to disk. You don't need to qCompress() the file as resources are already compressed.


#include <QApplication>
#include <QFileDialog>
#include <QFile>
#include <QString>

int main(int main, char **argv) {
QApplication app(argc, argv);
QString path = QFileDialog::getSaveFileName();
if(path.isEmpty()) return 0;
QFile f(":/myfile");
QFile outfile(path);
outfile.open(QFile::WriteOnly);
f.open(QFile::ReadOnly);
while(!f.atEnd()) {
QByteArray data = f.read(4096);
outfile.write(data);
}
return 0;
}

atomic
8th April 2015, 12:38
Thanks for thats, cool idea.
But I need create application which allow users to create compress / self-extracting archive. I have some idea but I do not know if I can run script like bash / python or my external program to uncompress when Qt Installer finished instalation( when user click 'finish' button )?

ChrisW67
8th April 2015, 21:55
Assuming you want a generic solution, i.e. One that can make a self-extracting compressed file of an arbitrary input file, then you need to write two applications and do some platform-dependent work.

The first application you need is a self-contained, small program that uncompress a stream of data embedded within its own executable. This cannot easily depend on any external library other than those absolutely standard on the target platform; ruling out Qt most likely.

The second application is one that takes an arbitrary file, compresses it and writes a copy of the first program executable, the compressed data, and probably some metadata like original file name and a checksum into a combined file.

The platform-specific part is how you take a complete executable (program 1) and append or insert the compressed data into it so that it will still run and can find the embedded data within itself to uncompress. This can be anything from reasonably easy to quite difficult depending the nature of executables on the platform and things like maintaining exe signatures or avoiding anti-virus defences. A quick Google for "append data to executable" (http://lmgtfy.com/?q=Append+data+to+executable) And variations on that theme will get you most of the way fairly quickly for Windows.