Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / C#

Managed C++ wrapper for ZLib

Rate me:
Please Sign up or sign in to vote.
4.56/5 (14 votes)
3 Mar 2005CPOL9 min read 155.4K   5.7K   46  
.NET wrapper for ZLib, written in MC++
/*
DevelopDotNet
http://www.developdotnet.com

File Created by: Alberto Ferrazzoli
Date: 22/05/2004

Class Description:
ZTest test application for DevelopDotnet.Compressor type library.

Notes:

Revision Log - Please mark significant changes in source code in the following format:

Date  -  Time -  Reviewer  -			Comments
15/06/2004		 Alberto Ferrazzoli		Added ZCompressor Test
19/06/2004		 Alberto Ferrazzoli		Added CRC Test
07/08/2004		 Alberto Ferrazzoli		Zip Progress on status bar
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Diagnostics;
using System.IO;
using System.Text;

using System.Runtime.Serialization.Formatters.Binary;
using DevelopDotNet.Compression;

namespace ZTest
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>

	public class ZTest : System.Windows.Forms.Form
	{
		private double _FN1;
		private double _FN2;
		private const string _sFibonacci = @"Fibonacci.dat";

		private ZipArchive _archive;
		private int _totalFilesFound;
		private bool _isMmoryZip;
		ProgressBar _progressBar;

		private Byte [] _compressedData;

		private MenuItem mnuLevel , mnuStrategy;
		private System.ComponentModel.IContainer components;

		#region Windows Form Components

		private System.Windows.Forms.MenuItem mnuFile;
		private System.Windows.Forms.MenuItem mnuFileExit;
		private System.Windows.Forms.MenuItem mnuHelp;
		private System.Windows.Forms.MenuItem mnuHelpAbout;
		private System.Windows.Forms.TabPage tabZStream;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.TabPage tabCompressor;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox txtData;
		private System.Windows.Forms.Button btnUncompress;
		private System.Windows.Forms.Button btnCompress;
		private System.Windows.Forms.StatusBar statusBar;
		private System.Windows.Forms.MenuItem mnuCompression;
		private System.Windows.Forms.MenuItem mnuCompressionLevel;
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Button btnSave;
		private System.Windows.Forms.Button btnLoad;
		private System.Windows.Forms.Button btnGenerate;
		private System.Windows.Forms.TextBox txtOut;
		private System.Windows.Forms.MainMenu mainMenu;
		private System.Windows.Forms.TabControl tabControl;
		private System.Windows.Forms.TabPage tabCrc;
		private System.Windows.Forms.Button btnAdler;
		private System.Windows.Forms.Button btnCrc;
		private System.Windows.Forms.Label lblAdler;
		private System.Windows.Forms.Label lblCrc;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TabPage tabZip;
		private System.Windows.Forms.ImageList imglZipToolbar;
		private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog;
		private System.Windows.Forms.TextBox txtComment;
		private System.Windows.Forms.Splitter splitter2;
		private System.Windows.Forms.Panel panelZipFiles;
		private System.Windows.Forms.ListView lvwZipFiles;
		private System.Windows.Forms.ToolBar toolBarZip;
		private System.Windows.Forms.ColumnHeader clhZipName;
		private System.Windows.Forms.ToolBarButton tlbSetPassword;
		private System.Windows.Forms.ToolBarButton tlbRemove;
		private System.Windows.Forms.ToolBarButton tlbExtractTo;
		private System.Windows.Forms.ToolBarButton tlbAddComment;
		private System.Windows.Forms.ToolBarButton tlbZipTest;
		private System.Windows.Forms.ColumnHeader clhZipSize;
		private System.Windows.Forms.ColumnHeader clhZipSizePacked;
		private System.Windows.Forms.ColumnHeader clhZipDateModified;
		private System.Windows.Forms.ColumnHeader clhZipCRC32;
		private System.Windows.Forms.StatusBarPanel statusBarPanel1;
		private System.Windows.Forms.StatusBarPanel statusBarPanel2;
		private System.Windows.Forms.ColumnHeader clhZipCompressRatio;
		private System.Windows.Forms.ColumnHeader clhZipComment;
		private System.Windows.Forms.ContextMenu cmnuZipFiles;
		private System.Windows.Forms.MenuItem cmnuZipFilesComment;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.MenuItem cmnuZipFilesTest;
		private System.Windows.Forms.MenuItem cmnuZipFilesRemove;
		private System.Windows.Forms.MenuItem cmnuOpenMemory;
		private System.Windows.Forms.ToolBarButton tlbOpen;
		private System.Windows.Forms.ToolBarButton tlbAdd;
		private System.Windows.Forms.ToolBarButton tlbNew;
		private System.Windows.Forms.Button btnBrowseIn;
		private System.Windows.Forms.TextBox txtFileIn;
		private System.Windows.Forms.ContextMenu cmnuNewZip;
		private System.Windows.Forms.MenuItem mnuCompressionStrategy;
		#endregion

		public ZTest()
		{
			//
			// Required for Windows Form Designer support
			//

			InitializeComponent();

			#region Initialize Menus
			EventHandler handlerLevel = new EventHandler(OnCompressionLevelClick);

			foreach(CompressionLevel level in Enum.GetValues(typeof(CompressionLevel)))
			{
				MenuItem mitem = new MenuItem(level.ToString(), handlerLevel);
				mitem.RadioCheck = true;
				mnuCompressionLevel.MenuItems.Add(mitem);
			}

			mnuLevel = mnuCompressionLevel.MenuItems[3];
			mnuLevel.Checked = true;

			EventHandler handlerStrategy = new EventHandler(OnCompressionStrategyClick);

			foreach(CompressionStrategy strategy in Enum.GetValues(typeof(CompressionStrategy)))
			{
				MenuItem mitem = new MenuItem(strategy.ToString(), handlerStrategy);
				mitem.RadioCheck = true;
				mnuCompressionStrategy.MenuItems.Add(mitem);
			}

			mnuStrategy = mnuCompressionStrategy.MenuItems[0];
			mnuStrategy.Checked = true;
			#endregion

			//_progress = 0;
			_progressBar = new ProgressBar();
			statusBar.Controls.Add(_progressBar);
			UpdateStatusBar();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}

			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code

		/// <summary>

		/// Required method for Designer support - do not modify

		/// the contents of this method with the code editor.

		/// </summary>

		private void InitializeComponent()

		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ZTest));
			this.mainMenu = new System.Windows.Forms.MainMenu();
			this.mnuFile = new System.Windows.Forms.MenuItem();
			this.mnuFileExit = new System.Windows.Forms.MenuItem();
			this.mnuCompression = new System.Windows.Forms.MenuItem();
			this.mnuCompressionLevel = new System.Windows.Forms.MenuItem();
			this.mnuCompressionStrategy = new System.Windows.Forms.MenuItem();
			this.mnuHelp = new System.Windows.Forms.MenuItem();
			this.mnuHelpAbout = new System.Windows.Forms.MenuItem();
			this.statusBar = new System.Windows.Forms.StatusBar();
			this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
			this.tabControl = new System.Windows.Forms.TabControl();
			this.tabCompressor = new System.Windows.Forms.TabPage();
			this.label3 = new System.Windows.Forms.Label();
			this.txtData = new System.Windows.Forms.TextBox();
			this.btnUncompress = new System.Windows.Forms.Button();
			this.btnCompress = new System.Windows.Forms.Button();
			this.tabZStream = new System.Windows.Forms.TabPage();
			this.panel1 = new System.Windows.Forms.Panel();
			this.txtOut = new System.Windows.Forms.TextBox();
			this.btnSave = new System.Windows.Forms.Button();
			this.btnLoad = new System.Windows.Forms.Button();
			this.btnGenerate = new System.Windows.Forms.Button();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.tabCrc = new System.Windows.Forms.TabPage();
			this.label1 = new System.Windows.Forms.Label();
			this.lblCrc = new System.Windows.Forms.Label();
			this.lblAdler = new System.Windows.Forms.Label();
			this.btnBrowseIn = new System.Windows.Forms.Button();
			this.btnCrc = new System.Windows.Forms.Button();
			this.txtFileIn = new System.Windows.Forms.TextBox();
			this.btnAdler = new System.Windows.Forms.Button();
			this.tabZip = new System.Windows.Forms.TabPage();
			this.panelZipFiles = new System.Windows.Forms.Panel();
			this.lvwZipFiles = new System.Windows.Forms.ListView();
			this.clhZipName = new System.Windows.Forms.ColumnHeader();
			this.clhZipSize = new System.Windows.Forms.ColumnHeader();
			this.clhZipSizePacked = new System.Windows.Forms.ColumnHeader();
			this.clhZipCompressRatio = new System.Windows.Forms.ColumnHeader();
			this.clhZipDateModified = new System.Windows.Forms.ColumnHeader();
			this.clhZipCRC32 = new System.Windows.Forms.ColumnHeader();
			this.clhZipComment = new System.Windows.Forms.ColumnHeader();
			this.cmnuZipFiles = new System.Windows.Forms.ContextMenu();
			this.cmnuZipFilesComment = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.cmnuZipFilesRemove = new System.Windows.Forms.MenuItem();
			this.cmnuZipFilesTest = new System.Windows.Forms.MenuItem();
			this.toolBarZip = new System.Windows.Forms.ToolBar();
			this.tlbNew = new System.Windows.Forms.ToolBarButton();
			this.cmnuNewZip = new System.Windows.Forms.ContextMenu();
			this.cmnuOpenMemory = new System.Windows.Forms.MenuItem();
			this.tlbOpen = new System.Windows.Forms.ToolBarButton();
			this.tlbAdd = new System.Windows.Forms.ToolBarButton();
			this.tlbRemove = new System.Windows.Forms.ToolBarButton();
			this.tlbExtractTo = new System.Windows.Forms.ToolBarButton();
			this.tlbAddComment = new System.Windows.Forms.ToolBarButton();
			this.tlbZipTest = new System.Windows.Forms.ToolBarButton();
			this.tlbSetPassword = new System.Windows.Forms.ToolBarButton();
			this.imglZipToolbar = new System.Windows.Forms.ImageList(this.components);
			this.splitter2 = new System.Windows.Forms.Splitter();
			this.txtComment = new System.Windows.Forms.TextBox();
			this.folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
			this.tabControl.SuspendLayout();
			this.tabCompressor.SuspendLayout();
			this.tabZStream.SuspendLayout();
			this.panel1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.tabCrc.SuspendLayout();
			this.tabZip.SuspendLayout();
			this.panelZipFiles.SuspendLayout();
			this.SuspendLayout();
			// 
			// mainMenu
			// 
			this.mainMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					 this.mnuFile,
																					 this.mnuCompression,
																					 this.mnuHelp});
			// 
			// mnuFile
			// 
			this.mnuFile.Index = 0;
			this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.mnuFileExit});
			this.mnuFile.Text = "&File";
			// 
			// mnuFileExit
			// 
			this.mnuFileExit.Index = 0;
			this.mnuFileExit.Text = "E&xit";
			this.mnuFileExit.Click += new System.EventHandler(this.mnuFileExit_Click);
			// 
			// mnuCompression
			// 
			this.mnuCompression.Index = 1;
			this.mnuCompression.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						   this.mnuCompressionLevel,
																						   this.mnuCompressionStrategy});
			this.mnuCompression.Text = "&Compression";
			// 
			// mnuCompressionLevel
			// 
			this.mnuCompressionLevel.Index = 0;
			this.mnuCompressionLevel.Text = "Level";
			// 
			// mnuCompressionStrategy
			// 
			this.mnuCompressionStrategy.Index = 1;
			this.mnuCompressionStrategy.Text = "Strategy";
			// 
			// mnuHelp
			// 
			this.mnuHelp.Index = 2;
			this.mnuHelp.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					this.mnuHelpAbout});
			this.mnuHelp.Text = "&Help";
			// 
			// mnuHelpAbout
			// 
			this.mnuHelpAbout.Index = 0;
			this.mnuHelpAbout.Text = "About...";
			this.mnuHelpAbout.Click += new System.EventHandler(this.mnuHelpAbout_Click);
			// 
			// statusBar
			// 
			this.statusBar.Location = new System.Drawing.Point(0, 355);
			this.statusBar.Name = "statusBar";
			this.statusBar.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						 this.statusBarPanel1,
																						 this.statusBarPanel2});
			this.statusBar.ShowPanels = true;
			this.statusBar.Size = new System.Drawing.Size(408, 22);
			this.statusBar.TabIndex = 2;
			this.statusBar.Layout += new System.Windows.Forms.LayoutEventHandler(this.StatusBarLayout);
			// 
			// statusBarPanel1
			// 
			this.statusBarPanel1.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Contents;
			this.statusBarPanel1.MinWidth = 150;
			this.statusBarPanel1.Width = 150;
			// 
			// statusBarPanel2
			// 
			this.statusBarPanel2.AutoSize = System.Windows.Forms.StatusBarPanelAutoSize.Spring;
			this.statusBarPanel2.BorderStyle = System.Windows.Forms.StatusBarPanelBorderStyle.None;
			this.statusBarPanel2.Width = 242;
			// 
			// tabControl
			// 
			this.tabControl.Controls.Add(this.tabCompressor);
			this.tabControl.Controls.Add(this.tabZStream);
			this.tabControl.Controls.Add(this.tabCrc);
			this.tabControl.Controls.Add(this.tabZip);
			this.tabControl.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tabControl.ItemSize = new System.Drawing.Size(52, 18);
			this.tabControl.Location = new System.Drawing.Point(0, 0);
			this.tabControl.Name = "tabControl";
			this.tabControl.SelectedIndex = 0;
			this.tabControl.Size = new System.Drawing.Size(408, 355);
			this.tabControl.TabIndex = 10;
			// 
			// tabCompressor
			// 
			this.tabCompressor.Controls.Add(this.label3);
			this.tabCompressor.Controls.Add(this.txtData);
			this.tabCompressor.Controls.Add(this.btnUncompress);
			this.tabCompressor.Controls.Add(this.btnCompress);
			this.tabCompressor.Location = new System.Drawing.Point(4, 22);
			this.tabCompressor.Name = "tabCompressor";
			this.tabCompressor.Size = new System.Drawing.Size(400, 329);
			this.tabCompressor.TabIndex = 1;
			this.tabCompressor.Text = "Compressor";
			this.tabCompressor.Visible = false;
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(8, 8);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(100, 16);
			this.label3.TabIndex = 7;
			this.label3.Text = "Data:";
			// 
			// txtData
			// 
			this.txtData.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtData.Location = new System.Drawing.Point(8, 24);
			this.txtData.Multiline = true;
			this.txtData.Name = "txtData";
			this.txtData.Size = new System.Drawing.Size(384, 264);
			this.txtData.TabIndex = 4;
			this.txtData.Text = "";
			// 
			// btnUncompress
			// 
			this.btnUncompress.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnUncompress.Location = new System.Drawing.Point(304, 296);
			this.btnUncompress.Name = "btnUncompress";
			this.btnUncompress.Size = new System.Drawing.Size(81, 23);
			this.btnUncompress.TabIndex = 6;
			this.btnUncompress.Text = "Uncompress";
			this.btnUncompress.Click += new System.EventHandler(this.btnUncompress_Click);
			// 
			// btnCompress
			// 
			this.btnCompress.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnCompress.Location = new System.Drawing.Point(216, 296);
			this.btnCompress.Name = "btnCompress";
			this.btnCompress.Size = new System.Drawing.Size(81, 23);
			this.btnCompress.TabIndex = 5;
			this.btnCompress.Text = "Compress";
			this.btnCompress.Click += new System.EventHandler(this.btnCompress_Click);
			// 
			// tabZStream
			// 
			this.tabZStream.Controls.Add(this.panel1);
			this.tabZStream.Controls.Add(this.splitter1);
			this.tabZStream.Controls.Add(this.dataGrid1);
			this.tabZStream.Location = new System.Drawing.Point(4, 22);
			this.tabZStream.Name = "tabZStream";
			this.tabZStream.Size = new System.Drawing.Size(400, 329);
			this.tabZStream.TabIndex = 0;
			this.tabZStream.Text = "ZStream";
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.txtOut);
			this.panel1.Controls.Add(this.btnSave);
			this.panel1.Controls.Add(this.btnLoad);
			this.panel1.Controls.Add(this.btnGenerate);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel1.Location = new System.Drawing.Point(0, 163);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(400, 166);
			this.panel1.TabIndex = 9;
			// 
			// txtOut
			// 
			this.txtOut.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.txtOut.Location = new System.Drawing.Point(0, 0);
			this.txtOut.Multiline = true;
			this.txtOut.Name = "txtOut";
			this.txtOut.ReadOnly = true;
			this.txtOut.Size = new System.Drawing.Size(400, 120);
			this.txtOut.TabIndex = 11;
			this.txtOut.TabStop = false;
			this.txtOut.Text = "";
			// 
			// btnSave
			// 
			this.btnSave.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnSave.Location = new System.Drawing.Point(224, 132);
			this.btnSave.Name = "btnSave";
			this.btnSave.Size = new System.Drawing.Size(81, 23);
			this.btnSave.TabIndex = 9;
			this.btnSave.Text = "Save";
			this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
			// 
			// btnLoad
			// 
			this.btnLoad.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnLoad.Location = new System.Drawing.Point(312, 132);
			this.btnLoad.Name = "btnLoad";
			this.btnLoad.Size = new System.Drawing.Size(81, 23);
			this.btnLoad.TabIndex = 10;
			this.btnLoad.Text = "Load";
			this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
			// 
			// btnGenerate
			// 
			this.btnGenerate.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.btnGenerate.Location = new System.Drawing.Point(136, 132);
			this.btnGenerate.Name = "btnGenerate";
			this.btnGenerate.Size = new System.Drawing.Size(81, 23);
			this.btnGenerate.TabIndex = 8;
			this.btnGenerate.Text = "Generate";
			this.btnGenerate.Click += new System.EventHandler(this.btnGenerate_Click);
			// 
			// splitter1
			// 
			this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
			this.splitter1.Location = new System.Drawing.Point(0, 160);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(400, 3);
			this.splitter1.TabIndex = 8;
			this.splitter1.TabStop = false;
			// 
			// dataGrid1
			// 
			this.dataGrid1.CaptionText = "Fibonacci\'s series";
			this.dataGrid1.DataMember = "";
			this.dataGrid1.Dock = System.Windows.Forms.DockStyle.Top;
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(0, 0);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(400, 160);
			this.dataGrid1.TabIndex = 4;
			// 
			// tabCrc
			// 
			this.tabCrc.Controls.Add(this.label1);
			this.tabCrc.Controls.Add(this.lblCrc);
			this.tabCrc.Controls.Add(this.lblAdler);
			this.tabCrc.Controls.Add(this.btnBrowseIn);
			this.tabCrc.Controls.Add(this.btnCrc);
			this.tabCrc.Controls.Add(this.txtFileIn);
			this.tabCrc.Controls.Add(this.btnAdler);
			this.tabCrc.Location = new System.Drawing.Point(4, 22);
			this.tabCrc.Name = "tabCrc";
			this.tabCrc.Size = new System.Drawing.Size(400, 329);
			this.tabCrc.TabIndex = 2;
			this.tabCrc.Text = "CRC";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(100, 16);
			this.label1.TabIndex = 6;
			this.label1.Text = "Input File:";
			// 
			// lblCrc
			// 
			this.lblCrc.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblCrc.Location = new System.Drawing.Point(16, 96);
			this.lblCrc.Name = "lblCrc";
			this.lblCrc.Size = new System.Drawing.Size(216, 16);
			this.lblCrc.TabIndex = 5;
			// 
			// lblAdler
			// 
			this.lblAdler.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblAdler.Location = new System.Drawing.Point(16, 64);
			this.lblAdler.Name = "lblAdler";
			this.lblAdler.Size = new System.Drawing.Size(216, 16);
			this.lblAdler.TabIndex = 4;
			// 
			// btnBrowseIn
			// 
			this.btnBrowseIn.Location = new System.Drawing.Point(296, 27);
			this.btnBrowseIn.Name = "btnBrowseIn";
			this.btnBrowseIn.Size = new System.Drawing.Size(16, 16);
			this.btnBrowseIn.TabIndex = 1;
			this.btnBrowseIn.Text = "...";
			this.btnBrowseIn.Click += new System.EventHandler(this.btnBrowseIn_Click);
			// 
			// btnCrc
			// 
			this.btnCrc.Location = new System.Drawing.Point(248, 93);
			this.btnCrc.Name = "btnCrc";
			this.btnCrc.Size = new System.Drawing.Size(81, 23);
			this.btnCrc.TabIndex = 3;
			this.btnCrc.Text = "CRC32";
			this.btnCrc.Click += new System.EventHandler(this.btnCrc_Click);
			// 
			// txtFileIn
			// 
			this.txtFileIn.Location = new System.Drawing.Point(16, 25);
			this.txtFileIn.Name = "txtFileIn";
			this.txtFileIn.Size = new System.Drawing.Size(272, 20);
			this.txtFileIn.TabIndex = 0;
			this.txtFileIn.Text = "";
			// 
			// btnAdler
			// 
			this.btnAdler.Location = new System.Drawing.Point(248, 61);
			this.btnAdler.Name = "btnAdler";
			this.btnAdler.Size = new System.Drawing.Size(81, 23);
			this.btnAdler.TabIndex = 2;
			this.btnAdler.Text = "Adler32";
			this.btnAdler.Click += new System.EventHandler(this.btnAdler_Click);
			// 
			// tabZip
			// 
			this.tabZip.Controls.Add(this.panelZipFiles);
			this.tabZip.Controls.Add(this.splitter2);
			this.tabZip.Controls.Add(this.txtComment);
			this.tabZip.Location = new System.Drawing.Point(4, 22);
			this.tabZip.Name = "tabZip";
			this.tabZip.Size = new System.Drawing.Size(400, 329);
			this.tabZip.TabIndex = 3;
			this.tabZip.Text = "Zip";
			// 
			// panelZipFiles
			// 
			this.panelZipFiles.Controls.Add(this.lvwZipFiles);
			this.panelZipFiles.Controls.Add(this.toolBarZip);
			this.panelZipFiles.Cursor = System.Windows.Forms.Cursors.Default;
			this.panelZipFiles.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panelZipFiles.Location = new System.Drawing.Point(0, 0);
			this.panelZipFiles.Name = "panelZipFiles";
			this.panelZipFiles.Size = new System.Drawing.Size(400, 262);
			this.panelZipFiles.TabIndex = 21;
			// 
			// lvwZipFiles
			// 
			this.lvwZipFiles.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						  this.clhZipName,
																						  this.clhZipSize,
																						  this.clhZipSizePacked,
																						  this.clhZipCompressRatio,
																						  this.clhZipDateModified,
																						  this.clhZipCRC32,
																						  this.clhZipComment});
			this.lvwZipFiles.ContextMenu = this.cmnuZipFiles;
			this.lvwZipFiles.Dock = System.Windows.Forms.DockStyle.Fill;
			this.lvwZipFiles.FullRowSelect = true;
			this.lvwZipFiles.Location = new System.Drawing.Point(0, 58);
			this.lvwZipFiles.Name = "lvwZipFiles";
			this.lvwZipFiles.Size = new System.Drawing.Size(400, 204);
			this.lvwZipFiles.TabIndex = 18;
			this.lvwZipFiles.View = System.Windows.Forms.View.Details;
			// 
			// clhZipName
			// 
			this.clhZipName.Text = "Name";
			this.clhZipName.Width = 180;
			// 
			// clhZipSize
			// 
			this.clhZipSize.Text = "Size";
			// 
			// clhZipSizePacked
			// 
			this.clhZipSizePacked.Text = "Packed";
			// 
			// clhZipCompressRatio
			// 
			this.clhZipCompressRatio.Text = "Ratio";
			// 
			// clhZipDateModified
			// 
			this.clhZipDateModified.Text = "Modified";
			this.clhZipDateModified.Width = 130;
			// 
			// clhZipCRC32
			// 
			this.clhZipCRC32.Text = "CRC32";
			this.clhZipCRC32.Width = 80;
			// 
			// clhZipComment
			// 
			this.clhZipComment.Text = "Comment";
			this.clhZipComment.Width = 160;
			// 
			// cmnuZipFiles
			// 
			this.cmnuZipFiles.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.cmnuZipFilesComment,
																						 this.menuItem4,
																						 this.cmnuZipFilesRemove,
																						 this.cmnuZipFilesTest});
			// 
			// cmnuZipFilesComment
			// 
			this.cmnuZipFilesComment.Index = 0;
			this.cmnuZipFilesComment.Text = "Comment";
			this.cmnuZipFilesComment.Click += new System.EventHandler(this.cmnuZipFilesComment_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 1;
			this.menuItem4.Text = "-";
			// 
			// cmnuZipFilesRemove
			// 
			this.cmnuZipFilesRemove.Index = 2;
			this.cmnuZipFilesRemove.Text = "Remove";
			this.cmnuZipFilesRemove.Click += new System.EventHandler(this.cmnuZipFilesRemove_Click);
			// 
			// cmnuZipFilesTest
			// 
			this.cmnuZipFilesTest.Index = 3;
			this.cmnuZipFilesTest.Text = "Test";
			this.cmnuZipFilesTest.Click += new System.EventHandler(this.cmnuZipFilesTest_Click);
			// 
			// toolBarZip
			// 
			this.toolBarZip.Appearance = System.Windows.Forms.ToolBarAppearance.Flat;
			this.toolBarZip.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						  this.tlbNew,
																						  this.tlbOpen,
																						  this.tlbAdd,
																						  this.tlbRemove,
																						  this.tlbExtractTo,
																						  this.tlbAddComment,
																						  this.tlbZipTest,
																						  this.tlbSetPassword});
			this.toolBarZip.ButtonSize = new System.Drawing.Size(32, 32);
			this.toolBarZip.DropDownArrows = true;
			this.toolBarZip.ImageList = this.imglZipToolbar;
			this.toolBarZip.Location = new System.Drawing.Point(0, 0);
			this.toolBarZip.Name = "toolBarZip";
			this.toolBarZip.ShowToolTips = true;
			this.toolBarZip.Size = new System.Drawing.Size(400, 58);
			this.toolBarZip.TabIndex = 17;
			this.toolBarZip.Wrappable = false;
			this.toolBarZip.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBarZip_ButtonClick);
			// 
			// tlbNew
			// 
			this.tlbNew.DropDownMenu = this.cmnuNewZip;
			this.tlbNew.ImageIndex = 0;
			this.tlbNew.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton;
			this.tlbNew.Text = "New";
			// 
			// cmnuNewZip
			// 
			this.cmnuNewZip.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					   this.cmnuOpenMemory});
			// 
			// cmnuOpenMemory
			// 
			this.cmnuOpenMemory.Index = 0;
			this.cmnuOpenMemory.RadioCheck = true;
			this.cmnuOpenMemory.Text = "Memory";
			this.cmnuOpenMemory.Click += new System.EventHandler(this.cmnuOpenMemory_Click);
			// 
			// tlbOpen
			// 
			this.tlbOpen.ImageIndex = 1;
			this.tlbOpen.Text = "Open";
			// 
			// tlbAdd
			// 
			this.tlbAdd.ImageIndex = 2;
			this.tlbAdd.Text = "Add";
			// 
			// tlbRemove
			// 
			this.tlbRemove.ImageIndex = 3;
			this.tlbRemove.Text = "Remove";
			// 
			// tlbExtractTo
			// 
			this.tlbExtractTo.ImageIndex = 4;
			this.tlbExtractTo.Text = "ExtractTo";
			// 
			// tlbAddComment
			// 
			this.tlbAddComment.ImageIndex = 7;
			this.tlbAddComment.Text = "Comment";
			// 
			// tlbZipTest
			// 
			this.tlbZipTest.ImageIndex = 5;
			this.tlbZipTest.Text = "Test";
			// 
			// tlbSetPassword
			// 
			this.tlbSetPassword.ImageIndex = 6;
			this.tlbSetPassword.Text = "Password";
			this.tlbSetPassword.Visible = false;
			// 
			// imglZipToolbar
			// 
			this.imglZipToolbar.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit;
			this.imglZipToolbar.ImageSize = new System.Drawing.Size(32, 32);
			this.imglZipToolbar.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imglZipToolbar.ImageStream")));
			this.imglZipToolbar.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// splitter2
			// 
			this.splitter2.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.splitter2.Location = new System.Drawing.Point(0, 262);
			this.splitter2.Name = "splitter2";
			this.splitter2.Size = new System.Drawing.Size(400, 3);
			this.splitter2.TabIndex = 20;
			this.splitter2.TabStop = false;
			// 
			// txtComment
			// 
			this.txtComment.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.txtComment.Location = new System.Drawing.Point(0, 265);
			this.txtComment.Multiline = true;
			this.txtComment.Name = "txtComment";
			this.txtComment.ReadOnly = true;
			this.txtComment.Size = new System.Drawing.Size(400, 64);
			this.txtComment.TabIndex = 19;
			this.txtComment.Text = "";
			this.txtComment.Visible = false;
			// 
			// ZTest
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(408, 377);
			this.Controls.Add(this.tabControl);
			this.Controls.Add(this.statusBar);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Menu = this.mainMenu;
			this.MinimumSize = new System.Drawing.Size(360, 390);
			this.Name = "ZTest";
			this.Text = "ZTest";
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
			this.tabControl.ResumeLayout(false);
			this.tabCompressor.ResumeLayout(false);
			this.tabZStream.ResumeLayout(false);
			this.panel1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.tabCrc.ResumeLayout(false);
			this.tabZip.ResumeLayout(false);
			this.panelZipFiles.ResumeLayout(false);
			this.ResumeLayout(false);

		}

		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>

		[STAThread]

		static void Main() 
		{
			Application.Run(new ZTest());
		}

		#region Helper Functions

		private float CompressRatio(long sizeUncompressed, long sizeCompressed)
		{
			if(sizeUncompressed > 0)
			{
				return ((float)(sizeUncompressed - sizeCompressed) * 100) / sizeUncompressed;
			}
			return 0;
		}

		#endregion

		#region Menu Event Handlers
		private void mnuFileExit_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void mnuHelpAbout_Click(object sender, System.EventArgs e)
		{
			About dlg = new About();
			dlg.ShowDialog();
		}

		private void OnCompressionLevelClick(object sender, EventArgs args)
		{
			mnuLevel.Checked = false;
			mnuLevel = (MenuItem) sender;
			mnuLevel.Checked = true;

			UpdateStatusBar();
		}

		private void OnCompressionStrategyClick(object sender, EventArgs args)
		{
			mnuStrategy.Checked = false;
			mnuStrategy = (MenuItem) sender;
			mnuStrategy.Checked = true;

			UpdateStatusBar();
		}
		#endregion

		#region Status Bar
		private void StatusBarLayout(object sender, LayoutEventArgs e)
		{
			if(_progressBar != null)
			{
				if(statusBar.Panels.Count >= 2)
				{
					Rectangle bounds = new Rectangle(new Point(statusBar.Panels[0].Width, 1), new Size(statusBar.Panels[1].Width, statusBar.Height));
					bounds.Inflate(-2, -1);
					_progressBar.Bounds = bounds;
				}
			}
		}

		private void UpdateStatusBar()
		{
			StatusBarPanel panel = statusBar.Panels[0];
			panel.Text = "Level: " + mnuLevel.Text + " - Strategy: " + mnuStrategy.Text;
			_progressBar.Value = 0;
		}
		#endregion

		#region ZStream Test
		private void btnGenerate_Click(object sender, System.EventArgs e)
		{
			if(tabControl.SelectedTab.Text.Equals("ZStream"))
				GenerateFibonacciData();
		}

		private void btnLoad_Click(object sender, System.EventArgs e)
		{	
			txtOut.Clear();
			txtOut.AppendText("Deserializing data from file " + _sFibonacci + " ..." + Environment.NewLine);

			string sFileName = Application.StartupPath + @"\" + _sFibonacci;

			FileStream fs = null;
			Stream decompressor = null;

			try
			{
				fs = new FileStream(sFileName, FileMode.Open);

				CRC32 sum = new CRC32();
				decompressor = new ZStream(fs, false, 15, sum);

				BinaryFormatter bf = new BinaryFormatter();
				FibonacciDataSet ds = (FibonacciDataSet) bf.Deserialize(decompressor);
				decompressor.Close();

				dataGrid1.DataSource = ds;
				dataGrid1.DataMember = "Fibonacci";

				UpdateStatistics(decompressor);
			}
			catch(Exception err)
			{
				txtOut.AppendText(err.Message);
			}
			finally
			{
				if(null != fs)
					fs.Close();
			}
		}

		private void btnSave_Click(object sender, System.EventArgs e)
		{
			FibonacciDataSet ds = (FibonacciDataSet) dataGrid1.DataSource;

			if(ds == null)
			{
				ds = (FibonacciDataSet) GenerateFibonacciData();
			}

			txtOut.Clear();
			txtOut.AppendText("Serializing data to file " + _sFibonacci + " ..." + Environment.NewLine);

			string sFileName = Application.StartupPath + @"\" + _sFibonacci;

			FileStream fs = null;
			Stream compressor = null;

			try
			{
				fs = new FileStream(sFileName, FileMode.Create, FileAccess.Write, FileShare.None);
				CRC32 sum = new CRC32();
				compressor = new ZStream(fs, 
					(CompressionLevel) Enum.Parse(typeof(CompressionLevel), mnuLevel.Text), 
					(CompressionStrategy) Enum.Parse(typeof(CompressionStrategy), mnuStrategy.Text), 15, sum);

				BinaryFormatter bf = new BinaryFormatter();
				bf.Serialize(compressor, ds);
				compressor.Close();

				UpdateStatistics(compressor);
			}
			catch(Exception err)
			{
				txtOut.AppendText(err.Message);
			}
			finally
			{
				if(null != fs)
					fs.Close();
			}
		}

		private void UpdateStatistics(Stream stream)
		{
			ZStream zstream = stream as ZStream;

			if(zstream != null)
			{
				int nChk = zstream.Checksum;
				long nSizeCompressed = zstream.CompressedLength;
				long nSizeUncompressed = zstream.UncompressedLength;
				float fRatio = CompressRatio(nSizeUncompressed, nSizeCompressed);

				txtOut.AppendText(String.Format("\r\nChecksum {0:X}\r\nSize {1} Bytes\r\nCompressed Size {2} Bytes\r\nCompression Ratio {3}%", 
					nChk, nSizeUncompressed, nSizeCompressed, fRatio));
			}
		}

		private DataSet GenerateFibonacciData()
		{
			txtOut.Clear();

			dataGrid1.DataSource = null;
			FibonacciDataSet ds = new FibonacciDataSet();

			txtOut.AppendText("Creating Fibonacci series data..." + Environment.NewLine);
			for(int x = 1; x <= 1000; x++) 
			{
				
				double n = Fibonacci(x);
				ds.Fibonacci.AddFibonacciRow(x, n, _FN1/_FN2);
			}

			dataGrid1.DataSource = ds;
			dataGrid1.DataMember = "Fibonacci";

			txtOut.AppendText("Done.");

			return ds;
		}

		private double Fibonacci(double n)
		{
			if(n < 1)
				return 0;
			if(n == 1 || n == 2)
			{
				_FN1 = _FN2 = 1;
				return 1;
			}

			double r = _FN1 + _FN2;
			
			_FN2 = _FN1;
			_FN1 = r;

			return r;
		}
		#endregion

		#region ZCompressor Test

		private void btnCompress_Click(object sender, System.EventArgs e)
		{
			string sData = txtData.Text;

			Encoding encoder = Encoding.UTF7;
			
			try
			{
				byte [] buffer = encoder.GetBytes(sData);

				// Compress the buffer
				_compressedData = ZCompressor.Compress(buffer, 
					(CompressionLevel) Enum.Parse(typeof(CompressionLevel), mnuLevel.Text));

				txtData.Text = encoder.GetString(_compressedData);
				
			}
			catch(ZException err)
			{
				txtData.Text = err.Message;
			}
		}

		private void btnUncompress_Click(object sender, System.EventArgs e)
		{
			if(_compressedData == null)
				return;

			try
			{
				string sData = txtData.Text;
				Encoding encoder = Encoding.UTF7;
				Byte [] uncompressed = ZCompressor.Uncompress(_compressedData);
			
				txtData.Text = encoder.GetString(uncompressed);
			}
			catch(ZException err)
			{
				txtData.Text = err.Message;
			}
		}
		#endregion

		#region CRC Test

		private void btnBrowseIn_Click(object sender, System.EventArgs e)
		{
			txtFileIn.Text = Common.BrowseForFile("Choose file", "All files (*.*)|*.*");
		}

		private void btnAdler_Click(object sender, System.EventArgs e)
		{
			Adler32 crc = new Adler32();
			DoChecksum(txtFileIn.Text, crc);
			lblAdler.Text = crc.Checksum.ToString("X");
		}

		private void btnCrc_Click(object sender, System.EventArgs e)
		{
			CRC32 crc = new CRC32();
			DoChecksum(txtFileIn.Text, crc);
			lblCrc.Text = crc.Checksum.ToString("X");
		}

		private long DoChecksum(string sFile, IChecksum chk)
		{
			FileStream fs = null;
			uint checksum = 0;

			try
			{
				Byte [] data = new Byte[32768];

				fs = new FileStream(sFile, FileMode.Open);
				
				int nRead = fs.Read(data, 0, 32768);
				
				while(nRead > 0)
				{
					checksum = chk.Update(data, 0, nRead);
					nRead = fs.Read(data, 0, 32768);
				}
			}
			catch(Exception err)
			{
				MessageBox.Show(err.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			finally
			{
				if(null != fs)
					fs.Close();
			}

			return checksum;
		}
		#endregion

		#region Zip Test

		private void toolBarZip_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{
			ToolBar toolbar = (ToolBar) sender;
			_totalFilesFound = 0;

			switch(e.Button.Text)
			{
				case "New":
					if(null != _archive)
					{
						NeedSave();
						_archive.Dispose();
					}
					_archive = OnNewZipArchive();
					break;

				case "Open":
					if(null != _archive)
					{
						NeedSave();
						_archive.Dispose();
					}
					_archive = OnOpenZipArchive();
					break;
				
				case "Add":
					OnAddZipArchive(_archive);
					break;

				case "Remove":
					OnRemoveZipArchive(_archive);
					break;

				case "ExtractTo":
					OnExtractToZipArchive(_archive);
					break;
				
				case "Test":
					OnTestZipArchive(_archive);
					break;

				case "Comment":
					OnCommentZipArchive(_archive);
					break;

				default:
					break;
			}
		}

		private void NeedSave()
		{
			Stream zipStream = _archive.ArchiveStream;
			if(zipStream.GetType() == typeof(MemoryStream))
			{
				DialogResult dlgRes = MessageBox.Show("Do you want save Memory Zip archive to file?", 
					"Save Memory Zip Archive", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
				
				if(DialogResult.Yes == dlgRes)
				{
					SaveFileDialog dlg = new SaveFileDialog();
					dlg.Filter = "zip files (*.zip)|*.zip|All files (*.*)|*.*";
					if(DialogResult.OK == dlg.ShowDialog())
					{
						using(FileStream file = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write))
						{
							StreamUtilities.StreamCopy(zipStream, 0, file);
						}
					}
				}
			}
		}
		private ZipArchive OnNewZipArchive()
		{
			ZipArchive archive = null;

			try
			{
				if(_isMmoryZip)
				{
					MemoryStream zipStream = new MemoryStream();
					archive = new ZipArchive(zipStream);
				}
				else
				{
					string sFile = Common.BrowseForFile("New zip file", "zip files (*.zip)|*.zip|All files (*.*)|*.*");
			
					if(0 == sFile.Length)
						return null;

					archive = new ZipArchive(sFile, FileMode.CreateNew, FileAccess.ReadWrite);
				}

				archive.Progress += new ZipProgressEventHandler(OnZipProgress);
				archive.Filter += new ZipFilterEventHandler(OnZipFilter);
				archive.Fail += new ZipFailEventHandler(OnZipFail);
			}
			catch(Exception e)
			{
				MessageBox.Show(e.Message, "New Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

			UpdateCommentView(archive);
			UpdateArchiveListView(archive);
			UpdateStatusBar();

			return archive;
		}
		private ZipArchive OnOpenZipArchive()
		{
			string sFile = Common.BrowseForFile("Open zip file", "zip files (*.zip)|*.zip|All files (*.*)|*.*");
			
			if(0 == sFile.Length)
				return null;

			ZipArchive archive = null;
			try
			{
				archive = new ZipArchive(sFile, FileMode.Open, FileAccess.ReadWrite);
			
				archive.Progress += new ZipProgressEventHandler(OnZipProgress);
				archive.Filter += new ZipFilterEventHandler(OnZipFilter);
				archive.Fail += new ZipFailEventHandler(OnZipFail);
			}
			catch(Exception e)
			{
				MessageBox.Show(e.Message, "Open Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

			UpdateCommentView(archive);
			UpdateArchiveListView(archive);
			UpdateStatusBar();

			return archive;
		}

		private void OnAddZipArchive(ZipArchive archive)
		{
			if(archive == null)
				return;

			ZipAdd dlg = new ZipAdd();
			
			dlg.OverwriteExisting = archive.OverwriteExisting;
			dlg.Recursive = true;

			DialogResult res = dlg.ShowDialog();
			if(res == DialogResult.Cancel)
				return;

			archive.DefaultCompressionLevel = (CompressionLevel) Enum.Parse(typeof(CompressionLevel), mnuLevel.Text);
			archive.OverwriteExisting = dlg.OverwriteExisting;
			archive.StoreFilePaths = dlg.StoreFilePath;
			archive.IncludeFilter = dlg.IncludeFilter;
			archive.ExcludeFilter = dlg.ExcludeFilter;
	
			if(dlg.AddFiles.Length > 0)
			{
				try
				{
					archive.Add(dlg.AddFiles, archive.DefaultCompressionLevel, dlg.Recursive);
				}
				catch(ZipException e)
				{
					MessageBox.Show(e.Message, "Add Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}

				UpdateArchiveListView(archive);
			}
		}

		private void OnRemoveZipArchive(ZipArchive archive)
		{
			if(archive == null)
				return;

			foreach(ListViewItem lvi in lvwZipFiles.SelectedItems)
			{
				ZipEntry entry = (ZipEntry) lvi.Tag;
				try
				{
					archive.Remove(entry.FileName, true);
				}
				catch(ZipException e)
				{
					MessageBox.Show(e.Message, "Remove Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}

			UpdateArchiveListView(archive);
		}

		private void OnExtractToZipArchive(ZipArchive archive)
		{
			if(archive == null)
				return;

			if(folderBrowserDialog.ShowDialog() == DialogResult.OK)
			{
				try
				{
					archive.OverwriteExisting = true;
					archive.BuildTree = true;
					archive.ExtractTo(folderBrowserDialog.SelectedPath);
				}
				catch(ZipException e)
				{
					MessageBox.Show(e.Message, "ExtractTo Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		private void OnTestZipArchive(ZipArchive archive)
		{
			if(archive == null)
				return;

			bool bTest = false;
			try
			{
				bTest = archive.Test();
			}
			catch(ZipException e)
			{					
				MessageBox.Show(e.Message, "ExtractTo Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

			if(bTest)
			{
				MessageBox.Show("Test successfully completed.",
					"Test", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			else
			{
				MessageBox.Show("Some error were found during test operation, the archive may be corrupted.", 
					"Test", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}
		
		private void OnCommentZipArchive(ZipArchive archive)
		{
			if(archive == null)
				return;

			ZipComment dlg = new ZipComment();
			dlg.Comment = archive.Comment;

			if(dlg.ShowDialog() == DialogResult.OK)
			{
				archive.Comment = dlg.Comment;
				UpdateCommentView(archive);
			}
		}

		private void OnZipProgress(object sender, ZipProgressEventArgs args)
		{
			_progressBar.Value = (int) (args.Percent);
		}

		private void OnZipFilter(object sender, ZipFilterEventArgs args)
		{
			_totalFilesFound += args.FilesFound;
			statusBarPanel1.Text = "Found " + _totalFilesFound.ToString() + " file(s)";
			statusBar.Update();
		}
		private void OnZipFail(object sender, ZipFailEventArgs args)
		{
			args.ThrowException = false;
			DialogResult res = MessageBox.Show(
				args.Exception.Message, "Error: ", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation
			);

			switch(res)
			{
				case DialogResult.Abort:
					args.RecoveryAction = ZipRecoveryAction.Abort;
					break;
				case DialogResult.Retry:
					args.RecoveryAction = ZipRecoveryAction.Retry;
					break;
				case DialogResult.Ignore:
					args.RecoveryAction = ZipRecoveryAction.Ignore;
					break;
				default:
					break;
			}
		}

		private void UpdateArchiveListView(ZipArchive archive)
		{
			lvwZipFiles.Items.Clear();

			if(null == archive)
				return;

			lvwZipFiles.BeginUpdate();

			foreach(ZipEntry zf in archive.Entries)
			{
				ListViewItem lvi = new ListViewItem(zf.FileName);
				lvi.Tag = zf;

				
				if(zf.IsDirectory)
				{
					lvi.SubItems.AddRange(new string [] { "", "", "" });
				}
				else
				{
					lvi.SubItems.Add(zf.UncompressedSize.ToString());
					lvi.SubItems.Add(zf.CompressedSize.ToString());
					float ratio = CompressRatio(zf.UncompressedSize, zf.CompressedSize);
					lvi.SubItems.Add(String.Format("{0:G3}%", ratio));
				}
				lvi.SubItems.Add(zf.LastModifiedDateTime.ToString());
				lvi.SubItems.Add(zf.CRC32.ToString("X"));
				lvi.SubItems.Add(zf.Comment);
				lvwZipFiles.Items.Add(lvi);
			}

			lvwZipFiles.EndUpdate();

			statusBarPanel1.Text = "";
			statusBar.Update();

		}

		private void UpdateCommentView(ZipArchive archive)
		{
			if(null == archive)
				return;

			txtComment.Visible = archive.Comment.Length > 0;
			if(txtComment.Visible)
			{
				txtComment.Text = archive.Comment;
			}
		}

		private void cmnuZipFilesComment_Click(object sender, System.EventArgs e)
		{
			if(null == _archive)
				return;

			ZipComment dlg = new ZipComment();
			if(dlg.ShowDialog() == DialogResult.OK)
			{
				if(lvwZipFiles.SelectedItems.Count > 0)
				{
					try
					{
						foreach(ListViewItem item in lvwZipFiles.SelectedItems)
						{
							ZipEntry entry = (ZipEntry) item.Tag;
							entry.Comment = dlg.Comment;
						}
					}
					catch(ZipException ex)
					{
						MessageBox.Show(ex.Message, "set comment error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}

					UpdateArchiveListView(_archive);
				}
			}
		}

		private void cmnuZipFilesRemove_Click(object sender, System.EventArgs e)
		{
			if(null == _archive)
				return;

			if(lvwZipFiles.SelectedItems.Count > 0)
			{
				try
				{
					foreach(ListViewItem item in lvwZipFiles.SelectedItems)
					{
						ZipEntry entry = (ZipEntry) item.Tag;
						_archive.Entries.Remove(entry);
					}
				}
				catch(ZipException ex)
				{
					MessageBox.Show(ex.Message, "remove error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}

				UpdateArchiveListView(_archive);
			}
		}

		private void cmnuZipFilesTest_Click(object sender, System.EventArgs e)
		{
			if(null == _archive)
				return;

			if(lvwZipFiles.SelectedItems.Count > 0)
			{
				try
				{
					bool bTest = false;

					foreach(ListViewItem item in lvwZipFiles.SelectedItems)
					{
						ZipEntry entry = (ZipEntry) item.Tag;
						if(! (bTest = _archive.Entries.Test(entry)))
							break;
					}

					if(bTest)
					{
						MessageBox.Show("Test successfully completed.", "Test",
							MessageBoxButtons.OK, MessageBoxIcon.Information);
					}
				}
				catch(ZipException ex)
				{					
					MessageBox.Show(ex.Message, "ExtractTo Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		private void cmnuOpenMemory_Click(object sender, System.EventArgs e)
		{
			MenuItem item = sender as MenuItem;
			_isMmoryZip = item.Checked = !item.Checked;
		}
		#endregion

	}
}











































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 (Senior)
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions