SSIS: Zipping files with WinRAR
August 20, 2014 5 Comments
One of the strengths of SSIS (SQL Server Integration Services), is that’s it’s really powerful with the components available. But it’s easy to use the “Execute Process Task” to execute external tools. One of these tools can be WinRAR.
Zip file task
By default, SSIS doesn’t have a zip file task. There are some 3rd party components, like the one Joost van Rossum (Blog | @ssisjoost) built. You can find his custom task here. But what if you don’t to use 3rd party components? You can create your own zip task, by using an “Execute Process Task”.
Variables and Parameters
Because I want to keep this as easy as possible, and I might want to reuse this later in another package, I started by creating 2 parameters and 1 local variable:
Variable
Function:
To generating a unique zipfile name per day, I decided to add the date to the zipfile name
Name:
ZipName
Value:
“ZIPPED_” +
(DT_WSTR, 50) YEAR((DT_DBTIMESTAMP) @[System::StartTime]) + “_” +
RIGHT(“00” + (DT_WSTR, 50) MONTH((DT_DBTIMESTAMP) @[System::StartTime]), 2) + “_” +
RIGHT(“00” + (DT_WSTR, 50) DAY((DT_DBTIMESTAMP) @[System::StartTime]), 2) +
“.zip”
This expression will evaluate to:
Parameter 1
Function:
Point the “Execute Process Task” to the folder of the files that need to be zipped
Name:
FilePath
Value:
C:\Temp
Parameter 2
Function:
Tell WinRAR which files to zip in which directory
Name:
FilesToZip
Value:
C:\\Temp\\*.txt
When you added these Variable and Parameters, you should have this Variable:
And this Parameters:
Once that is done, you can add an “Execute Process Task” to the Control Flow. When you open the component you just added, go to the executable textbox, and point it to the WinRAR executable on your disk”:
Now you need to add the arguments that tell WinRAR what files to zip, where to zip them, and which files need to be zipped. In the “Execute Process Task”, click on Expressions, and on the dotted button:
In the next window, select “Arguments” under Property, and press the dotted button again:
Now paste the string below in this window:
“a -df ” + @[$Package::FilePath] + @[User::ZipName] + ” ” + @[$Package::FilesToZip]
If you press the evaluate button, you’ll see this result:
The command line switches we use are:
a: Archive files (or x for Extract files)
-df: Delete files after archiving
So once you know how it’s done, this is pretty easy, and does what it has to: zip your files without any issues. One thing to remember, is that SSIS can’t throw the exceptions WinRAR throws. So I tested this before, and didn’t notice the directory didn’t exist. My SSIS package kept on failing, and I didn’t know why. But after trying the expression we put into the expression textbox of the “Execute Process Task” in a command prompt, I did see the actual error.
So even though it’s easy to use, it’s not always easy to debug when it fails. But as long as you can execute it via command line yourself, you’re just a few steps away from seeing the actual error.
If you want to read more about SSIS, don’t forget to check out these blog posts:
– Julie Koesmarno: On sabbatical
– Mickey Stuewe: SQL Server data transferred to a sqlite database using ssis
– Chris Yates: ISPACs – Got To Love Em
Pingback: ISPACs – Got To Love Em | The SQL Professor
Pingback: SQL Server
Question (I work for WinZip). Could you have done the same using WInZip. We have many Enterprise customers and it would be a good thing to know. Thanks!
I’m convinced that also works. As far as I know WinZip can be used via command line. So you just need to point the “Execute Process Task” to the WinZip executable on disk, and know the parameters to use.
The reason I used WinRAR, is that it was already installed on the machine. But there’s no reason not to use WinZip if you have that installed.
Pingback: ISPACs – Got To Love Em - SQL Server - SQL Server - Toad World