One of my packages is generating the CSV files. We need to Zip these files and there is another package which upload these files to FTP server.
Let’s see how we will zip the Files without using the 3rd
party tool.
Here we are using the GZipStream Class to Zip the files.
Below are the files.
Taking Script task and writing the below C# code to Zip
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 code.
public void Main() { string directoryPath = @"H:\SSIS1\ZipFiles"; DirectoryInfo directorySelected = new DirectoryInfo(directoryPath); foreach (FileInfo fileToCompress in directorySelected.GetFiles()) {
Compress(fileToCompress); } Dts.TaskResult
= (int)ScriptResults.Success; } public static void Compress(FileInfo
fileToCompress) { using (FileStream originalFileStream =
fileToCompress.OpenRead()) { if ((File.GetAttributes(fileToCompress.FullName) &
FileAttributes.Hidden) != FileAttributes.Hidden &
fileToCompress.Extension != ".gz") { using (FileStream compressedFileStream = File.Create(fileToCompress.FullName + ".gz")) { using (GZipStream compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress)) { originalFileStream.CopyTo(compressionStream); } } } } } |
Now running this package.
Package executed successfully.
Files are zipped successfully.
In Next post we will see how to unzip these files.
https://bageshkumarbagi-msbi.blogspot.com/2021/12/unzip-file-without-using-third-party.html
Thanks for reading!
No comments:
Post a Comment
If you have any doubt, please let me know.