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