8/6/2019 Ssis Unzip Script
1/7
[Type the document title]
[Type the document subtitle]
[Type the abstract of the document here. The abstract is typically a short summary of the contents
of the document. Type the abstract of the document here. The abstract is typically a short summary
of the contents of the document.]
8/6/2019 Ssis Unzip Script
2/7
SSIS Unpack a ZIP file with the Script Task
A while ago I needed to unpack a couple of zip files from SSIS. There is no Microsoft
SSIS task that contains this functionality so I searched the Internet. It seems that there
are quite some third party tools that offer this functionally. It's also possible to download
custom SSIS tasks. I personally always try to avoid third party tools and custom tasks so
I searched on.It seemed there is a way to unzip files from SSIS with the Script Task. With some Visual
Basic code using the Visual J# Library you can do the job. In this blog post I will use a
Foreach Loop Container to loop through a folder that contains multiple zip files and unzip
them one-by-one.
Make sure you have the Microsoft Visual J# Redistributable Package installed because a
reference to vjslib.dll (Visual J# Library) is needed in the Script Task. Download it here
for free.
Drag and drop a Foreach Loop Container on the Control Flow and create three variables
with scope on the Foreach Loop Container:
Now configure the Foreach Loop Container:
- Enumerator: Foreach File Enumerator
- Files: *.zip
8/6/2019 Ssis Unzip Script
3/7
- Retrieve file name: Name and extension
Next click on the + next to Expressions add the following expression to connect the
SourceFolder variable to the Directory property of the Foreach Loop Container:
Now go to the Variable Mappings and select the FileName variable on Index 0. Doing this
we will be able to access the current file name when the Foreach Loop Container
8/6/2019 Ssis Unzip Script
4/7
enumerates the zip files.
Now drag and drop a Script Task on the Control Flow, inside the Foreach Loop Container:
Open the Script Task Editor and do the following:- Set the ScripLanguage on: Microsoft Visual Basic 2008
8/6/2019 Ssis Unzip Script
5/7
- Select our three ReadOnlyVariables using the new SSIS2008 Select Variables window:
Now click Edit Script and copy/paste the following script: Imports SystemImports System.DataImports System.MathImports Microsoft.SqlServer.Dts.RuntimeImports java.util.zip
PublicSub Main()Try
Dim strSourceFile AsStringDim strDestinationDirectory As String
'MsgBox("Current File: " & Dts.Variables("FileName").Value.ToString)strDestinationDirectory = Dts.Variables("DestinationFolder").Value.ToStringstrSourceFile = Dts.Variables("SourceFolder").Value.ToString &
Dts.Variables("FileName").Value.ToStringDim oFileInputStream As New java.io.FileInputStream(strSourceFile)Dim oZipInputStream As New java.util.zip.ZipInputStream(oFileInputStream)Dim bTrue As Boolean = TrueDim sbBuf(1024) As SByteWhile 1 = 1
Dim oZipEntry As ZipEntry = oZipInputStream.getNextEntry()If oZipEntry IsNothingThenExitWhileIf oZipEntry.isDirectory Then
8/6/2019 Ssis Unzip Script
6/7
If NotMy.Computer.FileSystem.DirectoryExists(strDestinationDirectory &oZipEntry.getName) Then
My.Computer.FileSystem.CreateDirectory(strDestinationDirectory &oZipEntry.getName)
End IfElse
Dim oFileOutputStream As Newjava.io.FileOutputStream(strDestinationDirectory.Replace("\", "/") & oZipEntry.getName())
While 1 = 1
Dim iLen As Integer = oZipInputStream.read(sbBuf)If iLen < 0 Then Exit WhileoFileOutputStream.write(sbBuf, 0, iLen)
End WhileoFileOutputStream.close()
End IfEnd WhileoZipInputStream.close()oFileInputStream.close()
Catch ex As ExceptionThrow New Exception(ex.Message)
End TryEnd Sub
End Class
Now only one thing needs to be done, add a reference to vjslib.dll (Visual J# Lib rary):
8/6/2019 Ssis Unzip Script
7/7
&
Your unzip solution is ready now! For testing purposes you can uncomment the following
line in the script to see the file name of each processed zip file in a message box at
runtime:
'MsgBox("Current File: " & Dts.Variables("FileName").Value.ToString)
Top Related