forked from svn2github/dotnetzip
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZipFile.AddUpdate.cs
More file actions
2163 lines (2030 loc) · 93.9 KB
/
Copy pathZipFile.AddUpdate.cs
File metadata and controls
2163 lines (2030 loc) · 93.9 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
// ZipFile.AddUpdate.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
//
// ------------------------------------------------------------------
//
// last saved (in emacs):
// Time-stamp: <2011-August-01 16:42:07>
//
// ------------------------------------------------------------------
//
// This module defines the methods for Adding and Updating entries in
// the ZipFile.
//
// ------------------------------------------------------------------
//
using System;
using System.IO;
using System.Collections.Generic;
namespace Ionic.Zip
{
public partial class ZipFile
{
/// <summary>
/// Adds an item, either a file or a directory, to a zip file archive.
/// </summary>
///
/// <remarks>
/// <para>
/// This method is handy if you are adding things to zip archive and don't
/// want to bother distinguishing between directories or files. Any files are
/// added as single entries. A directory added through this method is added
/// recursively: all files and subdirectories contained within the directory
/// are added to the <c>ZipFile</c>.
/// </para>
///
/// <para>
/// The name of the item may be a relative path or a fully-qualified
/// path. Remember, the items contained in <c>ZipFile</c> instance get written
/// to the disk only when you call <see cref="ZipFile.Save()"/> or a similar
/// save method.
/// </para>
///
/// <para>
/// The directory name used for the file within the archive is the same
/// as the directory name (potentially a relative path) specified in the
/// <paramref name="fileOrDirectoryName"/>.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
/// respective values at the time of this call will be applied to the
/// <c>ZipEntry</c> added.
/// </para>
///
/// </remarks>
///
/// <seealso cref="Ionic.Zip.ZipFile.AddFile(string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string)"/>
///
/// <overloads>This method has two overloads.</overloads>
/// <param name="fileOrDirectoryName">
/// the name of the file or directory to add.</param>
///
/// <returns>The <c>ZipEntry</c> added.</returns>
public ZipEntry AddItem(string fileOrDirectoryName)
{
return AddItem(fileOrDirectoryName, null);
}
/// <summary>
/// Adds an item, either a file or a directory, to a zip file archive,
/// explicitly specifying the directory path to be used in the archive.
/// </summary>
///
/// <remarks>
/// <para>
/// If adding a directory, the add is recursive on all files and
/// subdirectories contained within it.
/// </para>
/// <para>
/// The name of the item may be a relative path or a fully-qualified path.
/// The item added by this call to the <c>ZipFile</c> is not read from the
/// disk nor written to the zip file archive until the application calls
/// Save() on the <c>ZipFile</c>.
/// </para>
///
/// <para>
/// This version of the method allows the caller to explicitly specify the
/// directory path to be used in the archive, which would override the
/// "natural" path of the filesystem file.
/// </para>
///
/// <para>
/// Encryption will be used on the file data if the <c>Password</c> has
/// been set on the <c>ZipFile</c> object, prior to calling this method.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
/// respective values at the time of this call will be applied to the
/// <c>ZipEntry</c> added.
/// </para>
///
/// </remarks>
///
/// <exception cref="System.IO.FileNotFoundException">
/// Thrown if the file or directory passed in does not exist.
/// </exception>
///
/// <param name="fileOrDirectoryName">the name of the file or directory to add.
/// </param>
///
/// <param name="directoryPathInArchive">
/// The name of the directory path to use within the zip archive. This path
/// need not refer to an extant directory in the current filesystem. If the
/// files within the zip are later extracted, this is the path used for the
/// extracted file. Passing <c>null</c> (<c>Nothing</c> in VB) will use the
/// path on the fileOrDirectoryName. Passing the empty string ("") will
/// insert the item at the root path within the archive.
/// </param>
///
/// <seealso cref="Ionic.Zip.ZipFile.AddFile(string, string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string, string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string, string)"/>
///
/// <example>
/// This example shows how to zip up a set of files into a flat hierarchy,
/// regardless of where in the filesystem the files originated. The resulting
/// zip archive will contain a toplevel directory named "flat", which itself
/// will contain files Readme.txt, MyProposal.docx, and Image1.jpg. A
/// subdirectory under "flat" called SupportFiles will contain all the files
/// in the "c:\SupportFiles" directory on disk.
///
/// <code>
/// String[] itemnames= {
/// "c:\\fixedContent\\Readme.txt",
/// "MyProposal.docx",
/// "c:\\SupportFiles", // a directory
/// "images\\Image1.jpg"
/// };
///
/// try
/// {
/// using (ZipFile zip = new ZipFile())
/// {
/// for (int i = 1; i < itemnames.Length; i++)
/// {
/// // will add Files or Dirs, recurses and flattens subdirectories
/// zip.AddItem(itemnames[i],"flat");
/// }
/// zip.Save(ZipToCreate);
/// }
/// }
/// catch (System.Exception ex1)
/// {
/// System.Console.Error.WriteLine("exception: {0}", ex1);
/// }
/// </code>
///
/// <code lang="VB">
/// Dim itemnames As String() = _
/// New String() { "c:\fixedContent\Readme.txt", _
/// "MyProposal.docx", _
/// "SupportFiles", _
/// "images\Image1.jpg" }
/// Try
/// Using zip As New ZipFile
/// Dim i As Integer
/// For i = 1 To itemnames.Length - 1
/// ' will add Files or Dirs, recursing and flattening subdirectories.
/// zip.AddItem(itemnames(i), "flat")
/// Next i
/// zip.Save(ZipToCreate)
/// End Using
/// Catch ex1 As Exception
/// Console.Error.WriteLine("exception: {0}", ex1.ToString())
/// End Try
/// </code>
/// </example>
/// <returns>The <c>ZipEntry</c> added.</returns>
public ZipEntry AddItem(String fileOrDirectoryName, String directoryPathInArchive)
{
if (File.Exists(fileOrDirectoryName))
return AddFile(fileOrDirectoryName, directoryPathInArchive);
if (Directory.Exists(fileOrDirectoryName))
return AddDirectory(fileOrDirectoryName, directoryPathInArchive);
throw new FileNotFoundException(String.Format("That file or directory ({0}) does not exist!",
fileOrDirectoryName));
}
/// <summary>
/// Adds a File to a Zip file archive.
/// </summary>
/// <remarks>
///
/// <para>
/// This call collects metadata for the named file in the filesystem,
/// including the file attributes and the timestamp, and inserts that metadata
/// into the resulting ZipEntry. Only when the application calls Save() on
/// the <c>ZipFile</c>, does DotNetZip read the file from the filesystem and
/// then write the content to the zip file archive.
/// </para>
///
/// <para>
/// This method will throw an exception if an entry with the same name already
/// exists in the <c>ZipFile</c>.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
/// respective values at the time of this call will be applied to the
/// <c>ZipEntry</c> added.
/// </para>
///
/// </remarks>
///
/// <example>
/// <para>
/// In this example, three files are added to a Zip archive. The ReadMe.txt
/// file will be placed in the root of the archive. The .png file will be
/// placed in a folder within the zip called photos\personal. The pdf file
/// will be included into a folder within the zip called Desktop.
/// </para>
/// <code>
/// try
/// {
/// using (ZipFile zip = new ZipFile())
/// {
/// zip.AddFile("c:\\photos\\personal\\7440-N49th.png");
/// zip.AddFile("c:\\Desktop\\2008-Regional-Sales-Report.pdf");
/// zip.AddFile("ReadMe.txt");
///
/// zip.Save("Package.zip");
/// }
/// }
/// catch (System.Exception ex1)
/// {
/// System.Console.Error.WriteLine("exception: " + ex1);
/// }
/// </code>
///
/// <code lang="VB">
/// Try
/// Using zip As ZipFile = New ZipFile
/// zip.AddFile("c:\photos\personal\7440-N49th.png")
/// zip.AddFile("c:\Desktop\2008-Regional-Sales-Report.pdf")
/// zip.AddFile("ReadMe.txt")
/// zip.Save("Package.zip")
/// End Using
/// Catch ex1 As Exception
/// Console.Error.WriteLine("exception: {0}", ex1.ToString)
/// End Try
/// </code>
/// </example>
///
/// <overloads>This method has two overloads.</overloads>
///
/// <seealso cref="Ionic.Zip.ZipFile.AddItem(string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.UpdateFile(string)"/>
///
/// <param name="fileName">
/// The name of the file to add. It should refer to a file in the filesystem.
/// The name of the file may be a relative path or a fully-qualified path.
/// </param>
/// <returns>The <c>ZipEntry</c> corresponding to the File added.</returns>
public ZipEntry AddFile(string fileName)
{
return AddFile(fileName, null);
}
/// <summary>
/// Adds a File to a Zip file archive, potentially overriding the path to be
/// used within the zip archive.
/// </summary>
///
/// <remarks>
/// <para>
/// The file added by this call to the <c>ZipFile</c> is not written to the
/// zip file archive until the application calls Save() on the <c>ZipFile</c>.
/// </para>
///
/// <para>
/// This method will throw an exception if an entry with the same name already
/// exists in the <c>ZipFile</c>.
/// </para>
///
/// <para>
/// This version of the method allows the caller to explicitly specify the
/// directory path to be used in the archive.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
/// respective values at the time of this call will be applied to the
/// <c>ZipEntry</c> added.
/// </para>
///
/// </remarks>
///
/// <example>
/// <para>
/// In this example, three files are added to a Zip archive. The ReadMe.txt
/// file will be placed in the root of the archive. The .png file will be
/// placed in a folder within the zip called images. The pdf file will be
/// included into a folder within the zip called files\docs, and will be
/// encrypted with the given password.
/// </para>
/// <code>
/// try
/// {
/// using (ZipFile zip = new ZipFile())
/// {
/// // the following entry will be inserted at the root in the archive.
/// zip.AddFile("c:\\datafiles\\ReadMe.txt", "");
/// // this image file will be inserted into the "images" directory in the archive.
/// zip.AddFile("c:\\photos\\personal\\7440-N49th.png", "images");
/// // the following will result in a password-protected file called
/// // files\\docs\\2008-Regional-Sales-Report.pdf in the archive.
/// zip.Password = "EncryptMe!";
/// zip.AddFile("c:\\Desktop\\2008-Regional-Sales-Report.pdf", "files\\docs");
/// zip.Save("Archive.zip");
/// }
/// }
/// catch (System.Exception ex1)
/// {
/// System.Console.Error.WriteLine("exception: {0}", ex1);
/// }
/// </code>
///
/// <code lang="VB">
/// Try
/// Using zip As ZipFile = New ZipFile
/// ' the following entry will be inserted at the root in the archive.
/// zip.AddFile("c:\datafiles\ReadMe.txt", "")
/// ' this image file will be inserted into the "images" directory in the archive.
/// zip.AddFile("c:\photos\personal\7440-N49th.png", "images")
/// ' the following will result in a password-protected file called
/// ' files\\docs\\2008-Regional-Sales-Report.pdf in the archive.
/// zip.Password = "EncryptMe!"
/// zip.AddFile("c:\Desktop\2008-Regional-Sales-Report.pdf", "files\documents")
/// zip.Save("Archive.zip")
/// End Using
/// Catch ex1 As Exception
/// Console.Error.WriteLine("exception: {0}", ex1)
/// End Try
/// </code>
/// </example>
///
/// <seealso cref="Ionic.Zip.ZipFile.AddItem(string,string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string, string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.UpdateFile(string,string)"/>
///
/// <param name="fileName">
/// The name of the file to add. The name of the file may be a relative path
/// or a fully-qualified path.
/// </param>
///
/// <param name="directoryPathInArchive">
/// Specifies a directory path to use to override any path in the fileName.
/// This path may, or may not, correspond to a real directory in the current
/// filesystem. If the files within the zip are later extracted, this is the
/// path used for the extracted file. Passing <c>null</c> (<c>Nothing</c> in
/// VB) will use the path on the fileName, if any. Passing the empty string
/// ("") will insert the item at the root path within the archive.
/// </param>
///
/// <returns>The <c>ZipEntry</c> corresponding to the file added.</returns>
public ZipEntry AddFile(string fileName, String directoryPathInArchive)
{
string nameInArchive = ZipEntry.NameInArchive(fileName, directoryPathInArchive);
ZipEntry ze = ZipEntry.CreateFromFile(fileName, nameInArchive);
if (Verbose) StatusMessageTextWriter.WriteLine("adding {0}...", fileName);
return _InternalAddEntry(ze);
}
/// <summary>
/// This method removes a collection of entries from the <c>ZipFile</c>.
/// </summary>
///
/// <param name="entriesToRemove">
/// A collection of ZipEntry instances from this zip file to be removed. For
/// example, you can pass in an array of ZipEntry instances; or you can call
/// SelectEntries(), and then add or remove entries from that
/// ICollection<ZipEntry> (ICollection(Of ZipEntry) in VB), and pass
/// that ICollection to this method.
/// </param>
///
/// <seealso cref="Ionic.Zip.ZipFile.SelectEntries(String)" />
/// <seealso cref="Ionic.Zip.ZipFile.RemoveSelectedEntries(String)" />
public void RemoveEntries(System.Collections.Generic.ICollection<ZipEntry> entriesToRemove)
{
if (entriesToRemove == null)
throw new ArgumentNullException("entriesToRemove");
foreach (ZipEntry e in entriesToRemove)
{
this.RemoveEntry(e);
}
}
/// <summary>
/// This method removes a collection of entries from the <c>ZipFile</c>, by name.
/// </summary>
///
/// <param name="entriesToRemove">
/// A collection of strings that refer to names of entries to be removed
/// from the <c>ZipFile</c>. For example, you can pass in an array or a
/// List of Strings that provide the names of entries to be removed.
/// </param>
///
/// <seealso cref="Ionic.Zip.ZipFile.SelectEntries(String)" />
/// <seealso cref="Ionic.Zip.ZipFile.RemoveSelectedEntries(String)" />
public void RemoveEntries(System.Collections.Generic.ICollection<String> entriesToRemove)
{
if (entriesToRemove == null)
throw new ArgumentNullException("entriesToRemove");
foreach (String e in entriesToRemove)
{
this.RemoveEntry(e);
}
}
/// <summary>
/// This method adds a set of files to the <c>ZipFile</c>.
/// </summary>
///
/// <remarks>
/// <para>
/// Use this method to add a set of files to the zip archive, in one call.
/// For example, a list of files received from
/// <c>System.IO.Directory.GetFiles()</c> can be added to a zip archive in one
/// call.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
/// respective values at the time of this call will be applied to each
/// ZipEntry added.
/// </para>
/// </remarks>
///
/// <param name="fileNames">
/// The collection of names of the files to add. Each string should refer to a
/// file in the filesystem. The name of the file may be a relative path or a
/// fully-qualified path.
/// </param>
///
/// <example>
/// This example shows how to create a zip file, and add a few files into it.
/// <code>
/// String ZipFileToCreate = "archive1.zip";
/// String DirectoryToZip = "c:\\reports";
/// using (ZipFile zip = new ZipFile())
/// {
/// // Store all files found in the top level directory, into the zip archive.
/// String[] filenames = System.IO.Directory.GetFiles(DirectoryToZip);
/// zip.AddFiles(filenames);
/// zip.Save(ZipFileToCreate);
/// }
/// </code>
///
/// <code lang="VB">
/// Dim ZipFileToCreate As String = "archive1.zip"
/// Dim DirectoryToZip As String = "c:\reports"
/// Using zip As ZipFile = New ZipFile
/// ' Store all files found in the top level directory, into the zip archive.
/// Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip)
/// zip.AddFiles(filenames)
/// zip.Save(ZipFileToCreate)
/// End Using
/// </code>
/// </example>
///
/// <seealso cref="Ionic.Zip.ZipFile.AddSelectedFiles(String, String)" />
public void AddFiles(System.Collections.Generic.IEnumerable<String> fileNames)
{
this.AddFiles(fileNames, null);
}
/// <summary>
/// Adds or updates a set of files in the <c>ZipFile</c>.
/// </summary>
///
/// <remarks>
/// <para>
/// Any files that already exist in the archive are updated. Any files that
/// don't yet exist in the archive are added.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
/// respective values at the time of this call will be applied to each
/// ZipEntry added.
/// </para>
/// </remarks>
///
/// <param name="fileNames">
/// The collection of names of the files to update. Each string should refer to a file in
/// the filesystem. The name of the file may be a relative path or a fully-qualified path.
/// </param>
///
public void UpdateFiles(System.Collections.Generic.IEnumerable<String> fileNames)
{
this.UpdateFiles(fileNames, null);
}
/// <summary>
/// Adds a set of files to the <c>ZipFile</c>, using the
/// specified directory path in the archive.
/// </summary>
///
/// <remarks>
/// <para>
/// Any directory structure that may be present in the
/// filenames contained in the list is "flattened" in the
/// archive. Each file in the list is added to the archive in
/// the specified top-level directory.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see
/// cref="Encryption"/>, <see cref="Password"/>, <see
/// cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see
/// cref="ExtractExistingFile"/>, <see
/// cref="ZipErrorAction"/>, and <see
/// cref="CompressionLevel"/>, their respective values at the
/// time of this call will be applied to each ZipEntry added.
/// </para>
/// </remarks>
///
/// <param name="fileNames">
/// The names of the files to add. Each string should refer to
/// a file in the filesystem. The name of the file may be a
/// relative path or a fully-qualified path.
/// </param>
///
/// <param name="directoryPathInArchive">
/// Specifies a directory path to use to override any path in the file name.
/// Th is path may, or may not, correspond to a real directory in the current
/// filesystem. If the files within the zip are later extracted, this is the
/// path used for the extracted file. Passing <c>null</c> (<c>Nothing</c> in
/// VB) will use the path on each of the <c>fileNames</c>, if any. Passing
/// the empty string ("") will insert the item at the root path within the
/// archive.
/// </param>
///
/// <seealso cref="Ionic.Zip.ZipFile.AddSelectedFiles(String, String)" />
public void AddFiles(System.Collections.Generic.IEnumerable<String> fileNames, String directoryPathInArchive)
{
AddFiles(fileNames, false, directoryPathInArchive);
}
/// <summary>
/// Adds a set of files to the <c>ZipFile</c>, using the specified directory
/// path in the archive, and preserving the full directory structure in the
/// filenames.
/// </summary>
///
/// <remarks>
/// <para>
/// If preserveDirHierarchy is true, any directory structure present in the
/// filenames contained in the list is preserved in the archive. On the other
/// hand, if preserveDirHierarchy is false, any directory structure that may
/// be present in the filenames contained in the list is "flattened" in the
/// archive; Each file in the list is added to the archive in the specified
/// top-level directory.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
/// respective values at the time of this call will be applied to each
/// ZipEntry added.
/// </para>
///
/// </remarks>
///
/// <param name="fileNames">
/// The names of the files to add. Each string should refer to a file in the
/// filesystem. The name of the file may be a relative path or a
/// fully-qualified path.
/// </param>
///
/// <param name="directoryPathInArchive">
/// Specifies a directory path to use to override any path in the file name.
/// This path may, or may not, correspond to a real directory in the current
/// filesystem. If the files within the zip are later extracted, this is the
/// path used for the extracted file. Passing <c>null</c> (<c>Nothing</c> in
/// VB) will use the path on each of the <c>fileNames</c>, if any. Passing
/// the empty string ("") will insert the item at the root path within the
/// archive.
/// </param>
///
/// <param name="preserveDirHierarchy">
/// whether the entries in the zip archive will reflect the directory
/// hierarchy that is present in the various filenames. For example, if <paramref name="fileNames"/>
/// includes two paths, \Animalia\Chordata\Mammalia\Info.txt and
/// \Plantae\Magnoliophyta\Dicotyledon\Info.txt, then calling this method with
/// <paramref name="preserveDirHierarchy"/> = <c>false</c> will result in an
/// exception because of a duplicate entry name, while calling this method
/// with <paramref name="preserveDirHierarchy"/> = <c>true</c> will result in the
/// full direcory paths being included in the entries added to the ZipFile.
/// </param>
/// <seealso cref="Ionic.Zip.ZipFile.AddSelectedFiles(String, String)" />
public void AddFiles(System.Collections.Generic.IEnumerable<String> fileNames,
bool preserveDirHierarchy,
String directoryPathInArchive)
{
if (fileNames == null)
throw new ArgumentNullException("fileNames");
_addOperationCanceled = false;
OnAddStarted();
if (preserveDirHierarchy)
{
foreach (var f in fileNames)
{
if (_addOperationCanceled) break;
if (directoryPathInArchive != null)
{
//string s = SharedUtilities.NormalizePath(Path.Combine(directoryPathInArchive, Path.GetDirectoryName(f)));
string s = Path.GetFullPath(Path.Combine(directoryPathInArchive, Path.GetDirectoryName(f)));
this.AddFile(f, s);
}
else
this.AddFile(f, null);
}
}
else
{
foreach (var f in fileNames)
{
if (_addOperationCanceled) break;
this.AddFile(f, directoryPathInArchive);
}
}
if (!_addOperationCanceled)
OnAddCompleted();
}
/// <summary>
/// Adds or updates a set of files to the <c>ZipFile</c>, using the specified
/// directory path in the archive.
/// </summary>
///
/// <remarks>
///
/// <para>
/// Any files that already exist in the archive are updated. Any files that
/// don't yet exist in the archive are added.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
/// respective values at the time of this call will be applied to each
/// ZipEntry added.
/// </para>
/// </remarks>
///
/// <param name="fileNames">
/// The names of the files to add or update. Each string should refer to a
/// file in the filesystem. The name of the file may be a relative path or a
/// fully-qualified path.
/// </param>
///
/// <param name="directoryPathInArchive">
/// Specifies a directory path to use to override any path in the file name.
/// This path may, or may not, correspond to a real directory in the current
/// filesystem. If the files within the zip are later extracted, this is the
/// path used for the extracted file. Passing <c>null</c> (<c>Nothing</c> in
/// VB) will use the path on each of the <c>fileNames</c>, if any. Passing
/// the empty string ("") will insert the item at the root path within the
/// archive.
/// </param>
///
/// <seealso cref="Ionic.Zip.ZipFile.AddSelectedFiles(String, String)" />
public void UpdateFiles(System.Collections.Generic.IEnumerable<String> fileNames, String directoryPathInArchive)
{
if (fileNames == null)
throw new ArgumentNullException("fileNames");
OnAddStarted();
foreach (var f in fileNames)
this.UpdateFile(f, directoryPathInArchive);
OnAddCompleted();
}
/// <summary>
/// Adds or Updates a File in a Zip file archive.
/// </summary>
///
/// <remarks>
/// <para>
/// This method adds a file to a zip archive, or, if the file already exists
/// in the zip archive, this method Updates the content of that given filename
/// in the zip archive. The <c>UpdateFile</c> method might more accurately be
/// called "AddOrUpdateFile".
/// </para>
///
/// <para>
/// Upon success, there is no way for the application to learn whether the file
/// was added versus updated.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
/// respective values at the time of this call will be applied to the
/// <c>ZipEntry</c> added.
/// </para>
/// </remarks>
///
/// <example>
///
/// This example shows how to Update an existing entry in a zipfile. The first
/// call to UpdateFile adds the file to the newly-created zip archive. The
/// second call to UpdateFile updates the content for that file in the zip
/// archive.
///
/// <code>
/// using (ZipFile zip1 = new ZipFile())
/// {
/// // UpdateFile might more accurately be called "AddOrUpdateFile"
/// zip1.UpdateFile("MyDocuments\\Readme.txt");
/// zip1.UpdateFile("CustomerList.csv");
/// zip1.Comment = "This zip archive has been created.";
/// zip1.Save("Content.zip");
/// }
///
/// using (ZipFile zip2 = ZipFile.Read("Content.zip"))
/// {
/// zip2.UpdateFile("Updates\\Readme.txt");
/// zip2.Comment = "This zip archive has been updated: The Readme.txt file has been changed.";
/// zip2.Save();
/// }
///
/// </code>
/// <code lang="VB">
/// Using zip1 As New ZipFile
/// ' UpdateFile might more accurately be called "AddOrUpdateFile"
/// zip1.UpdateFile("MyDocuments\Readme.txt")
/// zip1.UpdateFile("CustomerList.csv")
/// zip1.Comment = "This zip archive has been created."
/// zip1.Save("Content.zip")
/// End Using
///
/// Using zip2 As ZipFile = ZipFile.Read("Content.zip")
/// zip2.UpdateFile("Updates\Readme.txt")
/// zip2.Comment = "This zip archive has been updated: The Readme.txt file has been changed."
/// zip2.Save
/// End Using
/// </code>
/// </example>
///
/// <seealso cref="Ionic.Zip.ZipFile.AddFile(string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.UpdateDirectory(string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string)"/>
///
/// <param name="fileName">
/// The name of the file to add or update. It should refer to a file in the
/// filesystem. The name of the file may be a relative path or a
/// fully-qualified path.
/// </param>
///
/// <returns>
/// The <c>ZipEntry</c> corresponding to the File that was added or updated.
/// </returns>
public ZipEntry UpdateFile(string fileName)
{
return UpdateFile(fileName, null);
}
/// <summary>
/// Adds or Updates a File in a Zip file archive.
/// </summary>
///
/// <remarks>
/// <para>
/// This method adds a file to a zip archive, or, if the file already exists
/// in the zip archive, this method Updates the content of that given filename
/// in the zip archive.
/// </para>
///
/// <para>
/// This version of the method allows the caller to explicitly specify the
/// directory path to be used in the archive. The entry to be added or
/// updated is found by using the specified directory path, combined with the
/// basename of the specified filename.
/// </para>
///
/// <para>
/// Upon success, there is no way for the application to learn if the file was
/// added versus updated.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
/// respective values at the time of this call will be applied to the
/// <c>ZipEntry</c> added.
/// </para>
/// </remarks>
///
/// <seealso cref="Ionic.Zip.ZipFile.AddFile(string,string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.UpdateDirectory(string,string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string,string)"/>
///
/// <param name="fileName">
/// The name of the file to add or update. It should refer to a file in the
/// filesystem. The name of the file may be a relative path or a
/// fully-qualified path.
/// </param>
///
/// <param name="directoryPathInArchive">
/// Specifies a directory path to use to override any path in the
/// <c>fileName</c>. This path may, or may not, correspond to a real
/// directory in the current filesystem. If the files within the zip are
/// later extracted, this is the path used for the extracted file. Passing
/// <c>null</c> (<c>Nothing</c> in VB) will use the path on the
/// <c>fileName</c>, if any. Passing the empty string ("") will insert the
/// item at the root path within the archive.
/// </param>
///
/// <returns>
/// The <c>ZipEntry</c> corresponding to the File that was added or updated.
/// </returns>
public ZipEntry UpdateFile(string fileName, String directoryPathInArchive)
{
// ideally this would all be transactional!
var key = ZipEntry.NameInArchive(fileName, directoryPathInArchive);
if (this[key] != null)
this.RemoveEntry(key);
return this.AddFile(fileName, directoryPathInArchive);
}
/// <summary>
/// Add or update a directory in a zip archive.
/// </summary>
///
/// <remarks>
/// If the specified directory does not exist in the archive, then this method
/// is equivalent to calling <c>AddDirectory()</c>. If the specified
/// directory already exists in the archive, then this method updates any
/// existing entries, and adds any new entries. Any entries that are in the
/// zip archive but not in the specified directory, are left alone. In other
/// words, the contents of the zip file will be a union of the previous
/// contents and the new files.
/// </remarks>
///
/// <seealso cref="Ionic.Zip.ZipFile.UpdateFile(string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string)"/>
///
/// <param name="directoryName">
/// The path to the directory to be added to the zip archive, or updated in
/// the zip archive.
/// </param>
///
/// <returns>
/// The <c>ZipEntry</c> corresponding to the Directory that was added or updated.
/// </returns>
public ZipEntry UpdateDirectory(string directoryName)
{
return UpdateDirectory(directoryName, null);
}
/// <summary>
/// Add or update a directory in the zip archive at the specified root
/// directory in the archive.
/// </summary>
///
/// <remarks>
/// If the specified directory does not exist in the archive, then this method
/// is equivalent to calling <c>AddDirectory()</c>. If the specified
/// directory already exists in the archive, then this method updates any
/// existing entries, and adds any new entries. Any entries that are in the
/// zip archive but not in the specified directory, are left alone. In other
/// words, the contents of the zip file will be a union of the previous
/// contents and the new files.
/// </remarks>
///
/// <seealso cref="Ionic.Zip.ZipFile.UpdateFile(string,string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string,string)"/>
/// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string,string)"/>
///
/// <param name="directoryName">
/// The path to the directory to be added to the zip archive, or updated
/// in the zip archive.
/// </param>
///
/// <param name="directoryPathInArchive">
/// Specifies a directory path to use to override any path in the
/// <c>directoryName</c>. This path may, or may not, correspond to a real
/// directory in the current filesystem. If the files within the zip are
/// later extracted, this is the path used for the extracted file. Passing
/// <c>null</c> (<c>Nothing</c> in VB) will use the path on the
/// <c>directoryName</c>, if any. Passing the empty string ("") will insert
/// the item at the root path within the archive.
/// </param>
///
/// <returns>
/// The <c>ZipEntry</c> corresponding to the Directory that was added or updated.
/// </returns>
public ZipEntry UpdateDirectory(string directoryName, String directoryPathInArchive)
{
return this.AddOrUpdateDirectoryImpl(directoryName, directoryPathInArchive, AddOrUpdateAction.AddOrUpdate);
}
/// <summary>
/// Add or update a file or directory in the zip archive.
/// </summary>
///
/// <remarks>
/// <para>
/// This is useful when the application is not sure or does not care if the
/// item to be added is a file or directory, and does not know or does not
/// care if the item already exists in the <c>ZipFile</c>. Calling this method
/// is equivalent to calling <c>RemoveEntry()</c> if an entry by the same name
/// already exists, followed calling by <c>AddItem()</c>.
/// </para>
///
/// <para>
/// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
/// cref="Password"/>, <see cref="SetCompression"/>, <see
/// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
/// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their