Zip a folder in a Pragmatic way
Since I have been reading the book "The Pragmatic Programmer", I see the importance of using the available tools that you can find online. I have a requirement to add to a installer a zipped folder of the GlassFish (Java App Server) and then unzip it to process some files there and have GF running as a server. My first approach was use the GZipStream class from System.IO in my merge module. So I decided to do a Unit before include that code in my installer Merge Module. I found that C# class has not functionality to zip a folder with nested folders and I was thinking on create a recursive method to that job for me to accomplish that but I got a better "Pragmatic" idea, I googled it then I found a wonderful tool to do that, that fortunately is free to use on any project. It is SharpZipLib from ICSharpCode you can find it on http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx.
This is the code of the Unit Testing so I can sure this works great and it's too easy you only need to provide de Path of your folder as parameter
using NUnit.Framework;
using ICSharpCode.SharpZipLib.Zip;
namespace InstalllerTest
{
[TestFixture]
public class InstallJavaTest
{
private string zipName;
private string zipPath;
private string unzipPath;
[TestFixtureSetUp]
public void InitTest()
{
zipName = @"C:\Tests\GFJava.zip";
zipPath = @"C:\Sun\AppServer9U1";
unzipPath = @"C:\Tests";
}
[Test]
public void ZipFolderTest()
{
FastZip fZip = new FastZip();
fZip.CreateZip(zipName, zipPath, true, "");
Assert.IsTrue(File.Exists(zipName));
}
[Test]
public void UnZipFolderTest()
{
FastZip fZip = new FastZip();
fZip.ExtractZip(zipName, unzipPath, "");
Assert.IsTrue(Directory.GetFiles(unzipPath).Length > 1);
}
}
}