Note: GZipStream

Welcome to CodeSpread! A 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 absolutely correct. It compresses data. There are various application of this class and is very efficient too. It saves data in the form of compressed files. GZipStream basically works with bytes. It create a stream of bytes and writes into the respective compressed file. The below code shows how to make use of this class: using (GZipStream gzStrm = new GZipStream(file, CompressionMode.Compress)) { gzStrm.Write(b, 0, b.Length); } Here ‘b’ is nothing but a byte array or it can be some other Data structure. ‘using’ keyword provide more flexibility with respect to Disposing of no more referenced object/streams. In C#, using keyword is used in 2 different ways: ‘using’ as Directive: mostly used while including the namespace. ‘using’ as part of code: used in block of code to handle dispose of objects(IDisposable object) ‘file’ is the FileStream object which can be created as shown below: using (FileStream file = new FileStream(fileName, FileMode.Create)) { //write your code related to file operation. // Here every time a new file is created which is marked by this: FileMode.Create. } Hope this short description about GZipStream shed some light. Please contribute your experience to improve...

Read More

VB.Net: Download file from FTP and UnZip

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...

Read More