I am trying to extract .DAT file using Quazip library in Qt 5.14. I integrated this quazip library into my project and try to extract files using it. .txt file is get extracted but .DAT file is not get extracted. .DAT files are created but it does not contain any data.
Here is my source code
bool fileHelper::extractAll( QString folderPath, QString filePath ) {
QuaZip zip(filePath);
zip.open(QuaZip::mdUnzip);
bool isSuccess = false;
for(bool f=zip.goToFirstFile(); f; f=zip.goToNextFile())
{
// set source file in archive
QString filePath = zip.getCurrentFileName();
QuaZipFile zFile( zip.getZipName(), filePath );
// open the source file
zFile.open( QIODevice::ReadOnly );
// create a bytes array and write the file data into it
//QByteArray ba = zFile.read()
QByteArray ba = zFile.readAll();
// close the source file
zFile.close();
// set destination file
//QFile dstFile( getfileStoreRootDir()+filePath );
QFile dstFile( folderPath+filePath );
qDebug() << "dstFile :" << dstFile;
// open the destination file
dstFile.open( QIODevice::WriteOnly | QIODevice::Text );
// write the data from the bytes array into the destination file
dstFile.write( ba.data() );
//close the destination file
dstFile.close();
//mark extraction sucess
isSuccess = true;
}
zip.close();
return isSuccess; }
Please tell me am I doing something wrong or any other extra flag or something is required for it.
Related
i'm coding in qt. i'm using this to create a csv file.
QtCSV::StringData strData;
QStringList strList0;
int a=10; int b=20;
QStringList strList;
strList << QString::number(a) << QString::number(b);
strData.addRow(strList);
QFile my_file(fileName);
if(my_file.open(QFile::WriteOnly ))
QtCSV::Writer::write(my_file, strData);
the file created and data is writed. the problem is this method of writing create this line of data:
""10","20""
the inner "" is excess. how can i write so that it remove? the other standard csv file does not have this and my file cannot open in some programs.
I'm looking for the error I made on this code, but I can not find any solution since hours..
This function should simpli save a file to a directory:
void MyClass::saveSettingsToFile(QString file_name)
{
QString path;
path = dir.append(file_name);
QFile my_file(path);
if (!my_file.open(QFile::WriteOnly))
{
qDebug() << "Could not open file for writing";
}
QTextStream out(& my_file);
out << "some text \n"
my_file.flush();
my_file.close();
path = "";
file_name ="";
}
Where dir is a QString containing the directory, file_name is gathered from a lineEdit field.
When I first call the function with, for example file_name = "aaaa.txt", I find this aaaa.txt in the specified directory. All right.
When then I call again the function with file_name = "bbbb.txt", I find in the specified directory this file: aaaa.txtbbbb.txt, instead of I
bbbb.txt
It seems to me a very s****d error, but I cannot find what!
EDITED: there was this mistake QTextStream out(& path); instead of QTextStream out(& my_file);
You are modifying dir variable with QString::append. Variable dir is obviously a class member of MyClass. Try this instead:
void MyClass::saveSettingsToFile(QString file_name)
{
QString path(dir);
path.append(file_name);
QFile my_file(path);
//...
}
The QString::append function modify the parameter value itself as you can see in the documentation: http://doc.qt.io/qt-5/qstring.html#append
Example:
QString x = "free";
QString y = "dom";
x.append(y);
// x == "freedom"
So, what happens is that it keeps appending the content to the dir variable, not only assigning the result to path.
I am trying to make a simple widget which contains a lineedit which shows the file name and a button to open a filedialog.
and now I want to check if the file-extension is valid, in this case, a image file ending with jpg, png or bmp. I solved this with QFileInfo and QList, this code is in my btn_clicked slot:
QString filename = QFileDialog::getOpenFileName(this, tr("Select an image File", "", tr("Image Files (*.bmp *.jpg *.png);; All Files(*)"));
QList<QString> ext_list;
ext_list<<"bmp"<<"jpg"<<"png";
QFileInfo fi(filename);
QString ext = fi.suffix();
if (ext_list.contains(ext)){
// lineedit->setText(filename);
}
else {
QMessageBox msgBox;
msgBox.critical(0, "Error", "You must select a valid image file");
it works, but is there a more simple/elegant way to achieve the goal? Thx for your help.
You might be interested by the setNameFilters function : http://doc.qt.io/qt-5/qfiledialog.html#setNameFilters
Update
If you want to filter images without naming each extensions, you should use QMimeDatabase. This will allow you to define your filter for the QFileDialog and then get a list of extensions to check.
For example with jpeg and png:
QStringList mimeTypeFilters;
mimeTypeFilters << "image/jpeg" << "image/png";
QFileDialog fd;
fd.setFileMode(QFileDialog::ExistingFile);
fd.setMimeTypeFilters(mimeTypeFilters);
fd.exec();
QString filename = fd.selectedFiles().count() == 1 ? fd.selectedFiles().at(0) : "";
QMimeDatabase mimedb;
if(!mimeTypeFilters.contains(mimedb.mimeTypeForFile(filename).name()))
{
// something is wrong here !
}
i wanna delete/remove a file from storage. the file is store in the "/shared/photos/". this is how i store the file
QByteArray* data; //some image data
QImage image;
image.loadFromData(*data);
QFile outFile("shared/photos/"+filename);
outFile.open(QIODevice::WriteOnly);
image.save(&outFile, "PNG");
and i can successfully view the image file with this code :
QString filepath;
QString workingDir = QDir::currentPath();
filepath = "file://" + workingDir + "/shared/photos/"+filename;
and it's viewed with no problem.
the QString "filepath" contains this string
"file:///accounts/1000/appdata/com.example.Project.testDev_le_Project4b5f4904/shared/photos/02.jpg"
And now i tried to delete/remove this file from storage.
this is how i tried :
QString thumbnailImage = filepath;
// basically it contains string like filepath
//"file:///accounts/1000/appdata/com.example.Project.testDev_le_Project4b5f4904/shared/photos/02.jpg"
QFile thumb(thumbnailImage);
bool ok = thumb.remove();
QString error = thumb.errorString();
if(ok){ qDebug() << "delete thumbnailImage success = " << ok; }
else{ qDebug() << "delete thumbnailImage failed !! "; }
and it's not working. the debug says "No such file or directory".
i also tried
QFile::remove(thumbnailImage);
and still not working.
i also tried :
QFile::remove("/shared/photos/"+filename);
but still not working.
i also tried changing the workdir from QDir::currentPath() to QDir::homepath() and still no success.
so please tell me what exactly i should put in the QFile::remove() parameter.
the reference https://developer.blackberry.com/native/reference/cascades/qfile.html#remove said the parameter is QString filename.
bool QFile::remove ( const QString & fileName )
what exactly i should insert the parameter?
please help me guys.
thanks.
Regards,
Yoga Try Utomo
The file path is wrong. It should not contain "file://". Furthermore, you have to open the file before removing it.
QFile thumb("shared/photos/" + filename);
thumb.open(QIODevice::ReadWrite);
thumb.remove();
thumb.close();
I want to move a directory. My selected directory contains many sub directories and files.
How can I implement the same using Qt?
QDir::rename does it in the most cases. Following example moves theDir incl content from source to dest:
QString original = "/home/test/source/theDir";
QString dest = "/home/test/temp";
QDir dir;
if( !dir.rename( original, dest ) ){
throw Exception( "move failed" );
}