Bind different files program Qt
-
Hi i'm trying to bind different files in one with Qt. Why? Because it's more comfortable to have in one single file and when i want to open i open all.
But my problem is when i bind all files in one and change the resources of this one (the other's are corrupted). In other words, for example i change the icon in the output file(archivosalida) the other files are corrupted. So i was thinking to include my stub like resource because in my stub i add the other file's(all data) and their resources. So if i add my stub like resource when i change the icon in my single file, the resources of other's will not change. But i'm not sure if i can. I put the source of what things i'm trying.Binder:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QFileDialog> #include <QFile> #include <QTextStream> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); connect (ui->botonExaminar1,SIGNAL(clicked()),this,SLOT(examinar1())); connect (ui->botonExaminar2,SIGNAL(clicked()),this,SLOT(examinar2())); connect (ui->botonUnir,SIGNAL(clicked()),this,SLOT(juntar())); } MainWindow::~MainWindow() { delete ui; } void MainWindow::examinar1() { ui->ejecutable1Texto->setText(QFileDialog::getOpenFileName(this,"Abrir archivo")); } void MainWindow::examinar2() { ui->ejecutable2Texto->setText(QFileDialog::getOpenFileName(this,"Abrir archivo")); } void MainWindow::juntar() { /** función para juntar los dos ejecutables **/ /* declaraciones */ QFile archivoSalida; QFile *archivoEjecutable1; QFile *archivoEjecutable2; QFile *archivoStub; QByteArray tamano1; QByteArray tamano2; QByteArray tamano3; QByteArray trama(1024,'0'); QString nombreEjecutable1; QString nombreEjecutable2; /* inicializaciones */ archivoEjecutable1 = new QFile(); archivoEjecutable2 = new QFile(); archivoStub = new QFile(); /* establecer nombres de los ficheros */ archivoSalida.setFileName(QFileDialog::getSaveFileName(this,"Archivo de salida")); archivoEjecutable1->setFileName(ui->ejecutable1Texto->text()); archivoEjecutable2->setFileName(ui->ejecutable2Texto->text()); archivoStub->setFileName("./stubb.dat"); /* abrir ficheros */ archivoSalida.open(QFile::WriteOnly); archivoEjecutable1->open(QFile::ReadOnly); archivoEjecutable2->open(QFile::ReadOnly); archivoStub->open(QFile::ReadOnly); /* escribir en el fichero de salida los tres ejecutables */ archivoSalida.write(archivoStub->readAll() + archivoEjecutable1->readAll() + archivoEjecutable2->readAll()); /* Convertir los tamaños a QString */ tamano1.setNum(archivoStub->size()); tamano2.setNum(archivoEjecutable1->size()); tamano3.setNum(archivoEjecutable2->size()); /* Cojer los nombres de los ejecutables */ nombreEjecutable1 = archivoEjecutable1->fileName().split(QDir::separator()).last(); nombreEjecutable2 = archivoEjecutable2->fileName().split(QDir::separator()).last(); /* Crear la trama de datos */ trama = tamano1 + "|@|" + tamano2 + "|@|" + tamano3 + "|@|" + nombreEjecutable1.toLatin1() + "|@|" + nombreEjecutable2.toLatin1(); /* Escribir la trama en los últimos 1024 bytes del archivo de salida */ archivoSalida.write(trama,1024); /* Cerrar todos los ficheros */ archivoEjecutable1->close(); archivoEjecutable2->close(); archivoStub->close(); }
My Stub to run my other files in one.
#include "widget.h" #include "ui_widget.h" #include <QFile> #include <QDir> #include <QProcess> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); QDir directorio; QFile *archivoStub; QFile *archivoEjecutable1; QFile *archivoEjecutable2; QString trama; QString tamano1; QString tamano2; QString tamano3; QString nombre1; QString nombre2; archivoStub = new QFile(); archivoEjecutable1 = new QFile(); archivoEjecutable2 = new QFile(); archivoStub->setFileName(QApplication::applicationFilePath()); QFileInfo info1(QApplication::applicationFilePath()); QString fileName=info1.fileName(); archivoStub->open(QFile::ReadOnly); archivoStub->seek(archivoStub->size() - 1024); trama = archivoStub->read(1024); tamano1 = trama.split("|@|")[0]; tamano2 = trama.split("|@|")[1]; tamano3 = trama.split("|@|")[2]; nombre1 = trama.split("|@|")[3]; nombre2 = trama.split("|@|")[4]; archivoEjecutable1->setFileName(directorio.tempPath() + "/" + nombre1); archivoEjecutable1->open(QFile::WriteOnly); archivoStub->seek(tamano1.toInt()); archivoEjecutable1->write(archivoStub->read(tamano2.toInt())); archivoEjecutable1->close(); QProcess::startDetached(directorio.tempPath() + "/" + nombre1); archivoEjecutable2->setFileName(directorio.tempPath() + "/" + nombre2); archivoEjecutable2->open(QFile::WriteOnly); archivoStub->seek(tamano1.toInt() + tamano2.toInt()); archivoEjecutable2->write(archivoStub->read(tamano3.toInt())); archivoEjecutable2->close(); QProcess::startDetached(directorio.tempPath() + "/" + nombre2); QProcess process; process.start("taskkill /f /im "+fileName); process.waitForFinished(); process.waitForReadyRead(); qDebug("test"); QCoreApplication::quit(); } Widget::~Widget() { delete ui; }
My question how i can avoid that i change resources of my other's file or my stub to let me change the resources in my single file( the file with all my application's).
-
@RIVOPICO Are you sure "trama" does not contain more than 1024 bytes?
/* Crear la trama de datos */ trama = tamano1 + "|@|" + tamano2 + "|@|" + tamano3 + "|@|" + nombreEjecutable1.toLatin1() + "|@|" + nombreEjecutable2.toLatin1(); /* Escribir la trama en los últimos 1024 bytes del archivo de salida */ archivoSalida.write(trama,1024);
Why don't you use trama.size() instead of 1024?
-
@jsulm said in Bind different files program Qt:
Escribir la trama en los últimos 1024 bytes del archivo de salida
It's like a copy i'm copying file taking blocks of 1024 bytes to get size of trama. And the other is i want to put some icon in archivoSalida without change the resources of my other files. Or protect resources of my stub or something like that. I dont know if it is possible.
If you still dont understand me, what i'm trying, i'm trying this:
https://www.youtube.com/watch?v=sWIQIi4lg58I want to put resources to my executable files. In my case i want to put some resource like (icon) to my file archivoSalida. So i will add in my binder one textview and one pushbutton i will include this icon and will be added like resource to my executable file ArchivoSalida.
-
archivoSalida.write(trama,1024);
This will only write <=1024 bytes even if trampa contains more than 1024 bytes.
This could be the reason why the content of the file is broken.
Again: why don't you just write:archivoSalida.write(trama);
?
See: http://doc.qt.io/qt-5/qiodevice.html#write
If you going to change files often then I would not put them in one file - as you see it is much work to change something. -
Yeah but i can change the resources of one executable with Qt in one only executable without touch my other's. And it's possible like https://www.youtube.com/watch?v=sWIQIi4lg58
But i dont know how i can do this with only using one textview and pushbutton. And then include like resource and then read this resource in my executable archivosalida. -
Or if it is difficult maybe i can set the filename of archivosalida with icon included and then i will get the file with the resources included.
-
ok i change that:
QByteArray trama(1024,'0'); -> QByteArray trama; archivoSalida.write(trama,1024); -> archivoSalida.write(trama):
If i change this:
QByteArray trama(1024,'0'); -> QByteArray trama;I get this error:
the strange thing i can join files of 100 MB or without any limit using one trama of 1024 bytes¿? LOL
-
@RIVOPICO Try
archivoSalida.write(trama, trama.size());
-
@jsulm it's strange but when i bind files in linux works and when i compile for windows bind files but not seem's to do the functions that i use.
-
@jsulm But how i can put the size of my trama in my stub if i dont know the size??? Because i can change in my binder but i must to put the size of my trama to read the files.
-
@RIVOPICO I don't understand the problem. If you put something into QByteArray then that array knows its size and you can get it calling size() method as shown before.
-
@jsulm yeah but if you check in the stub you will see these lines:
archivoStub->seek(archivoStub->size() - 1024); trama = archivoStub->read(1024);
I'm extracting my trama and then i read it. I can put trama size in my binder. But in my stub how i can read this trama? Because if i put 1024 i know the size and i can extract this trama and then read it. But if i dont know my trama how my stub can read my trama? Because if i dont know where is the trama will show me list outrange. So in other words if i want to read my trama i must to know this size of my trama or include something in my stub to get the size of this trama.
Yeah i know but im trying to say that i seek my trama, i extract my trama because i know the size in my case i only put with 1024 bytes but i dont know if the size is more big (undefined)how my stub can know this size of trama? so i think my stub can't know the size of my trama.
-
This post is deleted! -
@RIVOPICO Well, if the size of what you want to read is variable then you need to specify a format. For example you can first put an integer into your file specifying the size and then trama of that size, then again size and trama and so on:
SIZE1TRAMA1SIZE2TRAMA2
Then you know how to interpret what you're reading.
For such stuff it is better to use QDataStream instead of QFile directly.QFile file("file.dat"); file.open(QIODevice::ReadOnly); QDataStream in(&file); int size; // Do the following in a loop until finished reading in >> size; char buffer[BUFFER_SIZE]; in.readRawData(buffer, size);
-
@jsulm yeah this i was trying. because if i put the size and then i can read this size, i can read my trama too. But i have some doubts, this code is for reading or for including the size of my data? sorry for all!
-
@RIVOPICO Reading
-
@jsulm i was thinking to include other trama with the size of my real trama and then get the size of the real trama because my other trama has defined size. Other way will be with a format but i dont know if i can seek one file with format or how i can read this file included in my all files. Anyways i think int number doesn't take many space to be a problem, i think.
-
@RIVOPICO Is there a reason why you keep deleting your postings? Makes it pretty hard to follow the thread.