forked from svn2github/dotnetzip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipForm.cs
More file actions
2154 lines (1828 loc) · 85.5 KB
/
Copy pathZipForm.cs
File metadata and controls
2154 lines (1828 loc) · 85.5 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ZipForm.cs
// ------------------------------------------------------------------
//
// Copyright (c) 2009-2011 Dino Chiesa
// All rights reserved.
//
// This code module is part of DotNetZip, a zipfile class library.
//
// ------------------------------------------------------------------
//
// This code is licensed under the Microsoft Public License.
// See the file License.txt for the license details.
// More info on: http://dotnetzip.codeplex.com
//
// ------------------------------------------------------------------
//
using System;
using System.Collections.Generic;
using System.Threading;
using System.Windows.Forms;
using Ionic.Zip;
namespace Ionic.Zip.Forms
{
public partial class ZipForm : Form
{
delegate void ZipProgress(ZipProgressEventArgs e);
delegate void ButtonClick(object sender, EventArgs e);
HiResTimer _hrt;
public ZipForm()
{
InitializeComponent();
InitializeListboxes();
FixTitle();
FillFormFromRegistry();
AdoptProgressBars();
SetListView2();
SetTextBoxes();
// first run only
if (numRuns == 0)
this.tabControl1.SelectedIndex = 2; // help/about tab
}
// This constructor works to load zips from the command line.
// It also works to allow "open With..." from Windows Explorer.
public ZipForm(string[] args)
: this()
{
if (args != null && args.Length >= 1 && args[0] != null)
_initialFileToLoad = args[0];
}
// in ZipForm.DragDrop.cs
partial void SetDragDrop();
private void SetTextBoxes()
{
this.tbComment.Text= TB_COMMENT_NOTE;
this.tbDefaultExtractDirectory.Text= TB_EXTRACT_DIR_NOTE;
this.tbExeOnUnpack.Text= TB_EXE_ON_UNPACK_NOTE;
// set autocomplete on a few textboxes
this.tbDirectoryToZip.AutoCompleteMode = AutoCompleteMode.Suggest;
this.tbDirectoryToZip.AutoCompleteSource = AutoCompleteSource.FileSystemDirectories;
this.tbExtractDir.AutoCompleteMode = AutoCompleteMode.Suggest;
this.tbExtractDir.AutoCompleteSource = AutoCompleteSource.FileSystemDirectories;
this.tbZipToOpen.AutoCompleteMode = AutoCompleteMode.Suggest;
this.tbZipToOpen.AutoCompleteSource = AutoCompleteSource.FileSystem;
this.tbZipToCreate.AutoCompleteMode = AutoCompleteMode.Suggest;
this.tbZipToCreate.AutoCompleteSource = AutoCompleteSource.FileSystem;
this.tbSelectionToExtract.AutoCompleteMode = AutoCompleteMode.Suggest;
this.tbSelectionToExtract.AutoCompleteSource = AutoCompleteSource.CustomSource;
this.tbSelectionToExtract.AutoCompleteCustomSource = _selectionCompletions;
this.tbSelectionToZip.AutoCompleteMode = AutoCompleteMode.Suggest;
this.tbSelectionToZip.AutoCompleteSource = AutoCompleteSource.CustomSource;
this.tbSelectionToZip.AutoCompleteCustomSource = _selectionCompletions;
tbExeOnUnpack_TextChanged(null, null);
}
private void SetListView2()
{
// The listview2 is a ListViewEx control, an extension of
// System.Windows.Forms.ListView that allows editing of subitems using arbitrary
// controls. You can have a textbox, a datepicker, or other controls.
// I want the user to be able to modify the directory-in-archive value in the
// list, which is why ListViewEx is interesting.
// I also want a checkbox associated to each list item, but I don't want the
// checkbox attached to the first subitem, as it normally is in a ListView. So I
// put an empty string as the main ListView item (subitem #0), and then the
// filename and directory-in-archive value in the 2nd and 3rd columns (subitems 1
// and 2). This way, the checkbox gets its own column.
// Next twist is I want the checkbox label to be uneditable, which is easy
// by just installing a listView2_BeforeLabelEdit method and always cancelling
// it. (e.CancelEdit = true).
// And then there is a "master checkbox" at the column header that indicates
// whether the state of all checkboxes in the list is the same. With the "check
// change" of any item in the list I want to see if the master should be checked
// or unchecked.
// The final thing is I want the checkbox for each item to change state only if I
// click on the checkbox itself, rather than "anywhere in the item row".
this.listView2.SubItemClicked += new ListViewEx.SubItemEventHandler(listView2_SubItemClicked);
this.listView2.SubItemEndEditing += new ListViewEx.SubItemEndEditingEventHandler(listView2_SubItemEndEditing);
this.listView2.DoubleClickActivation = true;
SetDragDrop();
}
private void AdoptProgressBars()
{
tabControl1_SelectedIndexChanged(null, null);
}
private void FixTitle()
{
this.Text = String.Format("DotNetZip Tool v{0}",
System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
}
private void InitializeListboxes()
{
InitEncodingsList();
InitFlavorList();
InitZip64List();
InitCompMethodList();
InitCompressionLevelList();
InitEncryptionList();
InitSplitBox();
InitExtractExistingFileList();
}
private void InitSplitBox()
{
string[] values = { "-none-", "64kb", "128kb", "256kb", "512kb", "1mb", "2mb", "4mb", "8mb", "16mb", "32mb", "64mb", "128mb", "256mb", "512mb", "1gb" };
foreach (var value in values)
this.comboSplit.Items.Add(value);
this.comboSplit.SelectedIndex = 0;
this.comboSplit.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
//this.comboSplit.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
}
private void InitEncryptionList()
{
List<String> _EncryptionNames = new List<string>(Enum.GetNames(typeof(Ionic.Zip.EncryptionAlgorithm)));
foreach (var name in _EncryptionNames)
{
if (name != "Unsupported")
comboEncryption.Items.Add(name);
}
// select the first item:
comboEncryption.SelectedIndex = 0;
this.tbPassword.Text = "";
}
private void InitExtractExistingFileList()
{
List<String> _ExtractActionNames = new List<string>(Enum.GetNames(typeof(Ionic.Zip.ExtractExistingFileAction)));
foreach (var name in _ExtractActionNames)
{
if (!name.StartsWith("Invoke"))
{
if (name.StartsWith("Throw"))
comboExistingFileAction.Items.Add("Stop");
else
comboExistingFileAction.Items.Add(name);
}
}
// select the first item:
comboExistingFileAction.SelectedIndex = 0;
}
private void InitEncodingsList()
{
List<String> _EncodingNames = new List<string>();
var e = System.Text.Encoding.GetEncodings();
foreach (var e1 in e)
{
if (!_EncodingNames.Contains(e1.Name))
if (!_EncodingNames.Contains(e1.Name.ToUpper()))
if (!_EncodingNames.Contains(e1.Name.ToLower()))
if (e1.Name != "IBM437" && e1.Name != "utf-8")
_EncodingNames.Add(e1.Name);
}
_EncodingNames.Sort();
comboEncoding.Items.Add("zip default (IBM437)");
comboEncoding.Items.Add("utf-8");
foreach (var name in _EncodingNames)
{
comboEncoding.Items.Add(name);
}
// select the first item:
comboEncoding.SelectedIndex = 0;
// also set the encoding usage
List<String> _UsageNames = new List<string>(Enum.GetNames(typeof(Ionic.Zip.ZipOption)));
_UsageNames.Sort();
foreach (var name in _UsageNames)
{
if (!name.StartsWith("Default"))
comboEncodingUsage.Items.Add(name);
}
// select the first item:
comboEncodingUsage.SelectedIndex = 0;
}
private void InitCompressionLevelList()
{
List<String> _CompressionLevelNames = new List<string>(Enum.GetNames(typeof(Ionic.Zlib.CompressionLevel)));
_CompressionLevelNames.Sort();
foreach (var name in _CompressionLevelNames)
{
if (!name.StartsWith("Level"))
{
comboCompLevel.Items.Add(name);
}
}
// select the 2nd item, "Default":
comboCompLevel.SelectedIndex = 2;
}
private void InitFlavorList()
{
this.comboFlavor.Items.Add("traditional Zip");
this.comboFlavor.Items.Add("Self-extractor (GUI)");
this.comboFlavor.Items.Add("Self-extractor (CMD)");
// select the first item:
comboFlavor.SelectedIndex = 0;
}
private void InitZip64List()
{
var _Names = new List<string>(Enum.GetNames(typeof(Ionic.Zip.Zip64Option)));
_Names.Sort();
foreach (var name in _Names)
{
if (!name.StartsWith("Default"))
comboZip64.Items.Add(name);
}
// select the first item:
comboZip64.SelectedIndex = 0;
}
private void InitCompMethodList()
{
var _Names = new List<string>(Enum.GetNames(typeof(Ionic.Zip.CompressionMethod)));
_Names.Sort();
foreach (var name in _Names)
{
if (!name.StartsWith("Default"))
comboCompMethod.Items.Add(name);
}
// select DEFLATE:
comboCompMethod.SelectedIndex = 1;
}
private void KickoffZipup()
{
if (String.IsNullOrEmpty(this.tbDirectoryToZip.Text)) return;
if (!System.IO.Directory.Exists(this.tbDirectoryToZip.Text))
{
var dlgResult = MessageBox.Show(String.Format("The directory you have specified ({0}) does not exist.", this.tbZipToCreate.Text),
"Not gonna happen", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (this.tbZipToCreate.Text == null || this.tbZipToCreate.Text == "") return;
// check for existence of the zip file:
if (System.IO.File.Exists(this.tbZipToCreate.Text))
{
var dlgResult = MessageBox.Show(String.Format("The file you have specified ({0}) already exists. Do you want to overwrite this file?", this.tbZipToCreate.Text), "Confirmation is Required", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlgResult != DialogResult.Yes) return;
System.IO.File.Delete(this.tbZipToCreate.Text);
}
// check for a valid zip file name:
string extension = System.IO.Path.GetExtension(this.tbZipToCreate.Text);
if ((extension != ".exe" && (this.comboFlavor.SelectedIndex == 1 || this.comboFlavor.SelectedIndex == 2)) ||
(extension != ".zip" && this.comboFlavor.SelectedIndex == 0))
{
var dlgResult = MessageBox.Show(String.Format("The file you have specified ({0}) has a non-standard extension ({1}) for this zip flavor. Do you want to continue anyway?",
this.tbZipToCreate.Text, extension), "Hold on there, pardner!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlgResult != DialogResult.Yes) return;
System.IO.File.Delete(this.tbZipToCreate.Text);
}
_hrt = new HiResTimer();
_hrt.Start();
_working = true;
_operationCanceled = false;
_nFilesCompleted = 0;
_totalBytesAfterCompress = 0;
_totalBytesBeforeCompress = 0;
PauseUI("Zipping...");
lblStatus.Text = "Zipping...";
var options = new SaveWorkerOptions
{
ZipName = this.tbZipToCreate.Text,
Selection = this.tbSelectionToZip.Text,
TraverseJunctions = this.chkTraverseJunctions.Checked,
Encoding = "ibm437",
//EncodingUsage = ZipOption.Always,
ZipFlavor = this.comboFlavor.SelectedIndex,
Password = this.tbPassword.Text,
WindowsTimes = this.chkWindowsTime.Checked,
UnixTimes = this.chkUnixTime.Checked,
RemoveFilesAfterExe = this.chkRemoveFiles.Checked,
ExtractExistingFile = this.comboExistingFileAction.SelectedIndex,
};
if (this.comboEncoding.SelectedIndex != 0)
options.Encoding = this.comboEncoding.SelectedItem.ToString();
options.Encryption = (EncryptionAlgorithm) Enum.Parse(typeof(EncryptionAlgorithm),
this.comboEncryption.SelectedItem.ToString());
options.CompressionLevel = (Ionic.Zlib.CompressionLevel) Enum.Parse(typeof(Ionic.Zlib.CompressionLevel),
this.comboCompLevel.SelectedItem.ToString());
options.CompressionMethod = (Ionic.Zip.CompressionMethod) Enum.Parse(typeof(Ionic.Zip.CompressionMethod),
this.comboCompMethod.SelectedItem.ToString());
options.EncodingUsage = (Ionic.Zip.ZipOption) Enum.Parse(typeof(Ionic.Zip.ZipOption),
this.comboEncodingUsage.SelectedItem.ToString());
string arg = this.comboSplit.SelectedItem.ToString().ToUpper();
try
{
int multiplier = 1;
int suffixLength = 0;
if (arg.EndsWith("KB"))
{
multiplier = 1024; suffixLength = 2;
}
else if (arg.EndsWith("K"))
{
multiplier = 1024; suffixLength = 1;
}
else if (arg.EndsWith("MB"))
{
multiplier = 1024*1024; suffixLength = 2;
}
else if (arg.EndsWith("M"))
{
multiplier = 1024*1024; suffixLength = 1;
}
else if (arg.EndsWith("GB"))
{
multiplier = 1024*1024*1024; suffixLength = 2;
}
else if (arg.EndsWith("G"))
{
multiplier = 1024*1024*1024; suffixLength = 1;
}
if (suffixLength > 0)
{
options.MaxSegmentSize =
Int32.Parse(arg.Substring(0,arg.Length-suffixLength)) * multiplier;
}
else
options.MaxSegmentSize = Int32.Parse(arg);
}
catch
{
// just reset to "none"
this.comboSplit.SelectedIndex = 0;
options.MaxSegmentSize = 0;
}
options.Zip64 = (Zip64Option)Enum.Parse(typeof(Zip64Option),
this.comboZip64.SelectedItem.ToString());
//this.listView2.Items.ToList();
var entriesList = new System.Collections.ArrayList(this.listView2.Items);
options.Entries = System.Array.ConvertAll((ListViewItem[])entriesList.ToArray(typeof(ListViewItem)), (item) =>
{
return new ItemToAdd
{
LocalFileName = item.SubItems[1].Text,
DirectoryInArchive = item.SubItems[2].Text,
FileNameInArchive = item.SubItems[3].Text,
};
}
);
string compress = (options.CompressionMethod == Ionic.Zip.CompressionMethod.Deflate)
? "Deflate level" + options.CompressionLevel.ToString()
: options.CompressionMethod.ToString();
options.Comment = String.Format("Encoding:{0} || Compression:{1} || Encrypt:{2} || ZIP64:{3}\r\nCreated at {4} || {5}\r\n",
options.Encoding,
compress,
(this.tbPassword.Text == "") ? "None" : options.Encryption.ToString(),
options.Zip64.ToString(),
System.DateTime.Now.ToString("yyyy-MMM-dd HH:mm:ss"),
this.Text);
if (this.tbComment.Text != TB_COMMENT_NOTE)
options.Comment += this.tbComment.Text;
if (this.tbExeOnUnpack.Text != TB_EXE_ON_UNPACK_NOTE)
options.ExeOnUnpack = this.tbExeOnUnpack.Text;
if (this.tbDefaultExtractDirectory.Text != TB_EXTRACT_DIR_NOTE)
options.ExtractDirectory = this.tbDefaultExtractDirectory.Text;
_workerThread = new Thread(this.DoSave);
_workerThread.Name = "Zip Saver thread";
_workerThread.Start(options);
this.Cursor = Cursors.WaitCursor;
}
private string FlavorToString(int p)
{
if (p == 2) return "SFX-CMD";
if (p == 1) return "SFX-GUI";
return "ZIP";
}
private bool _firstFocusInCommentTextBox = true;
private void tbComment_Enter(object sender, EventArgs e)
{
if (_firstFocusInCommentTextBox)
{
tbComment.Text = "";
tbComment.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
tbComment.ForeColor = System.Drawing.SystemColors.WindowText;
_firstFocusInCommentTextBox = false;
}
}
private void tbComment_Leave(object sender, EventArgs e)
{
string TextInTheBox = tbComment.Text;
if ((TextInTheBox == null) || (TextInTheBox == ""))
{
this.tbComment.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.tbComment.ForeColor = System.Drawing.SystemColors.InactiveCaption;
_firstFocusInCommentTextBox = true;
this.tbComment.Text = TB_COMMENT_NOTE;
}
}
private bool _firstFocusInExtractDirTextBox = true;
private void tbDefaultExtractDirectory_Enter(object sender, EventArgs e)
{
if (_firstFocusInExtractDirTextBox)
{
tbDefaultExtractDirectory.Text = "";
tbDefaultExtractDirectory.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
tbDefaultExtractDirectory.ForeColor = System.Drawing.SystemColors.WindowText;
_firstFocusInExtractDirTextBox = false;
}
}
private void tbDefaultExtractDirectory_Leave(object sender, EventArgs e)
{
string TextInTheBox = tbDefaultExtractDirectory.Text;
if ((TextInTheBox == null) || (TextInTheBox == ""))
{
this.tbDefaultExtractDirectory.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.tbDefaultExtractDirectory.ForeColor = System.Drawing.SystemColors.InactiveCaption;
_firstFocusInExtractDirTextBox = true;
this.tbDefaultExtractDirectory.Text = TB_EXTRACT_DIR_NOTE;
}
}
// I want the values in the combobox to be right-aligned.
private int delta = 0;
private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
var rc = new System.Drawing.Rectangle(e.Bounds.X + delta, e.Bounds.Y + delta,
e.Bounds.Width - delta, e.Bounds.Height - delta);
var sf = new System.Drawing.StringFormat
{
Alignment = System.Drawing.StringAlignment.Far
};
string str = (string)comboSplit.Items[e.Index];
if (e.State == (DrawItemState.Selected | DrawItemState.NoAccelerator
| DrawItemState.NoFocusRect) ||
e.State == DrawItemState.Selected)
{
e.Graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.CornflowerBlue), rc);
e.Graphics.DrawString(str, this.comboSplit.Font, new System.Drawing.SolidBrush(System.Drawing.Color.Cyan), rc, sf);
}
else
{
e.Graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), rc);
e.Graphics.DrawString(str, this.comboSplit.Font, new System.Drawing.SolidBrush(System.Drawing.Color.Black), rc, sf);
}
}
private bool _firstFocusInExeTextBox = true;
private void tbExeOnUnpack_Enter(object sender, EventArgs e)
{
if (_firstFocusInExeTextBox)
{
tbExeOnUnpack.Text = "";
tbExeOnUnpack.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
tbExeOnUnpack.ForeColor = System.Drawing.SystemColors.WindowText;
_firstFocusInExeTextBox = false;
}
}
private void tbExeOnUnpack_Leave(object sender, EventArgs e)
{
string TextInTheBox = tbExeOnUnpack.Text;
if ((TextInTheBox == null) || (TextInTheBox == ""))
{
this.tbExeOnUnpack.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.tbExeOnUnpack.ForeColor = System.Drawing.SystemColors.InactiveCaption;
_firstFocusInExeTextBox = true;
this.tbExeOnUnpack.Text = TB_EXE_ON_UNPACK_NOTE;
this.label18.Enabled = false;
this.chkRemoveFiles.Enabled = false;
}
else
{
this.label18.Enabled = true;
this.chkRemoveFiles.Enabled = true;
}
}
private void tbExeOnUnpack_TextChanged(object sender, EventArgs e)
{
if (this.tbExeOnUnpack.Text != TB_EXE_ON_UNPACK_NOTE && this.tbExeOnUnpack.Text != "")
{
this.label18.Enabled = true;
this.chkRemoveFiles.Enabled = true;
}
else
{
this.label18.Enabled = false;
this.chkRemoveFiles.Enabled = false;
}
}
private void SetProgressBars()
{
if (this.progressBar1.InvokeRequired)
{
this.progressBar1.Invoke(new MethodInvoker(this.SetProgressBars));
//this.progressBar1.Invoke(new ProgressBarSetup(this.SetProgressBars), new object[] { count });
}
else
{
this.progressBar1.Value = 0;
this.progressBar1.Maximum = _totalEntriesToProcess;
this.progressBar1.Minimum = 0;
this.progressBar1.Step = 1;
this.progressBar2.Value = 0;
this.progressBar2.Minimum = 0;
this.progressBar2.Maximum = 1; // will be set later, for each entry.
this.progressBar2.Step = 1;
}
}
private void DoSave(Object p)
{
SaveWorkerOptions options = p as SaveWorkerOptions;
try
{
using (var zip1 = new ZipFile())
{
zip1.CompressionMethod = options.CompressionMethod;
zip1.CompressionLevel = options.CompressionLevel;
zip1.AlternateEncoding = System.Text.Encoding.GetEncoding(options.Encoding);
zip1.AlternateEncodingUsage = options.EncodingUsage;
zip1.Comment = options.Comment;
zip1.MaxOutputSegmentSize = options.MaxSegmentSize;
zip1.Password = (options.Password != "") ? options.Password : null;
zip1.Encryption = options.Encryption;
zip1.EmitTimesInWindowsFormatWhenSaving = options.WindowsTimes;
zip1.EmitTimesInUnixFormatWhenSaving = options.UnixTimes;
zip1.AddDirectoryWillTraverseReparsePoints = options.TraverseJunctions;
foreach (ItemToAdd item in options.Entries)
{
var e = zip1.AddItem(item.LocalFileName, item.DirectoryInArchive);
// use a different name in the archive if appropriate
if (!String.IsNullOrEmpty(item.FileNameInArchive) && item.FileNameInArchive != System.IO.Path.GetFileName(item.LocalFileName))
e.FileName = item.FileNameInArchive;
}
_totalEntriesToProcess = zip1.EntryFileNames.Count;
SetProgressBars();
zip1.TempFileFolder = System.IO.Path.GetDirectoryName(options.ZipName);
zip1.SaveProgress += this.zip1_SaveProgress;
zip1.UseZip64WhenSaving = options.Zip64;
if (options.ZipFlavor == 2 || options.ZipFlavor == 1)
{
SelfExtractorSaveOptions sfxOptions = new SelfExtractorSaveOptions()
{
Flavor = (options.ZipFlavor == 1)?SelfExtractorFlavor.WinFormsApplication:
SelfExtractorFlavor.ConsoleApplication,
DefaultExtractDirectory = options.ExtractDirectory,
PostExtractCommandLine = options.ExeOnUnpack,
RemoveUnpackedFilesAfterExecute = options.RemoveFilesAfterExe,
ExtractExistingFile = (ExtractExistingFileAction) options.ExtractExistingFile,
};
zip1.SaveSelfExtractor(options.ZipName, sfxOptions);
}
else
zip1.Save(options.ZipName);
}
}
catch (System.Exception exc1)
{
MessageBox.Show(String.Format("Exception while zipping:\n{0}\n\n{1}", exc1.Message, exc1.StackTrace.ToString()));
btnCancel_Click(null, null);
}
}
void zip1_SaveProgress(object sender, SaveProgressEventArgs e)
{
switch (e.EventType)
{
case ZipProgressEventType.Saving_AfterWriteEntry:
StepArchiveProgress(e);
break;
case ZipProgressEventType.Saving_EntryBytesRead:
StepEntryProgress(e);
break;
case ZipProgressEventType.Saving_Completed:
SaveCompleted();
break;
case ZipProgressEventType.Saving_AfterSaveTempArchive:
TempArchiveSaved();
break;
}
if (_operationCanceled)
e.Cancel = true;
}
private void TempArchiveSaved()
{
if (this.lblStatus.InvokeRequired)
{
this.lblStatus.Invoke(new MethodInvoker(this.TempArchiveSaved));
}
else
{
System.TimeSpan ts = new System.TimeSpan(0, 0, (int)_hrt.Seconds);
lblStatus.Text = String.Format("Temp archive saved ({0})...{1}...",
ts.ToString(),
(this.comboFlavor.SelectedIndex == 0)
? "finishing"
: "compiling SFX");
}
}
private void SaveCompleted()
{
if (this.lblStatus.InvokeRequired)
{
this.lblStatus.Invoke(new MethodInvoker(this.SaveCompleted));
}
else
{
_hrt.Stop();
System.TimeSpan ts = new System.TimeSpan(0, 0, (int)_hrt.Seconds);
lblStatus.Text = String.Format("Done, Compressed {0} files, {1:N0}% of original, time: {2}",
_nFilesCompleted, (100.00 * _totalBytesAfterCompress) / _totalBytesBeforeCompress,
ts.ToString());
ResetUiState();
}
}
private void StepArchiveProgress(ZipProgressEventArgs e)
{
if (this.progressBar1.InvokeRequired)
{
this.progressBar1.Invoke(new ZipProgress(this.StepArchiveProgress), new object[] { e });
}
else
{
if (!_operationCanceled)
{
_nFilesCompleted++;
this.progressBar1.PerformStep();
_totalBytesAfterCompress += e.CurrentEntry.CompressedSize;
_totalBytesBeforeCompress += e.CurrentEntry.UncompressedSize;
// reset the progress bar for the entry:
this.progressBar2.Value = this.progressBar2.Maximum = 1;
this.Update();
#if NOT_SPEEDY
// Sleep here just to show the progress bar, when the number of files is small,
// or when all done.
// You may not want this for actual use!
if (this.progressBar2.Value == this.progressBar2.Maximum)
Thread.Sleep(350);
else if (_entriesToZip < 10)
Thread.Sleep(350);
else if (_entriesToZip < 20)
Thread.Sleep(200);
else if (_entriesToZip < 30)
Thread.Sleep(100);
else if (_entriesToZip < 45)
Thread.Sleep(80);
else if (_entriesToZip < 75)
Thread.Sleep(40);
// more than 75 entries, don't sleep at all.
#endif
}
}
}
private void StepEntryProgress(ZipProgressEventArgs e)
{
if (this.progressBar2.InvokeRequired)
{
this.progressBar2.Invoke(new ZipProgress(this.StepEntryProgress), new object[] { e });
}
else
{
if (!_operationCanceled)
{
if (this.progressBar2.Maximum == 1)
{
// reset
Int64 entryMax = e.TotalBytesToTransfer;
Int64 absoluteMax = System.Int32.MaxValue;
_progress2MaxFactor = 0;
while (entryMax > absoluteMax)
{
entryMax /= 2;
_progress2MaxFactor++;
}
if ((int)entryMax < 0) entryMax *= -1;
this.progressBar2.Maximum = (int)entryMax;
lblStatus.Text = String.Format("{0} of {1} files...({2})",
_nFilesCompleted + 1, _totalEntriesToProcess, e.CurrentEntry.FileName);
}
// downcast is safe here because we have shifted e.BytesTransferred
int xferred = (int)(e.BytesTransferred >> _progress2MaxFactor);
this.progressBar2.Value = (xferred >= this.progressBar2.Maximum)
? this.progressBar2.Maximum
: xferred;
this.Update();
}
}
}
private void btnDirBrowse_Click(object sender, EventArgs e)
{
var dlg1 = new Ionic.Utils.FolderBrowserDialogEx
{
Description = "Select a folder to zip up:",
ShowNewFolderButton = false,
ShowEditBox = true,
//NewStyle = false,
SelectedPath = this.tbDirectoryToZip.Text,
ShowFullPathInEditBox = true,
};
dlg1.RootFolder = System.Environment.SpecialFolder.MyComputer;
var result = dlg1.ShowDialog();
if (result == DialogResult.OK)
{
this.tbDirectoryToZip.Text = dlg1.SelectedPath;
this.tbDirectoryInArchive.Text = System.IO.Path.GetFileName(this.tbDirectoryToZip.Text);
}
}
private void btnCreateZipBrowse_Click(object sender, EventArgs e)
{
var dlg1 = new SaveFileDialog
{
OverwritePrompt = false,
Title = "Where would you like to save the generated Zip file?",
Filter = "ZIP files|*.zip",
};
try
{
dlg1.FileName = System.IO.Path.GetFileName(this.tbZipToCreate.Text);
}
catch
{
dlg1.FileName = "";
}
try
{
dlg1.InitialDirectory = System.IO.Path.GetDirectoryName(this.tbZipToCreate.Text);
}
catch
{
dlg1.InitialDirectory = "";
}
var result = dlg1.ShowDialog();
if (result == DialogResult.OK)
{
this.tbZipToCreate.Text = dlg1.FileName;
}
}
private void btnZipup_Click(object sender, EventArgs e)
{
// Do not start zipping while editing a textbox
// in listView2.
if (!textBox1.Visible)
KickoffZipup();
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (this.lblStatus.InvokeRequired)
{
this.lblStatus.Invoke(new ButtonClick(this.btnCancel_Click), new object[] { sender, e });
}
else
{
_operationCanceled = true;
lblStatus.Text = "Canceled...";
ResetUiState();
}
}
private void comboFlavor_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboFlavor.SelectedIndex == 1 || this.comboFlavor.SelectedIndex == 2)
{
// intelligently change the name of the thing to create
// It's an SFX, swap out ZIP and insert EXE
if (this.tbZipToCreate.Text.ToUpper().EndsWith(".ZIP"))
{
tbZipToCreate.Text = System.Text.RegularExpressions.Regex.Replace(tbZipToCreate.Text, "(?i:)\\.zip$", ".exe");
}
// enable/disable other dependent UI elements
this.label17.Enabled = false;
this.comboSplit.Enabled = false;
this.label15.Enabled = true;
this.tbDefaultExtractDirectory.Enabled = true;
this.label16.Enabled = true;
this.tbExeOnUnpack.Enabled = true;
this.label18.Enabled = true;
this.chkRemoveFiles.Enabled = true;
}
else if (this.comboFlavor.SelectedIndex == 0)
{
// intelligently change the name of the thing to create
// It's a regular ZIP, so swap out EXE and insert ZIP
if (this.tbZipToCreate.Text.ToUpper().EndsWith(".EXE"))
{
tbZipToCreate.Text = System.Text.RegularExpressions.Regex.Replace(tbZipToCreate.Text, "(?i:)\\.exe$", ".zip");
}
// enable/disable other dependent UI elements
this.label17.Enabled = true;
this.comboSplit.Enabled = true;
this.label15.Enabled = false;
this.tbDefaultExtractDirectory.Enabled = false;
this.label16.Enabled = false;
this.tbExeOnUnpack.Enabled = false;
this.label18.Enabled = false;
this.chkRemoveFiles.Enabled = false;
}
}
private void comboEncryption_SelectedIndexChanged(object sender, EventArgs e)
{
//this.tbPassword.Enabled = (this.comboBox3.SelectedItem.ToString() != "None");
if (this.comboEncryption.SelectedIndex == 0)
{
this.label9.Enabled = false;
this.tbPassword.Enabled = false;
this.chkHidePassword.Enabled = false;
}
else
{
this.label9.Enabled = true;
this.tbPassword.Enabled = true;
this.chkHidePassword.Enabled = true;
}
}
private void comboCompMethod_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.comboCompMethod.SelectedIndex == 1) // DEFLATE
{
this.label4.Enabled = true;
this.comboCompLevel.Enabled = true;
}
else
{
this.label4.Enabled = false;
this.comboCompLevel.Enabled = false;
}
}
private void comboEncoding_SelectedIndexChanged(object sender, EventArgs e)
{
this.comboEncodingUsage.Enabled = (this.comboEncoding.SelectedIndex != 0);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
this.tbPassword.PasswordChar = (this.chkHidePassword.Checked) ? '*' : '\0';
}
private void ResetUiState()
{
this.btnZipUp.Text = "Zip it!";
this.btnZipUp.Enabled = true;
this.btnCancel.Text = "Cancel";
this.btnCancel.Enabled = false;
this.btnExtract.Text = "Extract";
this.btnExtractDirBrowse.Enabled = true;
this.btnZipupDirBrowse.Enabled = true;
this.btnReadZipBrowse.Enabled = true;
this.btnClearItemsToZip.Enabled = true;
this.btnCreateZipBrowse.Enabled = true;
this.progressBar1.Value = 0;
this.progressBar2.Value = 0;
this.Cursor = Cursors.Default;
if (_workerThread != null)
if (!_workerThread.IsAlive)
_workerThread.Join();
_working = false;
}
private void SelectNamedEncoding(string s)
{