Sample code for 30+ languages & platforms
C

Zip Append StringBuilder

See more Zip Examples

Append the contents of a Chilkat StringBuilder object to a .zip.

Chilkat C Downloads

C
#include <C_CkZip.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkZip zip;
    const char *zipPath;
    HCkStringBuilder sb;
    int i;
    HCkZip zip2;
    const char *zipPath2;

    success = FALSE;

    //  This example requires the Chilkat API to have been previously unlocked.
    //  See Global Unlock Sample for sample code.

    zip = CkZip_Create();
    zipPath = "c:/temp/qa_output/out.zip";

    CkZip_NewZip(zip,zipPath);

    sb = CkStringBuilder_Create();
    i = 0;
    while (i < 100) {
        CkStringBuilder_AppendLine(sb,"This is a test",TRUE);
        i = i + 1;
    }

    CkZip_AddSb(zip,"this_is_a_test.txt",sb,"utf-8");

    success = CkZip_WriteZipAndClose(zip);
    if (success == FALSE) {
        printf("%s\n",CkZip_lastErrorText(zip));
        CkZip_Dispose(zip);
        CkStringBuilder_Dispose(sb);
        return;
    }

    printf("Success 1.\n");

    //  Perhaps you want to add a file to an existing .zip
    zip2 = CkZip_Create();
    //  Open the .zip we just wrote..
    success = CkZip_OpenZip(zip2,zipPath);

    CkStringBuilder_Clear(sb);
    i = 0;
    while (i < 100) {
        CkStringBuilder_AppendLine(sb,"This is a test 2",TRUE);
        i = i + 1;
    }

    CkZip_AddSb(zip2,"this_is_a_test_2.txt",sb,"utf-8");

    zipPath2 = "c:/temp/qa_output/out2.zip";
    CkZip_putFileName(zip2,zipPath2);
    success = CkZip_WriteZipAndClose(zip2);
    if (success == FALSE) {
        printf("%s\n",CkZip_lastErrorText(zip2));
        CkZip_Dispose(zip);
        CkStringBuilder_Dispose(sb);
        CkZip_Dispose(zip2);
        return;
    }

    printf("Success 2.\n");


    CkZip_Dispose(zip);
    CkStringBuilder_Dispose(sb);
    CkZip_Dispose(zip2);

    }