How to Zip ( Compress) Multiple Folders and Delete in SSIS Package - SQL Server Integration Services(SSIS) Tutorial

Scenario: 

We have a lot of folders in one of our Source Folder, we want to zip all of them to Archive folder. Each Folder will be zipped to its own file. Once the folders are zipped we want to delete them from Source Folder.

Solution:

To perform this requirement, we will be using Script task. Here are the things that you will learn in this video.


  1. How to create SSIS Package Parameters for SourceFolder and ArchFolder paths
  2. How to Map the Package Parameters to Script Task in SSIS Package
  3. How to change .NET Framework from 4. to 4.5 version in Script Task in SSIS Package
  4. How to Add reference to assemblies such as System.IO.Compression and System.IO.Compression.FileSystem
  5. Loop through folder names one by one by using Foreach Loop inside Script Task and Zip them and delete them.


Script used in the Script task to Zip Multiple Folders in SSIS Package

public void Main()
        {
            //Assign values to local variable from Package Parameters
            string inputfolder = Dts.Variables["$Package::SourceFolder"].Value.ToString();
            string archfolder = Dts.Variables["$Package::ArchFolder"].Value.ToString();
        

           //Loop through folders and zip them and delete them from Source folder
            foreach (string dir in Directory.GetDirectories(inputfolder))
            {
                //MessageBox.Show(s);
              //get only folder name form full path
                string foldername = dir.Replace(inputfolder, "");
               //Zip the input folder to file
                ZipFile.CreateFromDirectory(dir, archfolder+foldername+".zip");
               //Delete the folder
                Directory.Delete(dir,true);
              
            }

            Dts.TaskResult = (int)ScriptResults.Success;
        }




 Zip Multiple Folders to Separate Zip Files and then Delete from Source Directory in SSIS Package


2 comments: