Sample code for 30+ languages & platforms
SQL Server

Extract a .tar.gz Archive to a Directory

See more Gzip Examples

This example demonstrates how to use the UnTarGz method to extract a .tar.gz archive to a specified directory.

The method performs both the Gzip decompression and TAR extraction in a single step. It operates in streaming mode, meaning it does not create temporary files and uses a constant, minimal amount of memory regardless of archive size.

The bNoAbsolute parameter is set to _TRUE_ in this example to prevent extraction of files with absolute paths. This is a safety measure that helps avoid overwriting files in unintended locations (such as system directories).

This method is ideal for efficiently extracting large .tar.gz archives while maintaining security and low memory usage.

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

    -- This example demonstrates how to extract a .tar.gz archive to a directory.

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

    -- The .tar.gz file to be extracted:
    DECLARE @tgzPath nvarchar(4000)
    SELECT @tgzPath = 'c:/temp/archive.tar.gz'

    -- The destination directory:
    DECLARE @destDir nvarchar(4000)
    SELECT @destDir = 'c:/temp/outputDir'

    -- Set to 1 to prevent extraction of files with absolute paths:
    DECLARE @noAbsolute int
    SELECT @noAbsolute = 1

    -- Extract the archive:
    EXEC sp_OAMethod @gzip, 'UnTarGz', @success OUT, @tgzPath, @destDir, @noAbsolute
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @gzip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @gzip
        RETURN
      END


    PRINT 'Archive successfully extracted to: ' + @destDir

    EXEC @hr = sp_OADestroy @gzip


END
GO