PDA

View Full Version : Can't open password protected zip file using QuaZip



Rajesh.Rathod
11th September 2016, 14:43
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.


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());
QString file_password("1234");

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);

QByteArray file_data = file.readAll();
QString file_str(file_data);
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
dstFile.open( QIODevice::WriteOnly | QIODevice::Text );
// 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,

d_stranz
11th September 2016, 18:41
Are you using the correct codec? From the QuaZip class description page (http://quazip.sourceforge.net/classQuaZip.html) on SourgeForge:


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.

Rajesh.Rathod
12th September 2016, 17:21
I have modified my function after your suggestion .. here is the updated code...


bool CFileManager::UnZipFile(QString zipFilePath, QString unzipFilePath, QString filePassword)
{
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.");
QByteArray file_data = file.readAll();
QString file_str(file_data);
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.

d_stranz
12th September 2016, 18:01
"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?

Rajesh.Rathod
12th September 2016, 20:10
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,

d_stranz
13th September 2016, 00:02
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.

Rajesh.Rathod
13th September 2016, 10:15
Can someone verify here if zlib supports unzipping of password protected files.

I found couple website which says like so.

Thanks,

Rajesh.Rathod
14th September 2016, 05:16
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,

mrboese
11th October 2016, 14:55
I had the same problem. The solution was simple: Don't pass a QString as password. Pass something like
password.toLatin1().data(). Otherwise it's using another open() function. It should be using
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.

Rajesh.Rathod
14th October 2016, 13:09
Thanks for your input, I will try your suggested solution.