QuaZip does not unpack zip compressed folders
-
Hello!
I manage to unzip zip compressed binaries. But not zip-compressed folders with files.Example of error message
"C:\\Users\\posktomten\\PROGRAMMING\\TEST\\quazip_test\\quazip_testprg\\bin\\tls\\qopensslbackend.dll" QIODevice::write (QFile, "C:\Users\posktomten\PROGRAMMERING\TEST\quazip_test\quazip_testprg\bin\tls\qopensslbackend.dll"): device not open
All files seem to be found. But "device not open"
Appreciate if someone with more knowledge than I have can give me a clue!The code:
connect(ui->pbUnpack, &QPushButton::clicked, []() { QString pack = "C:/Users/posktomten/PROGRAMMERING/TEST/quazip_test/quazip_testprg/bin.zip"; QString till = "C:/Users/posktomten/PROGRAMMERING/TEST/quazip_test/quazip_testprg/"; QuaZip zip(pack); if(zip.open(QuaZip::mdUnzip)) { qDebug() << "OPEN"; for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) { // set source file in archive QString filePath = zip.getCurrentFileName(); // qDebug() << QDir::toNativeSeparators(till + filePath); 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.readAll(); //qDebug() << "ba " << ba; // Doesn't work with QByteArray, I've tried! // Reads all files // close the source file zFile.close(); // set destination file QFile dstFile(till + filePath); //qDebug() << till + filePath; // All files are found // open the destination file dstFile.open(QIODevice::WriteOnly); dstFile.write(ba.data()); //close the destination file dstFile.close(); } } else { qDebug() << "NOT OPEN"; } zip.close(); });
-
Hello!
I manage to unzip zip compressed binaries. But not zip-compressed folders with files.Example of error message
"C:\\Users\\posktomten\\PROGRAMMING\\TEST\\quazip_test\\quazip_testprg\\bin\\tls\\qopensslbackend.dll" QIODevice::write (QFile, "C:\Users\posktomten\PROGRAMMERING\TEST\quazip_test\quazip_testprg\bin\tls\qopensslbackend.dll"): device not open
All files seem to be found. But "device not open"
Appreciate if someone with more knowledge than I have can give me a clue!The code:
connect(ui->pbUnpack, &QPushButton::clicked, []() { QString pack = "C:/Users/posktomten/PROGRAMMERING/TEST/quazip_test/quazip_testprg/bin.zip"; QString till = "C:/Users/posktomten/PROGRAMMERING/TEST/quazip_test/quazip_testprg/"; QuaZip zip(pack); if(zip.open(QuaZip::mdUnzip)) { qDebug() << "OPEN"; for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) { // set source file in archive QString filePath = zip.getCurrentFileName(); // qDebug() << QDir::toNativeSeparators(till + filePath); 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.readAll(); //qDebug() << "ba " << ba; // Doesn't work with QByteArray, I've tried! // Reads all files // close the source file zFile.close(); // set destination file QFile dstFile(till + filePath); //qDebug() << till + filePath; // All files are found // open the destination file dstFile.open(QIODevice::WriteOnly); dstFile.write(ba.data()); //close the destination file dstFile.close(); } } else { qDebug() << "NOT OPEN"; } zip.close(); });
@posktomten Hi. Some thoughts:
Your code does not check that the destination file is successfully opened before trying to write (and write() is the source of the error message).
The destination location may not exist, i.e ....quazip_test/quazip_testprg/
.
If it exists, the destination location may not be writeable. This seems unlikely given the path in your code.
QFile will not automatically create intermediate directories. You may need to check for these (based on the path to the zip file entry and the base extract path) and create them as needed. -
@ChrisW67 Thanks! I should have realized that!
Now it works perfectly!connect(ui->pbUnpack, &QPushButton::clicked, []() { QString pack = "C:/Users/posktomten/PROGRAMMERING/various/QuaZip/quazip_test/quazip_testprg/bin.zip"; QString till = "C:/Users/posktomten/PROGRAMMERING/various/QuaZip/quazip_test/quazip_testprg/"; QuaZip zip(pack); if(zip.open(QuaZip::mdUnzip)) { qDebug() << "OPEN"; QDir dir; for(bool more = zip.goToFirstFile(); more; more = 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.readAll(); // Create folders recursively dir.mkdir(till + filePath); // close the source file zFile.close(); // set destination file QFile dstFile(till + filePath); // open the destination file dstFile.open(QIODevice::WriteOnly); // Write data from QByteArray to file dstFile.write(ba.data()); //close the destination file dstFile.close(); } } else { qDebug() << "NOT OPEN"; } zip.close(); });
-
P posktomten has marked this topic as solved on
-
@ChrisW67
I was way too fast. But now I dare to say that it works. But it is faster to unzip with QProcess and 7za.execonnect(ui->pbUnpack, &QPushButton::clicked, [this]() { QString pack = "C:/Users/posktomten/PROGRAMMERING/various/QuaZip/quazip_test/quazip_testprg/svtplay-dl-4.28.1.zip"; // QString till = "C:/Users/posktomten/PROGRAMMERING/various/QuaZip/quazip_test/quazip_testprg/"; QuaZip zip(pack); if(zip.open(QuaZip::mdUnzip)) { qDebug() << "OPEN"; QDir dir; for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) { // set source file in archive QString filePath = zip.getCurrentFileName(); QFileInfo fi(filePath); // The path to the folder QString dirpath = fi.path(); // The folder is created recursively dir.mkdir(dirpath); 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.readAll(); // close the source file zFile.close(); QFile dstFile(filePath); // open the destination file dstFile.open(QIODevice::WriteOnly); // Write data from QByteArray to file dstFile.write(ba.data(), ba.size()); //close the destination file dstFile.close(); } } else { qDebug() << "NOT OPEN"; } zip.close(); qDebug() << "DONE"; });
-
Error checking read/write
connect(ui->pbUnpack, &QPushButton::clicked, [this]() { QString ziped = "../svtplay-dl-4.28.1.zip"; QString unziped = ".."; QuaZip zip(ziped); // Open the zip file for reading if(zip.open(QuaZip::mdUnzip)) { // qDebug() << "Zip file opened successfully."; QDir dir; for(bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) { QString filePath = zip.getCurrentFileName(); QFileInfo fi(filePath); QString dirpath = fi.path(); dir.mkpath(unziped + "/" + dirpath); QuaZipFile zFile(zip.getZipName(), filePath); // Open the source file in the zip file for reading if(zFile.open(QIODevice::ReadOnly)) { // qDebug() << "Opened file in zip for reading: " << filePath; QByteArray ba = zFile.readAll(); zFile.close(); QFile dstFile(unziped + "/" + filePath); // Open the destination for writing QFileInfo fi(dstFile); // If it is a directory, an error message will appear if(fi.isDir()) { continue; } if(dstFile.open(QIODevice::WriteOnly)) { if(dstFile.isOpen()) { dstFile.write(ba.data(), ba.size()); dstFile.close(); // qDebug() << "Write successful."; } else { qDebug() << "Destination file is not open for writing."; } } else { qDebug() << "Could not open destination file (dstFile) for writing: " << dstFile.errorString(); } } else { qDebug() << "Could not open file in zip for reading: " << filePath; } } zip.close(); qDebug() << "Done."; } else { qDebug() << "Could not open zip file: " << ziped; } });