Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

SQL Server Examples

Web API Categories

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
Bounced Email
Box
CAdES
CSR
CSV
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)
MHT / HTML Email
MIME
MS Storage Providers
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
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(SQL Server) Chilkat Zip API Concepts

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

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)
    -- This example requires the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @zip int
    -- Use "Chilkat_9_5_0.Zip" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    -- 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.
    DECLARE @success int
    EXEC sp_OAMethod @zip, 'NewZip', @success OUT, 'test.zip'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- The FileName property should now contain "test.zip".  This is the name of the .zip
    -- that is written when WriteZip or WriteZipAndClose is called.

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

    -- 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
    DECLARE @saveExtraPath int
    SELECT @saveExtraPath = 0
    EXEC sp_OAMethod @zip, 'AppendOneFileOrDir', @success OUT, '/temp/abc123/HelloWorld123.txt', @saveExtraPath
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- 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:
    DECLARE @entry int
    EXEC sp_OAMethod @zip, 'GetEntryByIndex', @entry OUT, 0
    EXEC sp_OASetProperty @entry, 'FileName', 'helloWorld.txt'
    EXEC @hr = sp_OADestroy @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:
    EXEC sp_OAMethod @zip, 'AppendString2', @entry OUT, 'HelloWorld2.txt', 'hello world!', 'utf-8'
    EXEC @hr = sp_OADestroy @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. 

    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, 'GetEntryByIndex', @entry OUT, @i
        EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT

        EXEC sp_OAGetProperty @entry, 'EntryType', @iTmp0 OUT
        PRINT @sTmp0 + ', type=' + @iTmp0
        EXEC @hr = sp_OADestroy @entry

        SELECT @i = @i + 1
      END

    -- Write the "test.zip" file.
    EXEC sp_OAMethod @zip, 'WriteZip', @success OUT
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- 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:

    EXEC sp_OAGetProperty @zip, 'NumEntries', @n OUT
    SELECT @i = 0
    WHILE @i <= @n - 1
      BEGIN

        EXEC sp_OAMethod @zip, 'GetEntryByIndex', @entry OUT, @i
        EXEC sp_OAGetProperty @entry, 'FileName', @sTmp0 OUT

        EXEC sp_OAGetProperty @entry, 'EntryType', @iTmp0 OUT
        PRINT @sTmp0 + ', type=' + @iTmp0
        EXEC @hr = sp_OADestroy @entry

        SELECT @i = @i + 1
      END

    -- Finally, close the zip
    EXEC sp_OAMethod @zip, 'CloseZip', NULL

    EXEC @hr = sp_OADestroy @zip


END
GO

 

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