使用SSIS压缩文件夹 [英] Zip a folder using SSIS

查看:86
本文介绍了使用SSIS压缩文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SSIS中压缩文件夹,源文件夹中有12个文件,我需要压缩该文件夹.我可以很好地压缩文件,我的问题是文件夹.

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.

我必须使用winzip来创建压缩包.

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.

谢谢

推荐答案

添加脚本任务后,您就可以使用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;
    }

希望能对您有所帮助.

这篇关于使用SSIS压缩文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆