Sample code for 30+ languages & platforms
SQL Server

Replace/Update a FIle in a .zip

See more Zip Examples

Demonstrates how to replace/update a file from a .zip. Note: This requires the entire .zip to be rewritten.

Chilkat SQL Server Downloads

SQL Server
-- 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
    DECLARE @success int
    SELECT @success = 0

    -- This requires the Chilkat Zip API to have been previously unlocked.
    -- See Unlock Chilkat Zip for sample code.

    -- First prepare a .zip and write it..
    DECLARE @zip int
    EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @zip, 'NewZip', @success OUT, 'qa_output/abc.zip'

    -- Add some files..
    DECLARE @charset nvarchar(4000)
    SELECT @charset = 'utf-8'
    EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'a.txt', 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', @charset
    EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'b.txt', 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb', @charset
    EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'c.txt', 'cccccccccccccccccccccccccccccccccccc', @charset

    -- Write to qa_output/abc.zip
    -- This .zip contains three files: a.txt, b.txt, and c.txt
    EXEC sp_OAMethod @zip, 'WriteZipAndClose', @success OUT

    -- -------------------------------------------------------------------
    -- Open abc.zip, replace the content of the "b.txt" entry with something else, and re-write.
    DECLARE @zip2 int
    EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip2 OUT

    EXEC sp_OAMethod @zip2, 'OpenZip', @success OUT, 'qa_output/abc.zip'

    DECLARE @entry int
    EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT

    EXEC sp_OAMethod @zip2, 'EntryOf', @iTmp0 OUT, 'b.txt', @entry
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @entry, 'ReplaceString', @success OUT, 'This is the new content.  bbbbbbbbbbbbbbbbbbbbbb', 'utf-8'
      END

    -- Write the modified .zip back to "abc.zip"
    EXEC sp_OAMethod @zip2, 'WriteZipAndClose', @success OUT


    PRINT 'success.'

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


END
GO