Click here to Skip to main content
15,885,985 members
Articles / Desktop Programming / Windows Forms

A Tool to Strip Zips of Unwanted Files before Submitting to CodeProject - Version 2

Rate me:
Please Sign up or sign in to vote.
4.17/5 (6 votes)
24 Dec 2008CPOL8 min read 32.2K   329   19  
An article on some minor modifications to CPZipStripper
/*
 * CPZipStripper 1.0
 * Written by Nishant Sivakumar [http://blog.voidnish.com] 
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using ICSharpCode.SharpZipLib.Zip;
using Microsoft.Win32;

namespace CPZipStripper
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public partial class MainForm : System.Windows.Forms.Form
	{
		[DllImport("user32.dll")]
		private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

		[DllImport("user32.dll")]
		private static extern bool AppendMenu(IntPtr hMenu, Int32 wFlags, Int32 wIDNewItem,
			string lpNewItem);

		public const Int32 WM_SYSCOMMAND = 0x112;
		public const Int32 MF_SEPARATOR = 0x800;
		public const Int32 MF_STRING = 0x0;
		public const Int32 IDM_ABOUT = 5000;

		//Nish's delegates
		private delegate void DroppedFileHandler(string s);

		//Nish's member variables
		private ArrayList m_extensions = new ArrayList();
		private ArrayList m_paths = new ArrayList();
		private ArrayList m_files = new ArrayList();
		private string m_curfile = "";
		private bool bContainsTrash = false;

		private CPZipStripperSettings zsSettings = null;

		private readonly string strAbout = "About CPZipStripper...";
		private string configXmlPath;

		public MainForm(string configXmlPath)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			zsSettings = new CPZipStripperSettings();
			zsSettings.Reload();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			MainToolTip.SetToolTip(BtnRefresh, "Refresh list of files");
			MainToolTip.SetToolTip(BtnExit, "Close the program");
			MainToolTip.SetToolTip(BtnConfig, "Open config XML in your XML editor");
			MainToolTip.SetToolTip(listBox1, "List of files in zip file, red ones will be stripped");
			MainToolTip.SetToolTip(panel1, "Drag and drop any zip file into this window to strip it of unwanted files");
			MainToolTip.SetToolTip(panel2, "Drag and drop any zip file into this window to strip it of unwanted files");
			MainToolTip.SetToolTip(label1, "Drag and drop any zip file into this window to strip it of unwanted files");

			this.configXmlPath = configXmlPath;
		}

		//class to hold list item data
		class ZipDataItem
		{
			public string Name;
			public bool IsTrash;
			public ZipDataItem(string name, bool istrash)
			{
				Name = name;
				IsTrash = istrash;
			}
			public override string ToString()
			{
				return Name;
			}
		}

		private void MainForm_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
		{
			Array arr = (Array)e.Data.GetData(DataFormats.FileDrop);
			if (arr != null)
			{
				BeginInvoke(new DroppedFileHandler(FilterZip),
					new object[] { arr.GetValue(0).ToString() });
			}
		}

		private void MainForm_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
		{
			e.Effect = e.Data.GetDataPresent(DataFormats.FileDrop) ?
				DragDropEffects.Copy : DragDropEffects.None;
		}

		private void FilterZip(string strFile)
		{
			try
			{
				ZipFile zf = null;
				zf = new ZipFile(strFile);
				bContainsTrash = false;
				listBox1.Items.Clear();
				foreach (ZipEntry ze in zf)
				{
					string nam = ze.Name;
					bool bistrash = IsFileStrippable(nam);
					listBox1.Items.Add(new ZipDataItem(nam, bistrash));
					if (bistrash)
						bContainsTrash = true;
				}
				zf.Close();
				statusBar1.Text = m_curfile = strFile;
				if (bContainsTrash)
				{
					MainToolTip.SetToolTip(BtnStrip, "Strip zip off unwanted files");
					BtnStrip.Enabled = true;

				}
				else
				{
					MainToolTip.SetToolTip(BtnStrip, "");
					BtnStrip.Enabled = false;
				}
			}
			catch (Exception)
			{
				MessageBox.Show(
					"Please drag/drop only valid zip files.",
					"Error : Unexpected file type!",
					MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void listBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
		{
			if (e.Index >= 0)
			{
				ZipDataItem zdi = (ZipDataItem)listBox1.Items[e.Index];
				string str = zdi.Name;
				Font nfont = new Font("Microsoft Sans Serif", 10);
				SolidBrush fillBrush = null;
				SolidBrush drawBrush = null;
				bool bMark = zdi.IsTrash;
				if ((e.State & DrawItemState.Focus) == 0)
				{
					fillBrush = new SolidBrush(SystemColors.Window);
					drawBrush = new SolidBrush(SystemColors.WindowText);
				}
				else
				{
					fillBrush = new SolidBrush(SystemColors.Highlight);
					drawBrush = new SolidBrush(SystemColors.HighlightText);
				}
				if (bMark)
					drawBrush = new SolidBrush(Color.Red);
				e.Graphics.FillRectangle(fillBrush,
					e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
				e.Graphics.DrawString(str, nfont, drawBrush,
					e.Bounds.X * 2, e.Bounds.Y);
			}
		}

		private void MainForm_Load(object sender, System.EventArgs e)
		{
			AddContextMenuItem(".zip", "ZipStrip", "Open with &ZipStrip",
				"\"" + Application.ExecutablePath + "\" \"%1\"");
			IntPtr sysMenuHandle = GetSystemMenu(Handle, false);
			AppendMenu(sysMenuHandle, MF_SEPARATOR, 0, string.Empty);
			AppendMenu(sysMenuHandle, MF_STRING, IDM_ABOUT, strAbout);
			ReadConfig();
			listBox1.Select();
			string[] argv = Environment.GetCommandLineArgs();
			if (argv.Length == 2)
			{
				StringBuilder strLongPath = new StringBuilder(512);
				FilterZip(argv[1]);
			}
		}

		private bool IsFileStrippable(string fname)
		{
			bool ret = false;
			string path = null, name = null, ext = null;
			SplitString(fname, ref path, ref name, ref ext);
			if (m_extensions.Contains(ext))
				ret = true;
			foreach (string pathstr in m_paths)
			{
				if (("/" + path).IndexOf("/" + pathstr + "/") != -1)
				{
					ret = true;
					break;
				}
			}
			if (m_files.Contains(name + "." + ext))
				ret = true;
			return ret;
		}

		private void SplitString(string fname, ref string path,
			ref string name, ref string ext)
		{
			path = name = ext = "";
			if (fname.Length != 0)
			{
				int slashpos = fname.LastIndexOf("/");
				if (slashpos != -1)
				{
					path = fname.Substring(0, slashpos + 1);
				}
				name = fname.Substring(path.Length);
				int dotpos = name.LastIndexOf(".");
				if (dotpos != -1)
				{
					ext = name.Substring(dotpos + 1);
					name = name.Substring(0, dotpos);
				}
			}
			path = path.ToLower();
			name = name.ToLower();
			ext = ext.ToLower();
		}

		private void ReadConfig()
		{
			XmlDocument xd = new XmlDocument();
			try
			{
				m_extensions.Clear();
				m_paths.Clear();
				xd.Load(configXmlPath);
				XmlNode xnext = xd["config"]["extensions"];
				if (xnext != null)
				{
					foreach (XmlNode xn in xnext.ChildNodes)
					{
						m_extensions.Add(xn.InnerXml);
					}
				}
				XmlNode xnpath = xd["config"]["paths"];
				if (xnpath != null)
				{
					foreach (XmlNode xn in xnpath.ChildNodes)
					{
						m_paths.Add(xn.InnerXml);
					}
				}
				XmlNode xnfile = xd["config"]["files"];
				if (xnfile != null)
				{
					foreach (XmlNode xn in xnfile.ChildNodes)
					{
						m_files.Add(xn.InnerXml);
					}
				}
			}
			catch (Exception)
			{
			}
		}

		private void BtnExit_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		private void BtnRefresh_Click(object sender, System.EventArgs e)
		{
			if (m_curfile.Length > 0)
			{
				ReadConfig();
				FilterZip(m_curfile);
			}
		}

		private void BtnConfig_Click(object sender, System.EventArgs e)
		{
			ProcessStartInfo psi = new ProcessStartInfo();
			bool edited = false;

			if (!this.zsSettings.SetByUser)
			{
				try
				{
					// Look for a system default editor
					psi.FileName = configXmlPath;
					psi.Verb = "Edit";
					psi.UseShellExecute = true;
					Process.Start(psi);
					edited = true;
				}
				catch (Exception)
				{
					// No App listed for Edit, try Open.
					try
					{
						psi.FileName = configXmlPath;
						psi.Verb = "Open";
						psi.UseShellExecute = true;
						Process.Start(psi);
						edited = true;
					}
					catch
					{
						if (!this.zsSettings.SetByUser)
						{
							// No Edit OR Open, Ask user for App.
							if (MessageBox.Show("Your system does not have a specified XML editor." +
							Environment.NewLine + "Do you want to select a default application to edit XML files?",
								"No Default XML Editor Found!",
								MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
							{

								this.GetNewXmlEditor();
							}
						}
					}
				}
			}

			if (!edited)
			{
				try
				{
					string editorfile = this.zsSettings.XmlEditor;
					if (this.configXmlPath.Contains(" "))
					{
						Process.Start(editorfile, "\"" + this.configXmlPath + "\"");
					}
					else
					{
						Process.Start(editorfile, this.configXmlPath);
					}
				}
				catch
				{
					MessageBox.Show("This tool is unable to edit the configuration file." + Environment.NewLine +
						"Close this application and try editing it manually, then restart this program.");
				}
			}
		}

		private void GetNewXmlEditor()
		{
			XMLEditorChooser.Show(this.zsSettings);
		}

		private void BtnStrip_Click(object sender, System.EventArgs e)
		{
			if (m_curfile.Length > 0)
			{
				string strNewFile = m_curfile.Remove(m_curfile.Length - 3, 3) + "tmp";
				ZipOutputStream zos = new ZipOutputStream(File.Create(strNewFile));

				ZipInputStream zis = new ZipInputStream(File.OpenRead(m_curfile));
				ZipEntry ze = null;
				while ((ze = zis.GetNextEntry()) != null)
				{
					if (!IsFileStrippable(ze.Name))
					{
						zos.PutNextEntry(new ZipEntry(ze.Name));

						byte[] buff = new byte[2048];
						int bytes = 0;
						while ((bytes = zis.Read(buff, 0, buff.Length)) > 0)
						{
							zos.Write(buff, 0, bytes);
						}
						zos.CloseEntry();
					}
				}
				zis.Close();
				zos.Close();

				System.IO.File.Move(m_curfile, m_curfile + ".old");
				System.IO.File.Move(strNewFile, m_curfile);

				FilterZip(m_curfile);
			}
		}

		protected override void WndProc(ref Message m)
		{
			if (m.Msg == WM_SYSCOMMAND)
			{
				switch (m.WParam.ToInt32())
				{
					case IDM_ABOUT:
						MessageBox.Show(
							"CodeProject Zip Stripper 1.0\r\n" +
							"Copyright(C) Nishant Sivakumar 2004\r\n" +
							"Web :- http://www.voidnish.com/\r\n" +
							"Blog :- http://blog.voidnish.com/", strAbout);
						return;
					default:
						break;
				}
			}
			base.WndProc(ref m);
		}

		private bool AddContextMenuItem(string Extension,
			string MenuName, string MenuDescription, string MenuCommand)
		{
			bool ret = false;
			RegistryKey rkey = Registry.ClassesRoot.OpenSubKey(Extension);
			if (rkey != null)
			{
				string extstring = rkey.GetValue("").ToString();
				rkey.Close();
				if (extstring != null)
				{
					if (extstring.Length > 0)
					{
						rkey = Registry.ClassesRoot.OpenSubKey(extstring, true);
						if (rkey != null)
						{
							string strkey = "shell\\" + MenuName + "\\command";
							RegistryKey subky = rkey.CreateSubKey(strkey);
							if (subky != null)
							{
								subky.SetValue("", MenuCommand);
								subky.Close();
								subky = rkey.OpenSubKey("shell\\" + MenuName, true);
								if (subky != null)
								{
									subky.SetValue("", MenuDescription);
									subky.Close();
								}
								ret = true;
							}
							rkey.Close();
						}
					}
				}
			}
			return ret;
		}

		private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.zsSettings.Save();
		}

		private void btnSetEditor_Click(object sender, EventArgs e)
		{
			this.GetNewXmlEditor();
		}
	}
}

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
Retired
United Kingdom United Kingdom
Retired Systems Admin, Programmer, Dogsbody.
Mainly on Systems for Local Government, Health Authorities,
Insurance Industry - (COBOL eeeeeeeugh).
Inventor of Synchronized Shopping.

Comments and Discussions