首页 > 解决方案 > 压缩多个 csv 的 SSIS 脚本任务失败 - 当前上下文中不存在名称 zipfile

问题描述

我正在使用 SSIS 和 ac# 脚本任务。我想在不使用任何 3rd 方应用程序的情况下压缩多个 csv 文件。

这是我的代码:

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO.Compression;
#endregion

    public void Main()
        {
            {
                string startPath = @"\\xxxxxxxxxxxxxxxxxxxxxx\*.csv";
                string zipPath = @"\\xxxxxxxxxxxxxxxxxxxxx\result.zip";              
                ZipFile.CreateFromDirectory(startPath, zipPath);              
            }
            Dts.TaskResult = (int)ScriptResults.Success;
        }

但是,ZipFile被下划线和错误:

当前上下文中不存在名称 zipfile

我基于此 Microsoft 示例: https ://docs.microsoft.com/en-us/dotnet/standard/io/how-to-compress-and-extract-files

上述文章指出:如果您收到构建错误“当前上下文中不存在名称 'ZipFile'”,请将 System.IO.Compression.FileSystem 程序集的引用添加到您的项目中。但是 SSIS 的脚本编辑器中没有 System.IO.Compression.FileSystem 吗?

标签: c#ssiszip

解决方案


我相信您没有正确设置对 System.IO.Compression 的引用。

仅使用该语句using System.IO.Compression;不会使 System.IO.Compression 可用,您还必须将正确的 dll 引用到您的项目中。System.IO.Compression 是.Net 附带的.Net 程序集。

  • 在解决方案资源管理器中右键单击项目内(或直接在项目上)的引用文件夹。
  • 选择添加参考
  • 在 Assemblies 或 .NET 选项卡下,查找直到找到 System.IO.Compression.FileSystem.dll。
  • 选择它并单击确定。

如果您使用的是核心,则可能需要从 nuget 包中安装它。


推荐阅读