Exception when calling ZipFile.ExtractToDirectory from a Script Task

Nick Ryan (NZ) 121 Reputation points
2021-04-13T20:34:47.243+00:00

I've made myself a very simple package as a proof of concept. It calls a script task to unzip all the contents of a zip file. I've hard-coded the values for source zip and target directory.

The task fails with this (to me) very unhelpful error message.

87369-image.png

I'm not a C# programmer so I just copied the code from a page I now can't find.

This is my code:

#region Help:  Introduction to the script task  
/* The Script Task allows you to perform virtually any operation that can be accomplished in  
 * a .Net application within the context of an Integration Services control flow.   
 *   
 * Expand the other regions which have "Help" prefixes for examples of specific ways to use  
 * Integration Services features within this script task. */  
#endregion  
  
  
#region Namespaces  
using System.IO.Compression;  
#endregion  
  
namespace ST_7705c64ac5b442f6bd8a15993d3d2168  
{  
	[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]  
	public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase  
	{  
		public void Main()  
		{  
			string zipFilename = "\\<server name>\\MBSMIS\\Transfers\\IN\\Exonet\\Statements\\Statements.zip";  
			string targetDirectory = "\\<server name>\\MBSMIS\\Transfers\\IN\\Exonet\\Statements\\my-output-folder";  
  
			ZipFile.ExtractToDirectory(zipFilename, targetDirectory);  
  
			Dts.TaskResult = (int)ScriptResults.Success;  
		}  
		#region ScriptResults declaration  
		enum ScriptResults  
		{  
			Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,  
			Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure  
		};  
		#endregion  
	}  
}  

I have included a reference to System.IO.Compression.FileSystem which is probably a given as I don't think it compiles without that.

I've checked things like making sure the strings have the correct path and file-name and that the archive is not open anywhere else.

Is there a method of getting more helpful information out of the error or does that mean something to people more familiar with C#?

SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,438 questions
{count} votes

Accepted answer
  1. Yitzhak Khabinsky 24,831 Reputation points
    2021-04-13T21:54:03.363+00:00

    Hi @Nick Ryan (NZ) ,

    UNC file share should be like \server\share\dir1\dir2

    Please try the following in SSIS Script Task.

    void Main()  
    {  
    	bool rc = true;  
    	  
    	// UNC file share should be like \\server\share\dir1\dir2  
    	string zipFilename = @"\\server name\MBSMIS\Transfers\IN\Exonet\Statements\Statements.zip";  
    	string targetDirectory = @"\\server name\MBSMIS\Transfers\IN\Exonet\Statements\my-output-folder";  
      
    	try  
    	{  
    		ZipFile.ExtractToDirectory(zipFilename, targetDirectory);  
    	}  
    	catch (Exception ex)  
    	{  
    		rc = false;  
      
    		// Don't forget to tick/check off System::TaskName variable as ReadOnly in the Task Parameters  
    		Dts.Events.FireError(18, Dts.Variables["System::TaskName"].Value.ToString()  
    		  , ex.Message.ToString()  
    		  , "", 0);  
    	}  
      
    	Dts.TaskResult = (rc) ? (int)ScriptResults.Success : (int)ScriptResults.Failure;  
    }  
    

0 additional answers

Sort by: Most helpful