Click here to Skip to main content
15,878,809 members
Articles / SSIS

Compress Dynamic Files Using 7-Zip in SSIS

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
13 Feb 2013CPOL3 min read 30.8K   1   1
How to compress dynamic files using 7-zip in SSIS

Abstract

This blog post is in response to a request/comment from Thato in this previous article. Hence, I have taken much of the logic used in the aforementioned article and changed it from static to dynamic file compression. Thus, I am still using SQL Server Integration Services‘s Execute Process Task to compress dynamic files into a .rar format using 7-Zip.

Requirements

  • Microsoft Visual Studio 2005 or later
  • 7-Zip

Article

Let’s begin by launching Microsoft Visual Studio and create a new Integration Services Project which is located under Business Intelligence Projects category. (If you are using SQL Server 2012 or later, you will have to launch SQL Server Data Tools – SSDT.)

After you have assigned a project name, let's add some variables that will be used to store the dynamic content. I setup the variables as follows:

Image 1

  • var_FileName (is of type String and stores file name and extension)
  • var_WorkingDIR (is of type String and stores path for working directory)
  • var_NewFileName (is of type String and stores a derived archive file name)
  • var_Exec (is of type String and stores the path to the executable 7-Zip file)

Now let’s click and drag the ForEach Loop Container (FLC) into Control Flow pane from toolbox (SSIS Toolbox in SSDT).

Right click FLC and click on “Edit”. Under the Collection tab, ensure that Enumerator is Foreach File Enumerator. Setup Directory as expression and map it to the var_WorkingDIR variable. Lastly, make sure that Name and extension is checked under the Retrieve file name group box. In summary, the Collection tab should be configured as below:

Image 2

Still on FLC, under the Variable Mappings tab, map variable var_FileName to the first index 0 as shown below:

Image 3

The next step is to drag a Script Task (ST) into the FLC. The ST will be used to initialize variable var_NewFileName. Under the Script tab of the ST, ensure that you have configured the read/write variables as shown below:

Image 4

Still on ST, click Edit Script. Reference the System namespace as shown below:

The rest of the code in the Main class should be as follows:

Image 5

The final step is to drag and drop the Execute Process Task (EPT) into Control Flow pane from toolbox (SSIS Toolbox in SSDT).

Right click EPT and click on “Edit”.

Under the General Tab, you can assign relevant Name & Description. In this case, I have decided to name the task as “EPT – Compress Dynamic Files” whilst Description remains unchanged.

In the Process Tab, the EPT properties must be set as follows:

  • RequiredFullFileName: True
  • FailTaskIfReturnCodeIsNotSuccessValue: True
  • SuccessValue: 0
  • Timeout: 0
  • WindowStyle: Hidden

Still on the EPT, we now need to configure expressions. Thus, the expression tab should look as follows:

Image 6

Basically some of the noticeable things we have done above is to hide the EXE window that appears when the file is being compressed. The Executable and WorkingDirectory properties are set via variables. The Arguments property is also set using variables and we have further hard coded the extension of the compressed file to be that of type rar. Luckily 7-Zip supports various file compression types, so you can change the rar extension to an extension of your choice. Of course, depending on the extension you have provided, the size of the new compressed file will vary.

P.S.: You can download a copy of this package here.

It’s that simple.

Till next time folks, cheers!

Sifiso

This article was originally posted at http://www.selectsifiso.net

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead select SIFISO
South Africa South Africa
Sifiso W. Ndlovu is a certified Microsoft professional who holds a Master’s degree in IT Management from the University of Johannesburg. He specializes on a range of enterprise and consumer technologies using open source and proprietary software. He is the member of the Johannesburg SQL User Group wherein he has made several presentations on User Group Meetings and SQL Saturday sessions. He has written for a number of publications including SQLShack.com and SQLServerCentral.com.

Comments and Discussions

 
QuestionCompression error in SSIS Pin
dineshvishe13-Aug-13 20:47
dineshvishe13-Aug-13 20:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.