Click here to Skip to main content
15,896,359 members
Articles / Desktop Programming / WPF

Zip My Code

Rate me:
Please Sign up or sign in to vote.
4.78/5 (17 votes)
20 Dec 2009CPOL3 min read 72K   2K   48  
A utility stripping your source code to the essential core and then compressing it to a nice CodeProject article attachment.
using System;
using System.IO;

using System.Resources;
using System.Text;

using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

public class DefComp : netz.compress.ICompress
{

		private string head = null;
		private string body = null;

		// a default, no parameter constructor is required
		public DefComp()
		{
			System.Reflection.Assembly a = this.GetType().Assembly;
			ResourceManager rm = new ResourceManager("defcomp", a);
			byte[] data = (byte[])rm.GetObject("head");
			head = Encoding.ASCII.GetString(data);
			data = (byte[])rm.GetObject("body");
			body = Encoding.ASCII.GetString(data);
			rm.ReleaseAllResources();
		}

		// netz.compress.ICompress implementation

		public long Compress(string file, string zipFile)
		{
			long length = -1;
			FileStream ifs = null;
			FileStream ofs = null;
			try
			{
				ifs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read);
				ofs = File.Open(zipFile, FileMode.Create, FileAccess.Write, FileShare.None);
				DeflaterOutputStream dos = new DeflaterOutputStream(ofs, new Deflater(Deflater.BEST_COMPRESSION));
				byte[] buff = new byte[ifs.Length];
				while(true)
				{
					int r = ifs.Read(buff, 0, buff.Length);
					if(r <= 0) break;
					dos.Write(buff, 0, r);
				}
				dos.Flush();
				dos.Finish();
				length = dos.Length;
				dos.Close();
			}
			finally
			{
				if(ifs != null) ifs.Close();
				if(ofs != null) ofs.Close();
			}
			return length;
		}

		// return null if none required
		public string GetRedistributableDLLPath()
		{
			return "zip.dll";
		}

		public string GetHeadTemplate()
		{
			return head;
		}

		public string GetBodyTemplate()
		{
			return body;
		}

}//EOC

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Axis Communications
Sweden Sweden
Got my first computer in the 90's and loved it even though it sounded like a coffeemaker.

Now getting paid for designing cool applications, and drinks the coffee instead of listening to it being made.

Comments and Discussions