Create Zip File with Date and Keep adding files to it after loading for the same day in SSIS Package - SQL Server Integration Services(SSIS) Package

Scenario:

We get multiple files during a day and night. we have scheduled to run our SSIS Package every hour. We want to create a Zip file per day and then keep adding the files to it after loading in the same day. Next day a new Zip file should be automatically created and files should be copied to that after loading.


Solution:

In this video we will learn the solution for our scenario " How to Create A Zip File with Date Per day and add files to it after loading in SSIS Package". Below are the list of items we will learn in this video


  1. How to Create Package Parameters for InputFolder and ArchFolder so we can change the values of them according to the environment ( QA, UAT, Prod) and we don't have to make any changes in the SSIS Package. 
  2. How to use Foreach Loop Container to look through files in SSIS Package
  3. Save File name in FileName Variable From Foreacah Loop Container
  4. How to Change .NET Framework 4 to 4.5
  5. How to add reference to Assemblies such as System.IO.Compression and System.IO.Compression.FileSystem
  6. Create empty Zip File by using Script Task
  7. Add file to Zip file by using Script task in SSIS Package


Script used in Script Task to Create Zip file with Date and Add Files to Zip File in SSIS Package


public void Main()
        {


            //Assign values to local variable from SSIS Package Parameters and Variables
            string zipfile = Dts.Variables["User::ArchFullPath"].Value.ToString();
            string ArchFolder = Dts.Variables["$Package::ArchFolder"].Value.ToString();
            string filename = Dts.Variables["User::FileName"].Value.ToString();
            string inputfilepath = Dts.Variables["$Package::InputFolder"].Value.ToString() + filename;


            //If zip File already exist for the same day, just add the files to it
            if (
            File.Exists(zipfile))
            {
                MessageBox.Show(" Zip File does Exists");
                using (ZipArchive addfile = ZipFile.Open(zipfile, ZipArchiveMode.Update))
                {
                    addfile.CreateEntryFromFile(inputfilepath, filename);
                    File.Delete(inputfilepath);
                }

                //If zip file does not exist for the day, create it and add files to it
            }
            else
            {
                MessageBox.Show("File Does not exists");
                var fileStream = new FileStream(zipfile, FileMode.Create);
                fileStream.Close();

                using (ZipArchive addfile = ZipFile.Open(zipfile, ZipArchiveMode.Update))
                {
                    addfile.CreateEntryFromFile(inputfilepath, filename);
                    File.Delete(inputfilepath);
                }

            }


            Dts.TaskResult = (int)ScriptResults.Success;
        }

Expressions for ArchFullPath Variable:
@[$Package::ArchFolder]+Replace(Substring((DT_WSTR,30)GEtdate(),1,10),"-","_")+".zip"






1 comment:

  1. Hi Sir,

    Could you please create a post which will convert csv file to password protected using ssis

    Regards,
    Anmol

    ReplyDelete

Note: Only a member of this blog may post a comment.