Search content within the blog

Thursday, July 29, 2010

Zip and Unzip files using IcsharpCode

Add a referece to the DLL Ichsarpcode.SharpZipLib in either your project where you want to use it else add it in GAC.

Code to compress/Zip
private static void CompressZip(string sPath)
{
ZipOutputStream zipOut = new ZipOutputStream(File.Create(@"C:\ZippedContents\test.zip"));
foreach (string fName in Directory.GetFiles(sPath))
{
FileStream sReader = File.OpenRead(fName);
byte[] buff = new byte[Convert.ToInt32(sReader.Length)];
sReader.Read(buff, 0, (int)sReader.Length);
FileInfo fi = new FileInfo(fName);
ZipEntry entry = new ZipEntry(fi.Name);
entry.DateTime = fi.LastWriteTime;
entry.Size = sReader.Length;
sReader.Close();
zipOut.PutNextEntry(entry);
zipOut.Write(buff, 0, buff.Length);
}
zipOut.Finish();
zipOut.Close();
Console.WriteLine("Done!!");
}

Where:sPath is the path to the folder you want to compress


Code to decompress/Unzip

private static void UncompressZip(string sFile)
{
ZipInputStream zipIn = new ZipInputStream(File.OpenRead(sFile));
ZipEntry entry;
while ((entry = zipIn.GetNextEntry()) != null)
{
FileStream streamWriter = File.Create(@"C:\UnzipHere\" + entry.Name);
long size = entry.Size;
byte[] data = new byte[size];
while (true)
{
size = zipIn.Read(data, 0, data.Length);
if (size > 0) streamWriter.Write(data, 0, (int)size);
else break;
}
streamWriter.Close();
}
Console.WriteLine("Done!!");
}
Where: sFile is the zip file you want to unzip.

Tuesday, July 20, 2010

Advantages of hosting WCF Services in IIS as compared to self-hosting?

what are the advantages of hosting WCF Services in IIS as compared to self-hosting?

Automatic activation


IIS provides automatic activation that means the service is not necessary to be running in advance. When any message is received by the service it then launches and fulfills the request. But in case of self hosting the service should always be running.


Process recycling


If IIS finds that a service is not healthy that means if it has memory leaks etc, IIS recycles the process. Ok let us try to understand what is recycling in IIS process. For every browser instance, a worker process is spawned and the request is serviced. When the browser disconnects the worker, process stops and you loose all information. IIS also restarts the worker process. By default, the worker process is recycled at around 120 minutes. So why does IIS recycle. By restarting the worker process it ensures any bad code or memory leak do not cause issue to the whole system.
In case of self-hosting both the above features, you will need to code yourself. Lot of work right!!. That is why IIS is the best option for hosting services until you are really doing something custom

IIS Recycle option shown below.