"SfR Fresh" - the SfR Freeware/Shareware Archive

Member "csharp/zip/Form1.cs" of archive zip232dN.zip:


As a special service "SfR Fresh" has tried to format the requested source page into HTML format using source code syntax highlighting with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. That can be also achieved for any archive member file by clicking within an archive contents listing on the first character of the file(path) respectively on the according byte size field.
    1 #region README
    2 
    3 	//_____________________________________________________________________________
    4 	//
    5 	//Sample C# code, .NET Framework 1.1, contributed to the Info-Zip project by
    6 	//Adrian Maull, April 2005.
    7 	//
    8 	//If you have questions or comments, contact me at adrian.maull@sprintpcs.com.  Though
    9 	//I will try to respond to coments/questions, I do not guarantee such response.
   10 	//
   11 	//THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
   12 	//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
   13 	//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
   14 	//PARTICULAR PURPOSE.
   15 	//
   16 	//_____________________________________________________________________________
   17 
   18 
   19 #endregion
   20 
   21 using System;
   22 using System.Drawing;
   23 using System.Collections;
   24 using System.ComponentModel;
   25 using System.Windows.Forms;
   26 using System.Data;
   27 
   28 namespace CSharpInfoZip_ZipSample
   29 {
   30 	/// <summary>
   31 	/// Summary description for Form1.
   32 	/// </summary>
   33 	public class Form1 : System.Windows.Forms.Form
   34 	{
   35 		private System.Windows.Forms.Button btnSimpleZip;
   36 		private System.Windows.Forms.Label label1;
   37 		private System.Windows.Forms.TextBox textBox1;
   38 		private System.Windows.Forms.TextBox textBox2;
   39 		private System.Windows.Forms.Label label2;
   40 		private System.Windows.Forms.Button btnZipRecurse;
   41 		private System.Windows.Forms.Button btnStop;
   42 
   43 		//Define the Zip object
   44 		private Zip m_ZipObj;
   45 
   46 		/// <summary>
   47 		/// Required designer variable.
   48 		/// </summary>
   49 		private System.ComponentModel.Container components = null;
   50 
   51 		public Form1()
   52 		{
   53 			//
   54 			// Required for Windows Form Designer support
   55 			//
   56 			InitializeComponent();
   57 
   58 			//
   59 			// TODO: Add any constructor code after InitializeComponent call
   60 			//
   61 		}
   62 
   63 		/// <summary>
   64 		/// Clean up any resources being used.
   65 		/// </summary>
   66 		protected override void Dispose( bool disposing )
   67 		{
   68 			if( disposing )
   69 			{
   70 				if (components != null)
   71 				{
   72 					components.Dispose();
   73 				}
   74 			}
   75 			base.Dispose( disposing );
   76 		}
   77 
   78 		#region Windows Form Designer generated code
   79 		/// <summary>
   80 		/// Required method for Designer support - do not modify
   81 		/// the contents of this method with the code editor.
   82 		/// </summary>
   83 		private void InitializeComponent()
   84 		{
   85 			this.btnSimpleZip = new System.Windows.Forms.Button();
   86 			this.label1 = new System.Windows.Forms.Label();
   87 			this.textBox1 = new System.Windows.Forms.TextBox();
   88 			this.textBox2 = new System.Windows.Forms.TextBox();
   89 			this.label2 = new System.Windows.Forms.Label();
   90 			this.btnZipRecurse = new System.Windows.Forms.Button();
   91 			this.btnStop = new System.Windows.Forms.Button();
   92 			this.SuspendLayout();
   93 			//
   94 			// btnSimpleZip
   95 			//
   96 			this.btnSimpleZip.Location = new System.Drawing.Point(8, 16);
   97 			this.btnSimpleZip.Name = "btnSimpleZip";
   98 			this.btnSimpleZip.TabIndex = 0;
   99 			this.btnSimpleZip.Text = "Simple Zip";
  100 			this.btnSimpleZip.Click += new System.EventHandler(this.btnSimpleZip_Click);
  101 			//
  102 			// label1
  103 			//
  104 			this.label1.Location = new System.Drawing.Point(8, 64);
  105 			this.label1.Name = "label1";
  106 			this.label1.Size = new System.Drawing.Size(168, 16);
  107 			this.label1.TabIndex = 1;
  108 			this.label1.Text = "Zip DLL print callback message:";
  109 			//
  110 			// textBox1
  111 			//
  112 			this.textBox1.Location = new System.Drawing.Point(8, 80);
  113 			this.textBox1.Multiline = true;
  114 			this.textBox1.Name = "textBox1";
  115 			this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
  116 			this.textBox1.Size = new System.Drawing.Size(456, 88);
  117 			this.textBox1.TabIndex = 2;
  118 			this.textBox1.Text = "";
  119 			//
  120 			// textBox2
  121 			//
  122 			this.textBox2.Location = new System.Drawing.Point(8, 200);
  123 			this.textBox2.Multiline = true;
  124 			this.textBox2.Name = "textBox2";
  125 			this.textBox2.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
  126 			this.textBox2.Size = new System.Drawing.Size(456, 88);
  127 			this.textBox2.TabIndex = 4;
  128 			this.textBox2.Text = "";
  129 			//
  130 			// label2
  131 			//
  132 			this.label2.Location = new System.Drawing.Point(8, 184);
  133 			this.label2.Name = "label2";
  134 			this.label2.Size = new System.Drawing.Size(184, 16);
  135 			this.label2.TabIndex = 3;
  136 			this.label2.Text = "Zip DLL service callback message:";
  137 			//
  138 			// btnZipRecurse
  139 			//
  140 			this.btnZipRecurse.Location = new System.Drawing.Point(96, 16);
  141 			this.btnZipRecurse.Name = "btnZipRecurse";
  142 			this.btnZipRecurse.Size = new System.Drawing.Size(128, 23);
  143 			this.btnZipRecurse.TabIndex = 5;
  144 			this.btnZipRecurse.Text = "Zip a folder recursively";
  145 			this.btnZipRecurse.Click += new System.EventHandler(this.btnZipRecurse_Click);
  146 			//
  147 			// btnStop
  148 			//
  149 			this.btnStop.Location = new System.Drawing.Point(392, 16);
  150 			this.btnStop.Name = "btnStop";
  151 			this.btnStop.TabIndex = 6;
  152 			this.btnStop.Text = "Stop";
  153 			this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
  154 			//
  155 			// Form1
  156 			//
  157 			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
  158 			this.ClientSize = new System.Drawing.Size(474, 304);
  159 			this.Controls.Add(this.btnStop);
  160 			this.Controls.Add(this.btnZipRecurse);
  161 			this.Controls.Add(this.textBox2);
  162 			this.Controls.Add(this.textBox1);
  163 			this.Controls.Add(this.label2);
  164 			this.Controls.Add(this.label1);
  165 			this.Controls.Add(this.btnSimpleZip);
  166 			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
  167 			this.MaximizeBox = false;
  168 			this.MinimizeBox = false;
  169 			this.Name = "Form1";
  170 			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
  171 			this.Text = "Form1";
  172 			this.Load += new System.EventHandler(this.Form1_Load);
  173 			this.ResumeLayout(false);
  174 
  175 		}
  176 		#endregion
  177 
  178 		/// <summary>
  179 		/// The main entry point for the application.
  180 		/// </summary>
  181 		[STAThread]
  182 		static void Main()
  183 		{
  184 			Application.Run(new Form1());
  185 		}
  186 
  187 
  188 		#region Event Handlers
  189 
  190 		private void Form1_Load(object sender, System.EventArgs e)
  191 		{
  192 			btnStop.Enabled = false;
  193 		}
  194 
  195 		private void btnSimpleZip_Click(object sender, System.EventArgs e)
  196 		{
  197 
  198 			textBox1.Text = string.Empty;
  199 			textBox2.Text = string.Empty;
  200 
  201 			//Set up the files array
  202 			string [] files = new string[2];
  203 
  204 			//NOTE:
  205 			//Change these file names to whatever is appropriate
  206 			//on your system
  207 			files[0] = @"c:\tmp\testzipfile1.rtf";
  208 			files[1] = @"c:\tmp\testzipfile2.rtf";
  209 
  210 			//Instantiate the Zip object
  211 			m_ZipObj = new Zip();
  212 
  213 			//Set the Zip object properties
  214 			m_ZipObj.FilesToZip = files;
  215 			m_ZipObj.Verbose = VerboseMessagesEnum.True;
  216 			m_ZipObj.Level = CompressionLevelEnum.Level6;
  217 
  218 			//This is optional.  This is how to add a comment
  219 			m_ZipObj.Comment = "Test Comment";
  220 			m_ZipObj.CommentOption = CommentEnum.True;
  221 
  222 			//NOTE:
  223 			//File name of the resulting zip file.  Change this as appropriate
  224 			m_ZipObj.ZipFileName = @"c:\tmp\zip\csharpzip.zip";
  225 
  226 			//Wire the event handlers to receive the events from the Zip class
  227 			m_ZipObj.ReceivePrintMessage +=new ZipDLLPrintMessageEventHandler(zipObj_ReceivePrintMessage);
  228 			m_ZipObj.ReceiveServiceMessage +=new ZipDLLServiceMessageEventHandler(zipObj_ReceiveServiceMessage);
  229 
  230 			btnStop.Enabled = true;
  231 
  232 			//Zip the files
  233 			int ret = m_ZipObj.ZipFiles();
  234 
  235 			//Examine the return code
  236 			MessageBox.Show("Done.  Return Code: " + ret.ToString());
  237 		}
  238 
  239 
  240 
  241 		private void btnZipRecurse_Click(object sender, System.EventArgs e)
  242 		{
  243 			//NOTE:
  244 			//Consult the zip32 limits provided in the documentation
  245 
  246 			textBox1.Text = string.Empty;
  247 			textBox2.Text = string.Empty;
  248 
  249 			//Instantiate the Zip object
  250 			m_ZipObj = new Zip();
  251 
  252 			//_____________________________________________________________________________
  253 			//WORK AROUND:
  254 			//The InfoZip documentation states that the zip32.dll can recurse directories if the -r or -R flag is specified.
  255 			//In code this is specified by setting the fRecurse flag of the ZPOPT structure to 1 (-r) or 2 (-R).
  256 			//However, in my test, when I specified either of the recurse flags I frequently received errors coming
  257 			//back from the ZpArchive function.  My work around is to recurse the specified directories and prepare
  258 			//an array of file names and pass that array to zip32.  Everything seems to work OK if I do this.
  259 
  260 			//If you want to try the recurse option, below is an example of how to do it.
  261 
  262 //			string [] files = new string[1];
  263 //
  264 //			//NOTE:
  265 //			//Change this to whatever is appropriate
  266 //			System.IO.Directory.SetCurrentDirectory(@"C:\TmpTest");
  267 //
  268 //			//Specify the file mask you want to use.  Consult the zip32.dll documentation for the
  269 //			//-r and -R options
  270 //			files[0] = @"*.*";
  271 //
  272 //			m_ZipObj.FilesToZip = files;
  273 //			m_ZipObj.Recurse = RecurseEnum.Level2;
  274 //			m_ZipObj.Verbose = VerboseMessagesEnum.True;
  275 //			m_ZipObj.Level = CompressionLevelEnum.Level6;
  276 //
  277 //			//NOTE:
  278 //			//File name of the resulting zip file.  Change this as appropriate
  279 //			m_ZipObj.ZipFileName = @"c:\tmp\zip\csharprecurse.zip";
  280 //
  281 //			//Wire the event handlers to receive the events from the Zip class
  282 //			m_ZipObj.ReceivePrintMessage +=new ZipDLLPrintMessageEventHandler(zipObj_ReceivePrintMessage);
  283 //			m_ZipObj.ReceiveServiceMessage +=new ZipDLLServiceMessageEventHandler(zipObj_ReceiveServiceMessage);
  284 //
  285 //			btnStop.Enabled = true;
  286 //
  287 //			//Zip the files
  288 //			int ret = m_ZipObj.ZipFiles();
  289 //
  290 //			//Examine the return code
  291 //			MessageBox.Show("Done. Ret: " + ret.ToString());
  292 
  293 			//_____________________________________________________________________________
  294 			//Prepare an array of all the files in the directory
  295 
  296 			//NOTE:
  297 			//Change this to whatever is appropriate
  298 			string root = @"C:\Tmp\TestZip";
  299 
  300 			ArrayList fileList = new ArrayList();
  301 			RecurseGetDirsAndFiles(root, fileList);
  302 			fileList.TrimToSize();
  303 
  304 			//Build the files array.  Practically, the .NET limit here is your RAM
  305 			string [] files = new string[fileList.Count];
  306 			int idx = 0;
  307 
  308 			IEnumerator en = fileList.GetEnumerator();
  309 			while ( en.MoveNext() )
  310 			{
  311 				files[idx] = en.Current.ToString();
  312 				idx++;
  313 			}
  314 
  315 			//Set the Zip object properties
  316 			m_ZipObj.FilesToZip = files;
  317 			m_ZipObj.Verbose = VerboseMessagesEnum.True;
  318 			m_ZipObj.Level = CompressionLevelEnum.Level6;
  319 
  320 			//This is optional.  This is how to add a comment
  321 			m_ZipObj.Comment = "Test Comment";
  322 			m_ZipObj.CommentOption = CommentEnum.True;
  323 
  324 			//NOTE:
  325 			//File name of the resulting zip file.  Change this as appropriate
  326 			m_ZipObj.ZipFileName = @"c:\tmp\zip\csharpzip2.zip";
  327 
  328 			//Wire the event handlers to receive the events from the Zip class
  329 			m_ZipObj.ReceivePrintMessage +=new ZipDLLPrintMessageEventHandler(zipObj_ReceivePrintMessage);
  330 			m_ZipObj.ReceiveServiceMessage +=new ZipDLLServiceMessageEventHandler(zipObj_ReceiveServiceMessage);
  331 
  332 			btnStop.Enabled = true;
  333 
  334 			//Zip the files
  335 			int ret = m_ZipObj.ZipFiles();
  336 
  337 			//Examine the return code
  338 			MessageBox.Show("Done. Ret: " + ret.ToString());
  339 		}
  340 
  341 		private void zipObj_ReceivePrintMessage(object sender, ZipDLLPrintMessageEventArgs e)
  342 		{
  343 			textBox1.Text = e.PrintMessage + "\r\n";
  344 			Application.DoEvents();
  345 		}
  346 
  347 		private void zipObj_ReceiveServiceMessage(object sender, ZipDLLServiceMessageEventArgs e)
  348 		{
  349 			textBox2.Text = "Zipping: " + e.ArchiveFileName + " - " + e.SizeOfArchiveFile.ToString() + " bytes." + "\r\n";
  350 			Application.DoEvents();
  351 		}
  352 
  353 		#endregion
  354 
  355 		#region Private Functions
  356 
  357 		private void RecurseGetDirsAndFiles(string path, ArrayList al)
  358 		{
  359 			System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(path);
  360 
  361 			// List files.
  362 			foreach(System.IO.FileInfo f in d.GetFiles())
  363 				al.Add(f.FullName);
  364 
  365 			// Recurse into directories.
  366 			foreach(System.IO.DirectoryInfo dx in d.GetDirectories())
  367 				RecurseGetDirsAndFiles(dx.FullName,al);
  368 		}
  369 
  370 
  371 		private void btnStop_Click(object sender, System.EventArgs e)
  372 		{
  373 			m_ZipObj.Stop();
  374 		}
  375 
  376 
  377 		#endregion
  378 
  379 	}
  380 }