OLAP
Welcome to CodeSpread!

Experience:

Many application requires data migration tasks to execute on daily basis. To achieve this we generally seek help of SSIS package, if at all we are making use of SQL sever.

So, consider a case where you need to download a file from FTP location. Besides this, what if the file present on the FTP is in Zip format. How will you unzip and download. . . ?

Don’t worry!! Here is the solution:

You need to import the following namespaces:

System.IO.FileStream
System.IO.Compression
Microsoft.SqlServer.Dts.Runtime

The below code is written in VB.Net:

CODE:

Dim success As Boolean = True
Dim FilePath As String
FilePath = Dts.Variables("FilePath").Value.ToString()
Dim uncompressedFileName As String
Dim bytes(Int16.MaxValue) As Byte
Dim n As Integer = 1
uncompressedFileName = FilePath
Dts.Events.FireInformation(0, "", "decompressing " + FeedPath + " to " + uncompressedFileName, Nothing, -1, True)
Using writer As New IO.FileStream(uncompressedFileName, FileMode.Create)
Using compressedStream As Stream = File.Open(FilePath, FileMode.Open, FileAccess.Read, FileShare.None)
Using unzipper As New GZipStream(compressedStream, CompressionMode.Decompress)
Do Until n = 0
n = unzipper.Read(bytes, 0, bytes.Length)
writer.Write(bytes, 0, n)
Loop
unzipper.Close()
End Using
compressedStream.Close()
End Using
writer.Close()
success = True
End Using

Brief Overview:

  • Here “FilePath” is the location of the respective file on the FTP and Dts is the main object on which your package does all the operation and finally returns the results.
  • GZipStream(Read more here) is the method used for compression task which is present inside System.IO.Compression
  • Dts.Variables(“FilePath”).Value – This line is using the variable named “FilePath” which should be declared while creation of the package, probably one among the user defined variables.
  •  Please note that it is recommended and good coding habit to close the IO stream connection used while read/write operation of any file.

Rest of the code is self explanatory. Happy Compression!!

Author: jackie

I am a developer.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

More from CodeSpread:

  • SQL:My first SSIS PackageSQL:My first SSIS PackageI would like to share my experience about a situation, where I was given a task to export a file(say XML) from FTP location and get the data from this file populated into our database tables. Moreo...
  • C# Code to send a MailC# Code to send a MailWhile working in a project, we always neglect the importance of the code which we have written many times and now end up in copy pasting the code from old projects. Lately, I saw a request in fo...
  • Note: GZipStreamNote: GZipStreamA short note from my experience: From the name itself you can at least make a guess about the functionality of the GZipStream class(present under: System.IO.Compression). Yes..your guess is abso...
  • Nullable .Net typesNullable .Net typesWe declare .net types in our project daily but still we do not recollect default values for each .net type so we end up doing null check for each data type. Its a basic mistake of a developer but ....
  • Asp.Net: More about CookiesAsp.Net: More about CookiesWe have seen, how our cookies looks in our last article Cookies Part 1. In this article, we will talk about their properties,limitations and technical part. Properties and Limitations Cook...
  • Useful ‘ref’ and ‘out’ parametersUseful ‘ref’ and ‘out’ parameters It took me some time to realize the real potential of ref and out keywords, till the time I experience a condition which could only be simplified by implementing ref and out keyword. Lets st...
  • Asp.Net: Hidden Truth of CookiesAsp.Net: Hidden Truth of CookiesI know there are several articles on cookies as what are they? and how can they be created? but i always wanted to know more about them. My questions were where they are stored? how to track their ...
  • Concepts: S O L I DConcepts: S O L I DSOLID - Object oriented programming principles. May be its easy to forget these principles  but for creating a reusable code or maintainable code, people will always have to come back and check ...