Search Suggest

Zip sourcefile to archive

Case
I want to zip and delete my sourcefiles within the SSIS package when I have processed them.

Solution
This is a follow up on the unzip article. There are a lot of options to zip files within SSIS. For example:
* Update: my own zip task *





This solution uses the Microsoft Visual J# Redistributable Packages (don't worry you don't have to write J#), because it's free and you don't have use a third party or opensource dll on a production server which is often a big deal.

1) Visual J# Redistributable Packages
Install Microsoft Visual J# Redistributable Packages on your machine(s). You can download the correct version (x84, x64, IA64) at Microsoft.

2) Variables
Create two String variables named SourceFile and ArchiveFolder to store the filename and the zip/archive folder. Fill the ArchiveFolder with the folderpath where you want to store the zip files (for example H:\SoureFiles\Archive\).
Two String variables











3) Loop through folder with sourcefiles
This example loops through a folder, processes all files and then archives them. Create a foreach loop that loops through a folder with some text files (content doesn't matter for this example). Add an empty dataflow just for decoration. You can use that one later to process the textfiles.
Foreach file enumerator




















4) Map variable
Map the SourceFile variable to index 0 in the Foreach Loop.
Variable Mappings



















5) Script task with variables
Create a Script task within the Foreach loop and select the two variables as ReadOnly.
ReadOnly Variables



















6) Zip with Script task
Add a reference to the Microsoft Visual J# Redistributable Packages dll in your script. The name of the .NET dll is vjslib. After you have added it, it shows on the right site (3).
In Project menu, Add Reference...













After that add the following code (method and usings)
// C# code
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO; // Added
using java.util.zip; // Added
using java.io; // Added

namespace ST_967ebb1a53884e89ac28f0a69ad082f1.csproj
{
[System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{

#region VSTA generated code
enum ScriptResults
{
Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
};
#endregion

public void Main()
{
// Variables for zipfile and unzip folder
String SourceFile;
SourceFile = Dts.Variables["SourceFile"].Value.ToString();
String ArchiveFolder;
ArchiveFolder = Dts.Variables["ArchiveFolder"].Value.ToString();

// Determine zipfile path: archivefolder + filename (without path and extention) + "zip"
// Example:
// SourceFile = c:\temp\test.txt
// ArchiveFolder = c:\temp\archive\
// Then ZipFile = c:\temp\archive\test.zip
String ZipFile;
ZipFile = ArchiveFolder + SourceFile.Substring((SourceFile.LastIndexOf(@"\") + 1),(SourceFile.LastIndexOf(".") - SourceFile.LastIndexOf(@"\"))) + "zip";

// Create Zipfile
FileOutputStream fos = new FileOutputStream(ZipFile);
ZipOutputStream zos = new ZipOutputStream(fos);

// Add zipfile
FileInputStream fis = new FileInputStream(SourceFile);

// Remove drive and replace '\' with '/' for internal name
// Example:
// SourceFile = c:\temp\test.txt
// Then internal name = temp/test.txt
ZipEntry ze = new ZipEntry(SourceFile.Substring(3).Replace('\\', '/'));
zos.putNextEntry(ze);

// Loop through sourcefile to zip it
sbyte[] buffer = new sbyte[1024];
int len;
while((len = fis.read(buffer)) >= 0)
{
zos.write(buffer, 0, len);
}

// Close all objects
zos.closeEntry();
fis.close();
zos.close();
fos.close();

// Finally delete the source file
System.IO.File.Delete(SourceFile);

Dts.TaskResult = (int)ScriptResults.Success;
}
}
}
Note: you can delete the sourcefile with a File System Task aswell, but this is only one row of code extra.

7) Test
The end result looks something like this. Take it for a test drive...
The end result


















Note: This is a very basic script. You can add tons of checks and other features to make sure the script always works. Let me known if you have any questions. A unzip example is available here.

إرسال تعليق