Sample code for 30+ languages & platforms
SQL Server

ZIP a Visual Studio Project and Exclude Build Artifacts

See more Zip Examples

This example demonstrates how to create a ZIP archive of a Visual Studio project directory while excluding files and directories commonly created during builds.

This is useful when archiving or sharing source code without including compiled binaries, intermediate build output, debug symbols, cache files, or generated package folders.

The example excludes common Visual Studio build artifacts such as:

  • bin directories
  • obj directories
  • .vs directories
  • *.exe, *.dll, *.pdb, and related output files
  • Temporary and cache files

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

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

    -- Create a new, empty Zip object.
    -- The .zip file is not created until WriteZip or WriteZipAndClose is called.
    EXEC sp_OAMethod @zip, 'NewZip', @success OUT, 'VisualStudioProject.zip'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        RETURN
      END

    -- When the ZIP is extracted, all files will be placed beneath
    -- the "MyProject/" directory.
    -- 
    -- For example:
    -- 
    --     src/main.cpp
    -- 
    -- Will be stored in the ZIP as:
    -- 
    --     MyProject/src/main.cpp
    -- 
    EXEC sp_OASetProperty @zip, 'PathPrefix', 'MyProject/'

    -- We will add files from this Visual Studio project directory:
    -- 
    --     c:/projects/MyProject
    -- 
    -- The goal is to ZIP the source project without the files generated
    -- by Visual Studio builds.

    -- ------------------------------------------------------------
    -- Exclude directories commonly created by Visual Studio builds.
    -- 
    -- Any directory having one of these names will be skipped entirely,
    -- including all files and subdirectories beneath it.
    EXEC sp_OAMethod @zip, 'ExcludeDir', NULL, 'bin'
    EXEC sp_OAMethod @zip, 'ExcludeDir', NULL, 'obj'
    EXEC sp_OAMethod @zip, 'ExcludeDir', NULL, '.vs'
    EXEC sp_OAMethod @zip, 'ExcludeDir', NULL, 'packages'

    -- ------------------------------------------------------------
    -- Create a StringArray containing wildcard exclusion patterns
    -- for generated files and temporary artifacts.
    DECLARE @saExcludes int
    EXEC @hr = sp_OACreate 'Chilkat.StringArray', @saExcludes OUT

    -- Exclude common compiled output files.
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.exe'
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.dll'
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.pdb'
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.ilk'
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.lib'
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.obj'

    -- Exclude NuGet package files.
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.nupkg'

    -- Exclude temporary, cache, and user-specific files.
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.cache'
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.tmp'
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.user'
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.suo'
    EXEC sp_OAMethod @saExcludes, 'Append', @success OUT, '*.log'

    -- Apply the wildcard exclusion patterns to the Zip object.
    EXEC sp_OAMethod @zip, 'SetExclusions', NULL, @saExcludes

    -- ------------------------------------------------------------
    -- Recursively add references to the project files.
    -- 
    -- AppendFiles does not immediately read or compress the files.
    -- It adds file references to the Zip object. The referenced files
    -- are read and compressed later when WriteZipAndClose is called.
    DECLARE @recurse int
    SELECT @recurse = 1

    EXEC sp_OAMethod @zip, 'AppendFiles', @success OUT, 'c:/projects/MyProject/*', @recurse
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @zip, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @zip
        EXEC @hr = sp_OADestroy @saExcludes
        RETURN
      END

    -- ------------------------------------------------------------
    -- Write the ZIP archive and close it.
    -- 
    -- This is where the referenced source files are actually read,
    -- compressed as needed, and written to the final .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
        EXEC @hr = sp_OADestroy @saExcludes
        RETURN
      END


    PRINT 'Visual Studio project ZIP created successfully.'

    EXEC @hr = sp_OADestroy @zip
    EXEC @hr = sp_OADestroy @saExcludes


END
GO