Sample code for 30+ languages & platforms
SQL Server

Extract a Single ZIP Entry While Preserving Its Stored Path

See more Zip Examples

This example demonstrates how to use the ZipEntry.Extract method to extract a single entry from a ZIP archive.

The example first creates a ZIP archive containing a text file. It then re-opens the ZIP, finds the entry, and extracts it to a base directory.

The Extract method uses the relative path stored within the ZIP entry. Any required subdirectories are created automatically beneath the specified base directory.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- ------------------------------------------------------------
    -- First create a sample ZIP archive containing a small text file.

    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, 'c:/temp/sampleText.zip'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- Add a small text file from memory.
    EXEC sp_OAMethod @zip, 'AddString', @success OUT, 'myApp/docs/hello.txt', 'Hello from a ZIP entry!', 'utf-8'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- Write the ZIP archive to disk and close it.
    EXEC sp_OAMethod @zip, 'WriteZipAndClose', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- ------------------------------------------------------------
    -- Now open the ZIP archive we just created.

    DECLARE @zip2 int
    EXEC @hr = sp_OACreate 'Chilkat.Zip', @zip2 OUT

    EXEC sp_OAMethod @zip2, 'OpenZip', @success OUT, 'c:/temp/sampleText.zip'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @zip2
        RETURN
      END

    -- Find the entry by its stored ZIP path.
    DECLARE @entry int
    EXEC @hr = sp_OACreate 'Chilkat.ZipEntry', @entry OUT

    EXEC sp_OAMethod @zip2, 'EntryOf', @success OUT, 'myApp/docs/hello.txt', @entry

    IF @success = 0
      BEGIN

        PRINT 'Entry not found.'
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @zip2
        EXEC @hr = sp_OADestroy @entry
        RETURN
      END

    -- Extract this single entry to the base directory:
    -- 
    --     c:/temp/extracted
    -- 
    -- Because the entry's stored path is:
    -- 
    --     myApp/docs/hello.txt
    -- 
    -- the file will be extracted to:
    -- 
    --     qa_output/extracted/myApp/docs/hello.txt
    -- 
    -- The required subdirectories are created automatically.
    EXEC sp_OAMethod @entry, 'Extract', @success OUT, 'c:/temp/extracted'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @entry, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @zip2
        EXEC @hr = sp_OADestroy @entry
        RETURN
      END

    EXEC sp_OAMethod @zip2, 'CloseZip', NULL


    PRINT 'Entry extracted successfully.'

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


END
GO