Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
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)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Chilkat Zip API Concepts

See more Zip Examples

This example demonstrates several core concepts of the Chilkat.Zip class, including how ZIP entries are added, managed, and transformed as a ZIP archive progresses from an in-memory staging object to an actual written .zip file.

The example shows how to:

  • Initialize a new ZIP object using NewZip
  • Add filesystem file references using AddFile
  • Add in-memory text data using AddString
  • Modify the stored ZIP filename independently of the source filesystem filename
  • Examine ZIP entries before and after writing the ZIP archive
  • Understand how ZIP entry types change during the ZIP lifecycle

The example also demonstrates the meaning of the different ZipEntry.EntryType values:

  • 1 — File Entry: A reference to a filesystem file that has not yet been processed
  • 2 — Data Entry: An in-memory entry containing text or binary data
  • 0 — Mapped Entry: An entry already existing within the currently open ZIP archive

This example is especially useful for understanding that methods such as AddFile and AppendFiles initially add only references to filesystem files, and that the actual file reading and compression occur later when WriteZip or WriteZipAndClose is called.

Note: This example requires Chilkat v11.0.0 or greater.

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @zip int
    EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- ------------------------------------------------------------
    -- This example demonstrates some of the fundamental concepts
    -- of the Chilkat Zip class.
    -- 
    -- We will:
    -- 
    --   1) Create a new ZIP object
    --   2) Add a filesystem file using AddFile
    --   3) Add an in-memory text entry using AddString
    --   4) Examine the ZIP entries before writing the ZIP
    --   5) Write the ZIP archive
    --   6) Examine how the entry types change after writing
    -- 
    -- The final ZIP archive will contain:
    -- 
    --     helloWorld.txt
    --     HelloWorld2.txt
    -- 

    -- ------------------------------------------------------------
    -- Initialize the Zip object.
    -- 
    -- NewZip resets the Zip object to a new and empty state.
    -- It does NOT immediately create the .zip file.
    -- 
    -- The filename passed to NewZip becomes the default filename
    -- used later when WriteZip or WriteZipAndClose is called.
    EXEC sp_OAMethod @zip, 'NewZip', @success OUT, 'test.zip'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END


    EXEC sp_OAGetProperty @zip, 'FileName', @sTmp0 OUT
    PRINT 'ZIP filename = ' + @sTmp0

    PRINT ''

    -- ------------------------------------------------------------
    -- Add a file from the local filesystem.
    -- 
    -- AddFile does NOT immediately read or compress the file.
    -- Instead, it adds a reference to the filesystem file.
    -- 
    -- The actual file data will be read later when WriteZip
    -- or WriteZipAndClose is called.
    -- 
    -- Note:
    -- On Windows, forward slashes are equivalent to backslashes.
    DECLARE @saveExtraPath int
    SELECT @saveExtraPath = 0
    EXEC sp_OAMethod @zip, 'AddFile', @success OUT, '/temp/abc123/HelloWorld123.txt', @saveExtraPath

    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- The ZIP object now contains one entry that references
    -- the local filesystem file:
    -- 
    --     /temp/abc123/HelloWorld123.txt
    -- 

    -- ------------------------------------------------------------
    -- Change the filename that will be stored in the ZIP archive.
    -- 
    -- The source filesystem file remains:
    -- 
    --     /temp/abc123/HelloWorld123.txt
    -- 
    -- But the ZIP entry will be written as:
    -- 
    --     helloWorld.txt
    -- 
    DECLARE @entry int
    EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT

    EXEC sp_OAMethod @zip, 'EntryAt', @success OUT, 0, @entry
    EXEC sp_OASetProperty @entry, 'FileName', 'helloWorld.txt'

    -- ------------------------------------------------------------
    -- Add another ZIP entry directly from in-memory text data.
    -- 
    -- This entry does not reference a filesystem file.
    -- The text data already exists in memory.
    EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'HelloWorld2.txt', 'hello world!', 'utf-8'

    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @entry
        RETURN
      END

    -- ------------------------------------------------------------
    -- Examine the ZIP entries before writing the ZIP archive.
    -- 
    -- ZIP entries can have different entry types:
    -- 
    -- 0 -- Mapped Entry
    --      An entry already existing in an open ZIP archive.
    -- 
    -- 1 -- File Entry
    --      A reference to a filesystem file that has not yet
    --      been read or compressed.
    -- 
    -- 2 -- Data Entry
    --      An in-memory entry containing text or binary data.
    -- 
    -- 3 -- Null Entry
    --      An entry that no longer exists.
    -- 
    -- 4 -- New Directory Entry
    --      A directory entry added by AddEmpty.
    -- 
    -- At this point:
    -- 
    --   helloWorld.txt    => type 1
    --   HelloWorld2.txt   => type 2
    -- 

    PRINT 'Entries BEFORE writing the ZIP:'

    PRINT ''

    DECLARE @i int

    DECLARE @n int
    EXEC sp_OAGetProperty @zip, 'NumEntries', @n OUT

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN
        EXEC sp_OAMethod @zip, 'EntryAt', @success OUT, @i, @entry

        EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT

        EXEC sp_OAGetProperty @entry, 'EntryType', @iTmp0 OUT
        PRINT '  ' + @sTmp0 + ', type=' + @iTmp0
        SELECT @i = @i + 1
      END


    PRINT ''

    -- ------------------------------------------------------------
    -- Write the ZIP archive.
    -- 
    -- During this call:
    -- 
    --   * Filesystem file references are read
    --   * Data is compressed as needed
    --   * The .zip file is created and written
    -- 
    EXEC sp_OAMethod @zip, 'WriteZip', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @entry
        RETURN
      END

    -- ------------------------------------------------------------
    -- Examine the ZIP entries again AFTER writing.
    -- 
    -- Because we called WriteZip (instead of WriteZipAndClose),
    -- the ZIP archive remains open.
    -- 
    -- The entries are automatically converted to
    -- "mapped entries" (type 0), meaning they now point to
    -- entries within the currently open ZIP archive.
    -- 
    -- At this point:
    -- 
    --   helloWorld.txt    => type 0
    --   HelloWorld2.txt   => type 0
    -- 

    PRINT 'Entries AFTER writing the ZIP:'

    PRINT ''

    EXEC sp_OAGetProperty @zip, 'NumEntries', @n OUT

    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN
        EXEC sp_OAMethod @zip, 'EntryAt', @success OUT, @i, @entry

        EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT

        EXEC sp_OAGetProperty @entry, 'EntryType', @iTmp0 OUT
        PRINT '  ' + @sTmp0 + ', type=' + @iTmp0
        SELECT @i = @i + 1
      END


    PRINT ''

    -- ------------------------------------------------------------
    -- Close the ZIP archive and clear the Zip object.
    EXEC sp_OAMethod @zip, 'CloseZip', NULL


    PRINT 'Done.'

    EXEC @hr = sp_OADestroy @zip
    EXEC @hr = sp_OADestroy @entry


END
GO

 

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