Unicode C
Unicode C
Add Text Files to a ZIP Using AddString
See more Zip Examples
This example demonstrates how to use the AddString method to add multiple text files directly from string variables into a ZIP archive.
Each string is converted to bytes using the specified character encoding and stored as a separate file entry within the ZIP archive.
This method is useful for dynamically generating small text files such as configuration files, reports, JSON documents, XML, or log files entirely in memory.
Chilkat Unicode C Downloads
#include <C_CkZipW.h>
void ChilkatSample(void)
{
BOOL success;
HCkZipW zip;
const wchar_t *readmeText;
const wchar_t *jsonText;
const wchar_t *xmlText;
success = FALSE;
zip = CkZipW_Create();
success = CkZipW_NewZip(zip,L"stringEntries.zip");
if (success == FALSE) {
wprintf(L"%s\n",CkZipW_lastErrorText(zip));
CkZipW_Dispose(zip);
return;
}
// Add a README text file.
readmeText = L"This ZIP archive was created using AddString.";
success = CkZipW_AddString(zip,L"docs/readme.txt",readmeText,L"utf-8");
if (success == FALSE) {
wprintf(L"%s\n",CkZipW_lastErrorText(zip));
CkZipW_Dispose(zip);
return;
}
// Add a JSON configuration file.
jsonText = L"{ \"server\": \"example.com\", \"port\": 443 }";
success = CkZipW_AddString(zip,L"config/settings.json",jsonText,L"utf-8");
if (success == FALSE) {
wprintf(L"%s\n",CkZipW_lastErrorText(zip));
CkZipW_Dispose(zip);
return;
}
// Add a small XML document.
xmlText = L"<root><status>OK</status></root>";
success = CkZipW_AddString(zip,L"xml/status.xml",xmlText,L"utf-8");
if (success == FALSE) {
wprintf(L"%s\n",CkZipW_lastErrorText(zip));
CkZipW_Dispose(zip);
return;
}
// Write the ZIP archive to disk and close it.
success = CkZipW_WriteZipAndClose(zip);
if (success == FALSE) {
wprintf(L"%s\n",CkZipW_lastErrorText(zip));
CkZipW_Dispose(zip);
return;
}
wprintf(L"ZIP archive created successfully.\n");
CkZipW_Dispose(zip);
}