使用 SSIS 压缩文件夹

Zip a folder using SSIS(使用 SSIS 压缩文件夹)

本文介绍了使用 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 (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;
    }

希望能帮到你.

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

本文标题为:使用 SSIS 压缩文件夹

基础教程推荐