Sample code for 30+ languages & platforms
SQL Server

Exclude Multiple Directories When Adding Files to a ZIP

See more Zip Examples

This example demonstrates how to use the ExcludeDir method to exclude specific directory names when recursively adding files to a ZIP archive.

The ExcludeDir method excludes directories by name. Any directory matching an excluded name is skipped entirely, including all files and subdirectories beneath it.

This is useful for excluding directories such as:

  • .git
  • node_modules
  • bin
  • obj
  • Temporary or cache directories

Call ExcludeDir once for each directory name to exclude.

Suppose the local filesystem contains:

c:/project/src/main.cpp
c:/project/src/util.cpp
c:/project/bin/app.exe
c:/project/obj/main.obj
c:/project/.git/config
c:/project/node_modules/library/index.js

After excluding bin, obj, .git, and node_modules, only the files beneath src are added to the ZIP archive.

The resulting ZIP archive contains:

project/src/main.cpp
project/src/util.cpp

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

    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

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

    -- Exclude several directory names.
    -- 
    -- Any directory matching one of these names will be skipped
    -- during recursive file appending.
    EXEC sp_OAMethod @zip, 'ExcludeDir', NULL, 'bin'
    EXEC sp_OAMethod @zip, 'ExcludeDir', NULL, 'obj'
    EXEC sp_OAMethod @zip, 'ExcludeDir', NULL, '.git'
    EXEC sp_OAMethod @zip, 'ExcludeDir', NULL, 'node_modules'

    -- Recursively append all files beneath c:/project.
    -- 
    -- Because saveExtraPath = 1, the ZIP paths will
    -- include "project/" as the leading directory.
    DECLARE @recurse int
    SELECT @recurse = 1
    DECLARE @saveExtraPath int
    SELECT @saveExtraPath = 1
    DECLARE @archiveOnly int
    SELECT @archiveOnly = 0
    DECLARE @includeHidden int
    SELECT @includeHidden = 1
    DECLARE @includeSystem int
    SELECT @includeSystem = 0

    EXEC sp_OAMethod @zip, 'AppendFilesEx', @success OUT, 'c:/project', @recurse, @saveExtraPath, @archiveOnly, @includeHidden, @includeSystem
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- Write the ZIP archive.
    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


    PRINT 'ZIP archive created successfully.'

    EXEC @hr = sp_OADestroy @zip


END
GO