Monday, 13 December 2021

Unzip the file without using the third party tool (Using C# code) in SSIS package

 In the previous post we saw that how to Zip files.

Read here: Zip the file without using the third party tool(Using C# code) in SSIS package

https://bageshkumarbagi-msbi.blogspot.com/2021/12/zip-file-without-using-third-party-tool.html  

In this post we will see how to unzip these files.  

We will unzip the Files without using the 3rd party tool.

Here we are using the GZipStream Class to unzip the files.

Taking Script task and writing the below C# code to unzip these files.   

We are using the below namespaces

using System;

using System.Data;

using Microsoft.SqlServer.Dts.Runtime;

using System.Windows.Forms;

using System.IO;

using System.IO.Compression;

Below is the C# code.

                public void Main()           

                                {

         

 

            string directoryPath = @"H:\SSIS1\ZipFiles";

 

            DirectoryInfo directorySelected = new DirectoryInfo(directoryPath);

 

        foreach (FileInfo fileToDecompress in directorySelected.GetFiles())

            {

                Decompress(fileToDecompress);

            }

 

            Dts.TaskResult = (int)ScriptResults.Success;

        }

 

       public static void Decompress(FileInfo fileToDecompress)

        {

            using (FileStream originalFileStream = fileToDecompress.OpenRead())

            {

 

                string currentFileName = fileToDecompress.FullName;

                string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

 

                using (FileStream decompressedFileStream = File.Create(newFileName))

                {

                    using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))

                    {

                        decompressionStream.CopyTo(decompressedFileStream);

                       

                    }

                }

            }

 

        }

Now running this package. 

See the files.  


Files are unzipped successfully.

No comments:

Post a Comment

If you have any doubt, please let me know.

Popular Posts