且构网

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

使用 SSIS 压缩文件夹

更新时间:2023-09-26 09:26:28

添加脚本任务,你可以使用 ZipFile (class) 这里参考,必须参考项目中的 System.IO.Compression.FileSystem 程序集(.NET 框架 4.5).

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)

这两个变量必须在包的Variables选项卡(String类型)中定义,并且可以通过循环动态改变(例如for each)

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
esult.zip 
sFolderSource - the source folder containing the files affected eg. C:folder1folder2

脚本使用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;
    }

希望能帮到你.