SSIS Extract Zip file using System.IO.Compression

The System.IO.Compression namespace contains the following types for compressing and decompressing files and streams. You can also use these types to read and modify the contents of a compressed file:



To get more about the System.IO.Compression you may visit the detail page from MSDN website.



This demo is for those ETL developers want to use .net build-in compression library and to focus on their main task instead of digging through the internet and spending time searching for sample code using other third party library like 7zip and WiRar.

Please do take note that your production Integration Services Server must have .Net Framework 4.5 or later.


The following demo will show you how to use .net System.IO.Compression library to help you extract zip file in your SSIS project and for this short demo we will only use "ExtractToDirectory" method.


  • Create working directory for your SSIS Project and in my case my working directory is "D:\Temp\SSIS"
  • Copy your zipfile into your working directory


  • I created two string variables to place the full path of my zip file and destination directory
  • ZipFile default value "D:\Temp\SSIS\SamplezipFile.zip"
  • DestinationDirectory default value "D:\Temp\SSIS"




  • Open your Package.dtsx and create Script Task and rename it to "Extract Zip File"
  • Right click the script task and click Edit



  • On your Script Task Editor, under the ReadOnlyVariables add your two project variables that was declared before and click OK




  • Click the Edit Script button
  • Once the Visual Studio script editor open, on the Solution Explorer right click and select the Add Reference menu



  • Once the Add reference dialog box open, go to the directory where the System.IO.Compression.FileSystem.dll located and once the dll was selected click the Add button.




  • As you can see in the Reference Manager that the library was added and selected, click OK to close the window.



I place the sample code below, you may copy and paste it inside to your Main function

//I created two variables to hold the sytem variables 

//get $Project::ZipFile
string zipFile = Dts.Variables["$Project::ZipFile"].Value.ToString();

//get $Project::DestinationDirectory
string destinationDirectory = Dts.Variables["$Project::DestinationDirectory"].Value.ToString();

//extract the zip file
            System.IO.Compression.ZipFile.ExtractToDirectory(zipFile, destinationDirectory);

Dts.TaskResult = (int)ScriptResults.Success;



  •  Close the script editor and run your package.


  • Open your Window Explorer and open your destination directory and you will see that your zip file was extracted properly.








Comments

Popular posts from this blog

Error in smtp gmail using c#.net

Download TeamViewer version 6