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 21using System; 22using System.Drawing; 23using System.Collections; 24using System.ComponentModel; 25using System.Windows.Forms; 26using System.Data; 27 28namespace CSharpInfoZip_UnZipSample 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 btnUnzipArchive; 36 private System.Windows.Forms.OpenFileDialog openFileDialog1; 37 private System.Windows.Forms.Label label1; 38 private System.Windows.Forms.TextBox textBox1; 39 private System.Windows.Forms.Label lblProgress; 40 private System.Windows.Forms.ProgressBar prgBar; 41 42 //Define the Unzip object 43 Unzip m_Unzip = new Unzip(); 44 private ulong m_CurrentSize; 45 private System.Windows.Forms.Button btnListZipFiles; 46 private CheckBox chkOverwriteAll; 47 48 49 /// <summary> 50 /// Required designer variable. 51 /// </summary> 52 private System.ComponentModel.Container components = null; 53 54 public Form1() 55 { 56 // 57 // Required for Windows Form Designer support 58 // 59 InitializeComponent(); 60 61 // 62 // TODO: Add any constructor code after InitializeComponent call 63 // 64 } 65 66 /// <summary> 67 /// Clean up any resources being used. 68 /// </summary> 69 protected override void Dispose( bool disposing ) 70 { 71 if( disposing ) 72 { 73 if (components != null) 74 { 75 components.Dispose(); 76 } 77 } 78 base.Dispose( disposing ); 79 } 80 81 #region Windows Form Designer generated code 82 /// <summary> 83 /// Required method for Designer support - do not modify 84 /// the contents of this method with the code editor. 85 /// </summary> 86 private void InitializeComponent() 87 { 88 this.btnUnzipArchive = new System.Windows.Forms.Button(); 89 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); 90 this.label1 = new System.Windows.Forms.Label(); 91 this.textBox1 = new System.Windows.Forms.TextBox(); 92 this.lblProgress = new System.Windows.Forms.Label(); 93 this.prgBar = new System.Windows.Forms.ProgressBar(); 94 this.btnListZipFiles = new System.Windows.Forms.Button(); 95 this.chkOverwriteAll = new System.Windows.Forms.CheckBox(); 96 this.SuspendLayout(); 97 // 98 // btnUnzipArchive 99 // 100 this.btnUnzipArchive.Location = new System.Drawing.Point(8, 24); 101 this.btnUnzipArchive.Name = "btnUnzipArchive"; 102 this.btnUnzipArchive.Size = new System.Drawing.Size(96, 24); 103 this.btnUnzipArchive.TabIndex = 0; 104 this.btnUnzipArchive.Text = "Unzip archive..."; 105 this.btnUnzipArchive.Click += new System.EventHandler(this.btnUnzipArchive_Click); 106 // 107 // label1 108 // 109 this.label1.Location = new System.Drawing.Point(8, 64); 110 this.label1.Name = "label1"; 111 this.label1.Size = new System.Drawing.Size(184, 16); 112 this.label1.TabIndex = 1; 113 this.label1.Text = "Unzip DLL Print callback message:"; 114 // 115 // textBox1 116 // 117 this.textBox1.Location = new System.Drawing.Point(8, 80); 118 this.textBox1.Multiline = true; 119 this.textBox1.Name = "textBox1"; 120 this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical; 121 this.textBox1.Size = new System.Drawing.Size(464, 120); 122 this.textBox1.TabIndex = 2; 123 // 124 // lblProgress 125 // 126 this.lblProgress.Location = new System.Drawing.Point(8, 208); 127 this.lblProgress.Name = "lblProgress"; 128 this.lblProgress.Size = new System.Drawing.Size(216, 16); 129 this.lblProgress.TabIndex = 3; 130 // 131 // prgBar 132 // 133 this.prgBar.Location = new System.Drawing.Point(8, 224); 134 this.prgBar.Name = "prgBar"; 135 this.prgBar.Size = new System.Drawing.Size(216, 16); 136 this.prgBar.TabIndex = 4; 137 // 138 // btnListZipFiles 139 // 140 this.btnListZipFiles.Location = new System.Drawing.Point(120, 24); 141 this.btnListZipFiles.Name = "btnListZipFiles"; 142 this.btnListZipFiles.Size = new System.Drawing.Size(96, 24); 143 this.btnListZipFiles.TabIndex = 5; 144 this.btnListZipFiles.Text = "List zip files..."; 145 this.btnListZipFiles.Click += new System.EventHandler(this.btnListZipFiles_Click); 146 // 147 // chkOverwriteAll 148 // 149 this.chkOverwriteAll.AutoSize = true; 150 this.chkOverwriteAll.Location = new System.Drawing.Point(255, 29); 151 this.chkOverwriteAll.Name = "chkOverwriteAll"; 152 this.chkOverwriteAll.Size = new System.Drawing.Size(177, 17); 153 this.chkOverwriteAll.TabIndex = 6; 154 this.chkOverwriteAll.Text = "Overwrite all files without prompt"; 155 this.chkOverwriteAll.UseVisualStyleBackColor = true; 156 // 157 // Form1 158 // 159 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); 160 this.ClientSize = new System.Drawing.Size(480, 254); 161 this.Controls.Add(this.chkOverwriteAll); 162 this.Controls.Add(this.btnListZipFiles); 163 this.Controls.Add(this.prgBar); 164 this.Controls.Add(this.lblProgress); 165 this.Controls.Add(this.textBox1); 166 this.Controls.Add(this.label1); 167 this.Controls.Add(this.btnUnzipArchive); 168 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; 169 this.MaximizeBox = false; 170 this.MinimizeBox = false; 171 this.Name = "Form1"; 172 this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; 173 this.Text = "Form1"; 174 this.ResumeLayout(false); 175 this.PerformLayout(); 176 177 } 178 #endregion 179 180 /// <summary> 181 /// The main entry point for the application. 182 /// </summary> 183 [STAThread] 184 static void Main() 185 { 186 Application.Run(new Form1()); 187 } 188 189 #region Event Handlers 190 191 private void btnUnzipArchive_Click(object sender, System.EventArgs e) 192 { 193 openFileDialog1.ShowDialog(); 194 string file = openFileDialog1.FileName; 195 196 if (file == null | file == string.Empty) return; 197 198 //Clear the DLL messages output area 199 m_CurrentSize = 0; 200 prgBar.Value = prgBar.Minimum; 201 lblProgress.Text = ""; 202 textBox1.Text = ""; 203 204 //Instantiate the Unzip object 205 m_Unzip = new Unzip(); 206 207 //NOTE: 208 //There are many unzip options. This sample just demonstrates basic unzip options. 209 //Consult the InfoZip documentation for more option information. 210 211 //Set the Unzip object properties 212 m_Unzip.ZipFileName = file; 213 m_Unzip.HonorDirectories = HonorDirectoriesEnum.True; 214 m_Unzip.ExtractOrList = ExtractOrListEnum.Extract; 215 216 //This option sets the DLL to display only the archive comment and 217 //then exit immediately. 218 //m_Unzip.DisplayComment = DisplayCommentEnum.True; 219 220 //This option switches the DLL into "verbose ZipInfo" mode. The 221 //DLL extracts nothing, but instead prints out a verbose technical list 222 //of the content of the Zip archive central directory. 223 //This option works now, but gets terribly slow when applied to archives 224 //with a large number of entries. 225 //m_Unzip.VerboseZI = VerboseZIEnum.True; 226 227 if (chkOverwriteAll.Checked) 228 { 229 m_Unzip.OverWriteFiles = OverWriteFilesEnum.True; 230 m_Unzip.PromptToOverWrite = PromptToOverWriteEnum.NotRequired; 231 } 232 else 233 { 234 m_Unzip.OverWriteFiles = OverWriteFilesEnum.False; 235 m_Unzip.PromptToOverWrite = PromptToOverWriteEnum.Required; 236 } 237 238 //NOTE: 239 //Directory where the unzipped files are stored. Change this as appropriate 240 m_Unzip.ExtractDirectory = @"c:\temp\unzip"; 241 242 //Wire the event handlers to receive the events from the Unzip class 243 m_Unzip.ReceivePrintMessage +=new UnZipDLLPrintMessageEventHandler(unZip_ReceivePrintMessage); 244 m_Unzip.ReceiveServiceMessage +=new UnZipDLLServiceMessageEventHandler(unZip_ReceiveServiceMessage); 245 246 //Unzip the files 247 int ret = m_Unzip.UnZipFiles(); 248 249 //Examine the return code 250 MessageBox.Show("Done. Return Code: " + ret.ToString()); 251 } 252 253 private void btnListZipFiles_Click(object sender, System.EventArgs e) 254 { 255 256 openFileDialog1.ShowDialog(); 257 string file = openFileDialog1.FileName; 258 259 if (file == null | file == string.Empty) return; 260 261 //Clear the DLL messages output area 262 prgBar.Value = prgBar.Minimum; 263 lblProgress.Text = ""; 264 textBox1.Text = ""; 265 266 //Instantiate the Unzip object 267 m_Unzip = new Unzip(); 268 269 //NOTE: 270 //There are many unzip options. This sample just demonstrates basic unzip options. 271 //Consult the InfoZip documentation for more option information. 272 273 //Set the Unzip object properties 274 m_Unzip.ZipFileName = file; 275 m_Unzip.HonorDirectories = HonorDirectoriesEnum.True; 276 m_Unzip.ExtractOrList = ExtractOrListEnum.ListContents; 277 278 //Wire the event handlers to receive the events from the Unzip class 279 m_Unzip.ReceivePrintMessage +=new UnZipDLLPrintMessageEventHandler(unZip_ReceivePrintMessage); 280 m_Unzip.ReceiveServiceMessage +=new UnZipDLLServiceMessageEventHandler(unZip_ReceiveServiceMessage); 281 282 //Unzip the files 283 ZipFileEntries zfes = m_Unzip.GetZipFileContents(); 284 285 //Show the file contents 286 frmShowContents frm = new frmShowContents(); 287 frm.UnzippedFileCollection = zfes; 288 289 //WORK AROUND: 290 frm.Comment = m_Unzip.GetZipFileComment(); 291 292 frm.ShowDialog(this); 293 294 //Examine the return code 295 MessageBox.Show("Done."); 296 297 } 298 299 private void unZip_ReceivePrintMessage(object sender, UnZipDLLPrintMessageEventArgs e) 300 { 301 textBox1.Text += e.PrintMessage.Replace("\n", Environment.NewLine); 302 Application.DoEvents(); 303 } 304 305 private int unZip_ReceiveServiceMessage(object sender, UnZipDLLServiceMessageEventArgs e) 306 { 307 m_CurrentSize += e.SizeOfFileEntry; 308 prgBar.Value = Convert.ToInt32(100 * Convert.ToDouble(m_CurrentSize) 309 / Convert.ToDouble(e.ZipFileSize)); 310 lblProgress.Text = "Unzipping " + m_CurrentSize.ToString("N0") + " of " + 311 e.ZipFileSize.ToString("N0") + " bytes."; 312 Application.DoEvents(); 313 314 return 0; 315 } 316 317 #endregion 318 319 320 321 } 322} 323