Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Unicode C Examples

ASN.1
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)

Java KeyStore (JKS)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(Unicode C) Chilkat Zip API Concepts

This example helps clarify some common misconceptions w/ using the Chilkat Zip API.

Chilkat C/C++ Library Downloads

MS Visual C/C++

Linux/CentOS C/C++

Alpine Linux C/C++

MAC OS X C/C++

armhf/aarch64 C/C++

C++ Builder

iOS C/C++

Android C/C++

Win Mobile 5.0/Pocket PC 2003

Solaris C/C++

FreeBSD C/C++

OpenBSD C/C++

MinGW C/C++

#include <C_CkZipW.h>
#include <C_CkZipEntryW.h>

void ChilkatSample(void)
    {
    HCkZipW zip;
    BOOL success;
    BOOL saveExtraPath;
    HCkZipEntryW entry;
    int i;
    int n;

    zip = CkZipW_Create();

    //  Any string unlocks the component for the 1st 30-days.
    success = CkZipW_UnlockComponent(zip,L"Anything for 30-day trial");
    if (success != TRUE) {
        wprintf(L"%s\n",CkZipW_lastErrorText(zip));
        CkZipW_Dispose(zip);
        return;
    }

    //  To clarify some concepts, this example will create a new .zip containing 2 files.

    //  The NewZip method is called to initialize the zip object to a new and empty state.
    //  It does not actually create the .zip file.  It simply intializes the object.
    //  if the zip object did not contain anything previously, then this method could be skipped
    //  altogether.  It has the side-effect of setting the zip.FileName property.
    success = CkZipW_NewZip(zip,L"test.zip");
    if (success != TRUE) {
        wprintf(L"%s\n",CkZipW_lastErrorText(zip));
        CkZipW_Dispose(zip);
        return;
    }

    //  The FileName property should now contain "test.zip".  This is the name of the .zip
    //  that is written when WriteZip or WriteZipAndClose is called.
    wprintf(L"zip filename = %s\n",CkZipW_fileName(zip));

    //  When a file is "appended" to the zip object, we are really just appending a reference
    //  to the file in the filesystem.  A "zip entry" is added to the zip object and this entry
    //  can be one of several types.  It can point to a file.  It can contain uncompressed binary
    //  or text data, it can point to an entry in the already-opened zip (if we had called OpenZip
    //  instead), etc.

    //  Now.. add a reference to a single file by calling AppendOneFileOrDir
    //  Note: On Windows, forward slashes are equivalent to backslashes
    saveExtraPath = FALSE;
    success = CkZipW_AppendOneFileOrDir(zip,L"/temp/abc123/HelloWorld123.txt",saveExtraPath);
    if (success != TRUE) {
        wprintf(L"%s\n",CkZipW_lastErrorText(zip));
        CkZipW_Dispose(zip);
        return;
    }

    //  We now have a zip object with one entry, which points to the file /temp/abc123/HelloWorld123.txt
    //  If desired, we could change the filename of the zip entry so that when the zip is written,
    //  the file has a different name:
    entry = CkZipW_GetEntryByIndex(zip,0);
    CkZipEntryW_putFileName(entry,L"helloWorld.txt");
    CkZipEntryW_Dispose(entry);

    //  When the zip is actually written, the entry's data will stream from /temp/abc123/HelloWorld123.txt
    //  and be compressed into an entry within the .zip having the filename "helloWorld.txt"

    //  Now add another entry, this time from a string containing the file content:
    entry = CkZipW_AppendString2(zip,L"HelloWorld2.txt",L"hello world!",L"utf-8");
    CkZipEntryW_Dispose(entry);

    //  Examine the entries in the zip so far..
    //  Each entry can be one of the following types:

    //  0 -- Mapped Entry: An entry in an existing Zip file.
    //  1 -- File Entry: A file not yet in memory, but referenced.
    //       These entries are added by calling AppendFiles, AppendFilesEx, AppendOneFileOrDir, etc.
    //  2 -- Data Entry: An entry containing uncompressed data from memory.
    //       These entries are added by calling AppendData, AppendString, etc.
    //  3 -- Null Entry: An entry that no longer exists in the .zip.
    //  4 -- New Directory Entry: A directory entry added by calling AppendNewDir.

    n = CkZipW_getNumEntries(zip);
    for (i = 0; i <= n - 1; i++) {

        entry = CkZipW_GetEntryByIndex(zip,i);
        wprintf(L"%s, type=%d\n",CkZipEntryW_fileName(entry),CkZipEntryW_getEntryType(entry));
        CkZipEntryW_Dispose(entry);
    }

    //  Write the "test.zip" file.
    success = CkZipW_WriteZip(zip);
    if (success != TRUE) {
        wprintf(L"%s\n",CkZipW_lastErrorText(zip));
        CkZipW_Dispose(zip);
        return;
    }

    //  Our zip object still contains 2 entries, but now they point to the entries within the
    //  test.zip  (We called WriteZip, which writes the .zip but does not close it.  Instead,
    //  the entries of the zip object are updated to become "mapped" entries.
    //  For example:

    n = CkZipW_getNumEntries(zip);
    for (i = 0; i <= n - 1; i++) {

        entry = CkZipW_GetEntryByIndex(zip,i);
        wprintf(L"%s, type=%d\n",CkZipEntryW_fileName(entry),CkZipEntryW_getEntryType(entry));
        CkZipEntryW_Dispose(entry);
    }

    //  Finally, close the zip
    CkZipW_CloseZip(zip);


    CkZipW_Dispose(zip);

    }

 

© 2000-2016 Chilkat Software, Inc. All Rights Reserved.