ifdef Q_OS_WIN // Windows
define UUU(x) (LPCWSTR)x.unicode()
define SSS(x) QString((QChar*)x)
else // Mac and Linux
define UUU(x) (wchar_t*)x.toStdWString().c_str()
define SSS(x) QString::fromWCharArray(x)
endif
DOCUMENT TO QString::toStdWString() http://doc.qt.io/qt-5/qstring.html#toStdWString
MyTool.h: CkZipW* m_zip;
bool MyTool::Mytool { m_zip = new CkZipW(); bool success = m_zip->UnlockComponent(ZIP_CODE); }
bool MyTool::ZipAES(QString strSrcFile, QString strPasswd) { const QFileInfo fi = QFileInfo(strSrcFile); QString strFileName = fi.fileName()+".zip"; QString strDestFile = "c:/dev/"+strFileName;
bool success1 = m_zip->NewZip(UUU(strDestFile));
m_zip->put_Encryption(4);
m_zip->put_EncryptKeyLength(128);
m_zip->put_EncryptPassword(UUU(strPasswd));
bool success2 = m_zip->AppendOneFileOrDir(UUU(strSrcFile), false);
if (success2 != true) {
qDebug() << strSrcFile;
}
bool success3 = m_zip->WriteZipAndClose();
return success1 && success2 && success3;
}
I Use MyTool tool; tool.ZipAES("c:/test.txt", "00000");
QString is always Unicode with 16 bit in all platforms. But Chilkat always use wchar_t which has different bits. So I have to transform QString to wchar_t.
The above code works well in windows. And it works well also in Mac (Zip and UnZip). But in Mac, the real password is not 00000 (I copy the zip file to windows and try to use winrar to decompress it, but it fail), it is other something. Windows has not this problem. Could you tell me what's wrong with my code above and what is the real password final if possible. Thanks!
I use C++/Mingw/Clang, CkZipW 9.5.0.52, Qt5.6.