Can't open password protected zip file using QuaZip
Dear All,
I would like to open password protected zip file using QuaZip but it is not working and I can see it says open mode not supported.
File is stored on my local computer.
I can unzip normal file.
Here is my code sample.
Code:
bool CURLScanner::UnZipFile()
{
QuaZip zip(TEST_ZIP_FILE);
//QuaZip zip(FILE_SAVE_PATH);
zip.open(QuaZip::mdUnzip);
QuaZipFile file(&zip);
//QString file_password(m_pURLDownloader->GetFileLastUpdatedTime());
for(bool f=zip.goToFirstFile(); f; f=zip.goToNextFile())
{
printf("\n File Found in zip folder : %s", file.getFileName().toUtf8().constData());
file.
open(QIODevice::ReadOnly,file_password
);
if(file_str.isEmpty())
{
printf("\n NO FILE CONTENT");
}
else
{
//do something with the data
printf("\n\n UNZIP FILE CONTENT : %s", file_str.toUtf8().constData());
// set destination file
QFile dstFile
( TEST_UNZIP_FILE
);
//QFile dstFile( FILE_SAVE_PATH_2 );
// open the destination file
// write the data from the bytes array into the destination file
dstFile.write( file_data.data() );
//close the destination file
dstFile.close();
}
file.close();
}
zip.close();
return true;
}
Please help me.
Thanks,
Re: Can't open password protected zip file using QuaZip
Are you using the correct codec? From the QuaZip class description page on SourgeForge:
Quote:
This class supports localized file names inside ZIP archive, but you have to set up proper codec with setCodec() function. By default, locale codec will be used, which is probably ok for UNIX systems, but will almost certainly fail with ZIP archives created in Windows. This is because Windows ZIP programs have strange habit of using DOS encoding for file names in ZIP archives.
Your code doesn't check to see if QuaZip:: open() succeeded. So any assumptions on whether the rest of the code works as expected are flawed.
Re: Can't open password protected zip file using QuaZip
I have modified my function after your suggestion .. here is the updated code...
Code:
{
printf("\nPROGRESS : UNZIP FILE REQUESTED ......\n");
bool bFileUnZipDone(false);
QFile filePath
(zipFilePath
);
if(filePath.exists())
{
printf("\nPROGRESS : UnZipFile - file path exists.");
QuaZip zip(zipFilePath);
zip.setFileNameCodec("WINDOWS-1251");
if(zip.open(QuaZip::mdUnzip))
{
printf("\nPROGRESS : UnZipFile - zip opened successfully.");
// Iterate over all the files found in zip folder.
QuaZipFile file(&zip);
for(bool fileIter = zip.goToFirstFile(); fileIter ; fileIter = zip.goToNextFile())
{
printf("\nPROGRESS : UnZipFile - inside file loop..");
//file.open(QIODevice::ReadOnly);
if(file.
open(QIODevice::ReadOnly,filePassword
))// -- THIS FUNCTION IS NOT WORKING AND PRINTS ERROR IN CONSOLE. {
printf("\nPROGRESS : UnZipFile - zip file opened successfully.");
if(file_str.isEmpty())
{
printf("\nPROGRESS : unzip file data found, saving to a fdisk file...");
bFileUnZipDone = SaveFileToDisk(unzipFilePath, file_data);
}
file.close();
}
}
zip.close();
}
else
{
printf("\n QuaZip open error : %d ",zip.getZipError());
}
}
return bFileUnZipDone;
}
Console shows "QuaZipFile::open(): open mode 1 not supported by this function" message when it reaches to QuaZipFile open function, marked with capital letter comment in above code.
I would like to unzip file on windows 8.1.
Please help,
Thanks.
Re: Can't open password protected zip file using QuaZip
Quote:
"QuaZipFile:pen(): open mode 1 not supported by this function"
Well, try another mode then. Maybe unzipping a passworded file requires write permission.
Can you unzip this file using the WinZIP program on your PC?
What happens if you make your own ZIP file with a password (in other words, zip something from your PC and put a password on it). Can your program read that?
Re: Can't open password protected zip file using QuaZip
I have already tried other mode and all says mode not supported.
I can create password protected zip on my computer and unzip it using winzip.
but programmatically I can't unzip file even if it was create on my computer.
Does anyone tried this programmatically.
It will be helpful if someone has some working example, please help.
Is there anyway that JlCompress can do this, I didn't find any.
Thanks,
Re: Can't open password protected zip file using QuaZip
Have you used the debugger to step into the QuaZip code to see where it is failing? The error code is not very informative - you need to find out what is happening inside to cause the failure.
Re: Can't open password protected zip file using QuaZip
Can someone verify here if zlib supports unzipping of password protected files.
I found couple website which says like so.
Thanks,
Re: Can't open password protected zip file using QuaZip
Update from my side is the password protected sample file which I couldn't open programmatically using QuaZip can be open by some other sample C++ code I found from some other source.
I have converted that code in static win32 lib and would like to use in my Qt project using QtCreator, work in progress.
Meanwhile some one really have successfully opened password protected zip file using QuaZip then please let me know.
Thanks,
Re: Can't open password protected zip file using QuaZip
I had the same problem. The solution was simple: Don't pass a QString as password. Pass something like
Code:
password.toLatin1().data()
. Otherwise it's using another open() function. It should be using
Code:
open(OpenMode mode, int *method, int *level, bool raw, const char *password =NULL)
(Open with password just calls this with openMod, 0,0,false,password.
Re: Can't open password protected zip file using QuaZip
Thanks for your input, I will try your suggested solution.