Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zip a folder using SSIS

I am trying to zip a Folder in SSIS, there are 12 files in the source folder and I need to zipthat folder. I can get the files to zip fine my problem is the folders.

I have to use winzip to create the zipped packages.

Can anyone point me to a good tutorial. I haven't been able to implement any of the samples that I have found.

Thanks

like image 914
Gnanaskandhan Avatar asked Jul 04 '26 15:07

Gnanaskandhan


1 Answers

Adding a Script Task, yuo can use the ZipFile (class) here reference, you must refer to the System.IO.Compression.FileSystem assembly in the project (.NET Framework 4.5).

You need to provide to the Script Task the folder to be zipped and the name of the compressed folder as ReadOnlyVariables (to be added in the tab ReadOnlyVariables)

These two variables must be defined in the Variables tab (String type) of the package and can be changed dynamically through a cycle (eg. for each)

I use these two variables:

sFolderCompressed - the folder '.zip' that you want to obtain eg. C:\folder1\result.zip 
sFolderSource - the source folder containing the files affected eg. C:\folder1\folder2

enter image description here

The script is made using c#, choose Script Language: Microsoft Visual C#

enter image description here

This is the code to be added in the Main method:

using System.IO.Compression;

    public void Main()
    {
        try
        {
            string zipPath = (string)Dts.Variables["User::sFolderCompressed"].Value;
            string startPath = (string)Dts.Variables["User::sFolderSource"].Value;


            ZipFile.CreateFromDirectory(startPath, zipPath);
        }
        catch (Exception objException)
        {
            Dts.TaskResult = (int)ScriptResults.Failure;
            // Log the exception
        }
        Dts.TaskResult = (int)ScriptResults.Success;
    }

I hope can help.

like image 158
ɐlǝx Avatar answered Jul 07 '26 08:07

ɐlǝx