且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用SSIS压缩文件夹

更新时间:2023-09-26 09:17:40

添加脚本任务后,您就可以使用ZipFile(类)

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).

您需要向脚本任务提供要压缩的文件夹,并将压缩后的文件夹的名称提供为ReadOnlyVariables(将添加到选项卡ReadOnlyVariables中)

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)

我使用以下两个变量:

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

该脚本是使用c#制作的,请选择脚本语言:Microsoft Visual C#

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

这是要在Main方法中添加的代码:

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;
    }

希望能对您有所帮助.