[Note from Pinal]: This is a 130th episode of Notes from the Field series. In this episode we are going to learn something very simple but effective about SIS and Zip. This subject is not very much discussed about and hardly there are many information about this subject available. In this episode of the Notes from the Field series database expert Kevin Hazzard explains the how to use ZIP with SSIS. Read the experience of Kevin in his own words.
My son David is a database and ETL developer like me. He came to me with an interesting problem a while back. After dumping a bunch of tables into text-based data files using SSIS, he was required to zip them up before transmitting them via FTP. Everything he needed to do the job was built into SSIS except for the zipping function.
In the UNIX world, I’d have told him to pack the directory into a tar ball from the command line. But how do you zip up an entire folder from the command line in Windows? Better still, how could you do this programmatically within SSIS? Archiving and unarchiving of files and folders should be something that Windows does with ease. Yet, Windows was pretty late to the game with respect to compressed folder support. While some compression capabilities are built into the Windows shell, doing this from the command line without installing software isn’t straightforward.
ZIP with SSIS
If you search for “Zip Windows Command Line” across the web, you’ll find lots of third party tools that can do the job. And if you restrict the search to “Zip in SSIS” you’ll find pretty much the same set of tools that you must weave into a solution using libraries which often need to be installed in the Global Assembly Cache (GAC). Let’s face it: nobody likes to use third party tools in SSIS, especially to solve problems that seem so simple. Aside from the potential legal and security concerns, deploying and maintaining installed applications on various ETL servers can present a range of non-trivial, lingering operational issues that someone must manage.
I assumed that because the major search engines pointed only to these kinds of solutions on the first page or two of results that they represented the best-known way to go about zipping up files within SSIS. After an hour on his own, David came back to me and said he had found a better way. He discovered that the .NET Framework versions 4.5 and newer include a handy class in the System.IO.Compression namespace called ZipFile. Inside that class is a method named CreateFromDirectory that will do the trick. David simply dropped a script task onto his SSIS control flow, set the script project’s .NET Framework version to 4.5 and added a few lines of C# code that looked like this:
string filename = Dts.Variables["vZipFileName"].Value.ToString(); if (System.IO.File.Exists(filename)) System.IO.File.Delete(filename); System.IO.Compression.ZipFile.CreateFromDirectory( Dts.Variables["vExtractDirectory"].Value.ToString(), filename);
All that’s required are the two parameters vZipFileName and vExtractDirectory passed via SSIS variables. The script first checks to see if the Zip file already exists and deletes it as necessary. David had discovered through repeated testing that the deletion step was necessary since the CreateFromDirectory call will fail if the target file already exists.
It’s really that easy: aside from the required .NET Framework upgrade, there are no other tools or libraries to install. As you can imagine, I was pleasantly surprised by David’s quick research, his out-of-the-box thinking and the elegant solution that came from it. Other interesting methods in the ZipFile class like ExtractToDirectory and Open can be used to read and process compressed files if you need to ingest data from them, of course. I hope you find this simple way of handling Zip files as useful on your SSIS journeys as David’s proud Dad does.
If you want to get started with SQL Server with the help of experts, read more over at Fix Your SQL Server.
Reference: Pinal Dave (http://blog.sqlauthority.com)
First appeared on How to Use Zip With SSIS? – Notes from the Field #130