Zip or Compress Files according to the Name of Files in SSIS Package - SQL Server Integration Services(SSIS) Tutorial

Scenario:

We have one Source folder in which everybody puts the files to load. There are Customer Files those start with Customer_Date_Time, There are Sales files with Sales_DateTime etc.  We want to create an SSIS Package that can run at the end of the day and zip the files according to the names of the files.

Solution:

  1. Create Package Parameters for SourceFolder and ArchFolder so the path can be changed by using SSIS configuration according to the environment
  2. Create FileName variable so we can save file name with extension when loop through files by using  Foreach Loop Container 
  3. Create ZipFile Variable and write expression to build Full Zip File path, Here is the expression that we used in the video @[$Package::ArchFolder] +Substring( @[User::FileName] ,1,FindString( @[User::FileName],"_",1))+Replace(Substring((DT_WSTR,30)Getdate(),1,10),"-","_")+".zip"
  4. Change .Net Framework to 4.5 version in script task so we can add assemblies such as System.IO.Compression and System.IO.Compression.FileSystem
  5. Write Script to Create new Zip file if not exists according to name part and date. Then add the file to it and delete the file from source folder.
  6. If zip file already exists then add the file to it and delete the source file from Source Folder.

Script used in the Script task to create Zip File according to the name part of files in source folder.

public void Main()
        {

            //Assign values to local variable from SSIS Package Parameters and Variables
            string zipfile = Dts.Variables["User::ZipFile"].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);
                    
                    //Delete the file after zipping it
                    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);
                }

                //Delete the file after zipping it
                File.Delete(inputfilepath);
               
            }

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

How to Zip/Compress the files according to the name part in SSIS Package - SSIS Tutorial




    Related Posts/Videos on Zip / UnZip by Script Task  

No comments:

Post a Comment