Sunday, 22 September 2019

Scenario#2: Compressing a file using Script Task in SSIS (BIDS 2008)

Solution: Please add Script Task and write following code inside it.

        Public void Main(string[] args)
        {
             string srcPath = Dts.Variables["zipFiles"].Value.ToString();
            //string srcPath = @"C:\SQL3_Tasks\ssPharmacy\";
            srcPath = srcPath + "\\";
            string FileName = getFileName(srcPath);//"SSPharmacy20150605.txt";
            string JustFileName = FileName.Substring(0, FileName.Length - 4);

            FileInfo fileToCompress = new FileInfo(srcPath + FileName);

            FileStream sourceFile = File.OpenRead(srcPath + FileName);

            FileStream destinationFile = File.Create(srcPath + FileName + ".gz");

            byte[] buffer = new byte[sourceFile.Length];
            sourceFile.Read(buffer, 0, buffer.Length);

            using (GZipStream output = new GZipStream(destinationFile,
                CompressionMode.Compress))
            {
                output.Write(buffer, 0, buffer.Length);
            }
            sourceFile.Close();
           destinationFile.Close();

        }

Detailed Description:

  1. DTS Variables: We can add variables into SSIS Package with different scope levels. We can pass any variable to script task. Following are the steps for passing any variable to Task:
  2. Edit Script Task, which will open Script Task Editor. Please see below screen shot:
  3. There are two sections where we can provide our variables:
    1. ReadOnlyVariables: in this section we can add only those variables which we want to provide as input only and which is needed for our functionality implementation in Script Task.
    2. ReadWriteVaraibles: In this section we can add those varibales which we want to fill as our functionality output.