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:
- Create Package Parameters for SourceFolder and ArchFolder so the path can be changed by using SSIS configuration according to the environment
- Create FileName variable so we can save file name with extension when loop through files by using Foreach Loop Container
- 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"
- 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
- 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.
- 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
- How to change .NET Framework version in Script Task and Add Reference to Assembly(ZipFile Demo)
- Load Text Files,Zip them to Folder with Datetime and Delete From Input Directory in SSIS Package
- Extract Files From Zip Files and Delete the Zip Files once Unzipped in SSIS Package
- Load File/s, Zip and Delete from Source Folder in SSIS Package
- Zip ( Compress) Files and Add them to Folder According to Extension in SSIS Package
- Zip or Compress Files according to the Name of Files in SSIS Package
- Get File names from Zip Files and Insert into SQL Server Table in SSIS Package
- How to Zip ( Compress) Multiple Folders and Delete in SSIS Package
- Create Zip file per day with Date Only and Add Files to it in SSIS Package
No comments:
Post a Comment