Unzip Single File from a Zip file using Native .NET Code
Using SSIS 2005 can I unzip a "Single File" from a .ZIP file? Please advise.
I know there are many 3rd party tools we can install and use process task to do this or I think VJ# has this inbuilt. But I was looking for some solution using .NET's own "System.IO.Packaging.Package ".
Thanks.
October 28th, 2010 12:45pm
The way I see it, you need to use a Script Component.
There are many ZIP libraries available, please refer to
http://weblogs.asp.net/jgalloway/archive/2007/10/25/creating-zip-archives-in-net-without-an-external-library-like-sharpziplib.aspx
An interesting fact: .Net 4.0 supports compression out of the box -
http://msdn.microsoft.com/en-us/library/system.io.compression.aspx
So you could merely use a wrapper to extract a single file or create a custom SSIS component to do that, like here:
http://aminosoftware.com/articles/ssis-components-getting-starting/Arthur My Blog
Free Windows Admin Tool Kit Click here and download it now
October 28th, 2010 1:14pm
Hi Arthur,
Thanks for your reply. Actually I was looking for the .NET Out of the box stuff. So Can I use those .NET 4.0 Namespace in SSIS 2005. I think the SQL Server box should have .NET 4.0 installed? (As a prerequisite here?)
Can you also point me to some sample for this?
Thanks.
October 28th, 2010 1:25pm
Hi,
the .Net 4.0 is not installed with the SQL Server 2005.
In several of my packages I simply use a command line to unzip/zip multiple files. So it is an Execute Process task.
This article:
http://www.thejoyofcode.com/SSIS_Compress_File_Task.aspx might be helpful for you.
Calling a bat file from a Script Task is the warpper I meant.Arthur My Blog
Free Windows Admin Tool Kit Click here and download it now
October 28th, 2010 1:39pm
A good example I found I read once in our forum:
http://social.msdn.microsoft.com/Forums/en-US/sqlintegrationservices/thread/00f110d9-9be8-481c-9696-75be9973e728Arthur My Blog
October 28th, 2010 1:41pm
I this one need Visual J# library to be installed.
Thanks,
Prabhat Nath
Free Windows Admin Tool Kit Click here and download it now
October 28th, 2010 2:32pm
System.IO.Compression is supported in .NET Framework 2.0 too. Please see the following article for more information:
http://msdn.microsoft.com/en-us/library/system.io.compression(v=VS.80).aspx
The code in the thread Arthur posted is not J#, but a VB.NET. If you want C#, please see:
using Microsoft.CSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Math;
using System.Text;
using System.IO.Compression;
using Microsoft.SqlServer.Dts.Runtime;
///<summary>Decompress file</summary>
public class ScriptMain
{
public void Main()
{
Dts.TaskResult = Dts.Results.Success;
bool success = true;
string workFilePath = null;
workFilePath = Dts.Variables("CompressedFilePath").Value.ToString();
if (File.Exists(workFilePath)) {
if (!workFilePath.EndsWith(".gz")) {
Dts.Events.FireInformation(0, "", workFilePath + " is not compressed; skipping decompression", null, -1, true);
return;
}
string uncompressedFileName = null;
byte[] bytes = new byte[Int16.MaxValue + 1];
int n = 1;
try {
uncompressedFileName = workFilePath.Substring(0, workFilePath.Length - 3);
Dts.Events.FireInformation(0, "", "decompressing " + workFilePath + " to " + uncompressedFileName, null, -1, true);
using (FileStream writer = new FileStream(uncompressedFileName, FileMode.Create)) {
using (Stream compressedStream = File.Open(workFilePath, FileMode.Open, FileAccess.Read, FileShare.None)) {
using (GZipStream unzipper = new GZipStream(compressedStream, CompressionMode.Decompress)) {
while (!(n == 0)) {
n = unzipper.Read(bytes, 0, bytes.Length);
writer.Write(bytes, 0, n);
}
unzipper.Close();
}
compressedStream.Close();
}
writer.Close();
}
} catch (Exception ex) {
Dts.Events.FireError(0, ex.TargetSite().ToString(), "Unable to decompress " + workFilePath + "; " + ex.Message, null, -1);
success = false;
} finally {
if (success == false & File.Exists(uncompressedFileName)) {
Dts.TaskResult = Dts.Results.Failure;
File.Delete(uncompressedFileName);
}
}
} else {
Dts.Events.FireError(0, "", workFilePath + " does not exist", null, -1);
Dts.TaskResult = Dts.Results.Failure;
return;
}
}
}
Thanks,
Jin ChenJin Chen - MSFT
November 1st, 2010 6:15am