ZipFile.AddUpdate.cs 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184
  1. // ZipFile.AddUpdate.cs
  2. // ------------------------------------------------------------------
  3. //
  4. // Copyright (c) 2009-2011 Dino Chiesa.
  5. // All rights reserved.
  6. //
  7. // This code module is part of DotNetZip, a zipfile class library.
  8. //
  9. // ------------------------------------------------------------------
  10. //
  11. // This code is licensed under the Microsoft Public License.
  12. // See the file License.txt for the license details.
  13. // More info on: http://dotnetzip.codeplex.com
  14. //
  15. // ------------------------------------------------------------------
  16. //
  17. // last saved (in emacs):
  18. // Time-stamp: <2011-November-01 13:56:58>
  19. //
  20. // ------------------------------------------------------------------
  21. //
  22. // This module defines the methods for Adding and Updating entries in
  23. // the ZipFile.
  24. //
  25. // ------------------------------------------------------------------
  26. //
  27. using System;
  28. using System.IO;
  29. using System.Collections.Generic;
  30. namespace Ionic.Zip
  31. {
  32. public partial class ZipFile
  33. {
  34. /// <summary>
  35. /// Adds an item, either a file or a directory, to a zip file archive.
  36. /// </summary>
  37. ///
  38. /// <remarks>
  39. /// <para>
  40. /// This method is handy if you are adding things to zip archive and don't
  41. /// want to bother distinguishing between directories or files. Any files are
  42. /// added as single entries. A directory added through this method is added
  43. /// recursively: all files and subdirectories contained within the directory
  44. /// are added to the <c>ZipFile</c>.
  45. /// </para>
  46. ///
  47. /// <para>
  48. /// The name of the item may be a relative path or a fully-qualified
  49. /// path. Remember, the items contained in <c>ZipFile</c> instance get written
  50. /// to the disk only when you call <see cref="ZipFile.Save()"/> or a similar
  51. /// save method.
  52. /// </para>
  53. ///
  54. /// <para>
  55. /// The directory name used for the file within the archive is the same
  56. /// as the directory name (potentially a relative path) specified in the
  57. /// <paramref name="fileOrDirectoryName"/>.
  58. /// </para>
  59. ///
  60. /// <para>
  61. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  62. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  63. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  64. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  65. /// respective values at the time of this call will be applied to the
  66. /// <c>ZipEntry</c> added.
  67. /// </para>
  68. ///
  69. /// </remarks>
  70. ///
  71. /// <seealso cref="Ionic.Zip.ZipFile.AddFile(string)"/>
  72. /// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string)"/>
  73. /// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string)"/>
  74. ///
  75. /// <overloads>This method has two overloads.</overloads>
  76. /// <param name="fileOrDirectoryName">
  77. /// the name of the file or directory to add.</param>
  78. ///
  79. /// <returns>The <c>ZipEntry</c> added.</returns>
  80. public ZipEntry AddItem(string fileOrDirectoryName)
  81. {
  82. return AddItem(fileOrDirectoryName, null);
  83. }
  84. /// <summary>
  85. /// Adds an item, either a file or a directory, to a zip file archive,
  86. /// explicitly specifying the directory path to be used in the archive.
  87. /// </summary>
  88. ///
  89. /// <remarks>
  90. /// <para>
  91. /// If adding a directory, the add is recursive on all files and
  92. /// subdirectories contained within it.
  93. /// </para>
  94. /// <para>
  95. /// The name of the item may be a relative path or a fully-qualified path.
  96. /// The item added by this call to the <c>ZipFile</c> is not read from the
  97. /// disk nor written to the zip file archive until the application calls
  98. /// Save() on the <c>ZipFile</c>.
  99. /// </para>
  100. ///
  101. /// <para>
  102. /// This version of the method allows the caller to explicitly specify the
  103. /// directory path to be used in the archive, which would override the
  104. /// "natural" path of the filesystem file.
  105. /// </para>
  106. ///
  107. /// <para>
  108. /// Encryption will be used on the file data if the <c>Password</c> has
  109. /// been set on the <c>ZipFile</c> object, prior to calling this method.
  110. /// </para>
  111. ///
  112. /// <para>
  113. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  114. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  115. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  116. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  117. /// respective values at the time of this call will be applied to the
  118. /// <c>ZipEntry</c> added.
  119. /// </para>
  120. ///
  121. /// </remarks>
  122. ///
  123. /// <exception cref="System.IO.FileNotFoundException">
  124. /// Thrown if the file or directory passed in does not exist.
  125. /// </exception>
  126. ///
  127. /// <param name="fileOrDirectoryName">the name of the file or directory to add.
  128. /// </param>
  129. ///
  130. /// <param name="directoryPathInArchive">
  131. /// The name of the directory path to use within the zip archive. This path
  132. /// need not refer to an extant directory in the current filesystem. If the
  133. /// files within the zip are later extracted, this is the path used for the
  134. /// extracted file. Passing <c>null</c> (<c>Nothing</c> in VB) will use the
  135. /// path on the fileOrDirectoryName. Passing the empty string ("") will
  136. /// insert the item at the root path within the archive.
  137. /// </param>
  138. ///
  139. /// <seealso cref="Ionic.Zip.ZipFile.AddFile(string, string)"/>
  140. /// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string, string)"/>
  141. /// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string, string)"/>
  142. ///
  143. /// <example>
  144. /// This example shows how to zip up a set of files into a flat hierarchy,
  145. /// regardless of where in the filesystem the files originated. The resulting
  146. /// zip archive will contain a toplevel directory named "flat", which itself
  147. /// will contain files Readme.txt, MyProposal.docx, and Image1.jpg. A
  148. /// subdirectory under "flat" called SupportFiles will contain all the files
  149. /// in the "c:\SupportFiles" directory on disk.
  150. ///
  151. /// <code>
  152. /// String[] itemnames= {
  153. /// "c:\\fixedContent\\Readme.txt",
  154. /// "MyProposal.docx",
  155. /// "c:\\SupportFiles", // a directory
  156. /// "images\\Image1.jpg"
  157. /// };
  158. ///
  159. /// try
  160. /// {
  161. /// using (ZipFile zip = new ZipFile())
  162. /// {
  163. /// for (int i = 1; i &lt; itemnames.Length; i++)
  164. /// {
  165. /// // will add Files or Dirs, recurses and flattens subdirectories
  166. /// zip.AddItem(itemnames[i],"flat");
  167. /// }
  168. /// zip.Save(ZipToCreate);
  169. /// }
  170. /// }
  171. /// catch (System.Exception ex1)
  172. /// {
  173. /// System.Console.Error.WriteLine("exception: {0}", ex1);
  174. /// }
  175. /// </code>
  176. ///
  177. /// <code lang="VB">
  178. /// Dim itemnames As String() = _
  179. /// New String() { "c:\fixedContent\Readme.txt", _
  180. /// "MyProposal.docx", _
  181. /// "SupportFiles", _
  182. /// "images\Image1.jpg" }
  183. /// Try
  184. /// Using zip As New ZipFile
  185. /// Dim i As Integer
  186. /// For i = 1 To itemnames.Length - 1
  187. /// ' will add Files or Dirs, recursing and flattening subdirectories.
  188. /// zip.AddItem(itemnames(i), "flat")
  189. /// Next i
  190. /// zip.Save(ZipToCreate)
  191. /// End Using
  192. /// Catch ex1 As Exception
  193. /// Console.Error.WriteLine("exception: {0}", ex1.ToString())
  194. /// End Try
  195. /// </code>
  196. /// </example>
  197. /// <returns>The <c>ZipEntry</c> added.</returns>
  198. public ZipEntry AddItem(String fileOrDirectoryName, String directoryPathInArchive)
  199. {
  200. if (File.Exists(fileOrDirectoryName))
  201. return AddFile(fileOrDirectoryName, directoryPathInArchive);
  202. if (Directory.Exists(fileOrDirectoryName))
  203. return AddDirectory(fileOrDirectoryName, directoryPathInArchive);
  204. throw new FileNotFoundException(String.Format("That file or directory ({0}) does not exist!",
  205. fileOrDirectoryName));
  206. }
  207. /// <summary>
  208. /// Adds a File to a Zip file archive.
  209. /// </summary>
  210. /// <remarks>
  211. ///
  212. /// <para>
  213. /// This call collects metadata for the named file in the filesystem,
  214. /// including the file attributes and the timestamp, and inserts that metadata
  215. /// into the resulting ZipEntry. Only when the application calls Save() on
  216. /// the <c>ZipFile</c>, does DotNetZip read the file from the filesystem and
  217. /// then write the content to the zip file archive.
  218. /// </para>
  219. ///
  220. /// <para>
  221. /// This method will throw an exception if an entry with the same name already
  222. /// exists in the <c>ZipFile</c>.
  223. /// </para>
  224. ///
  225. /// <para>
  226. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  227. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  228. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  229. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  230. /// respective values at the time of this call will be applied to the
  231. /// <c>ZipEntry</c> added.
  232. /// </para>
  233. ///
  234. /// </remarks>
  235. ///
  236. /// <example>
  237. /// <para>
  238. /// In this example, three files are added to a Zip archive. The ReadMe.txt
  239. /// file will be placed in the root of the archive. The .png file will be
  240. /// placed in a folder within the zip called photos\personal. The pdf file
  241. /// will be included into a folder within the zip called Desktop.
  242. /// </para>
  243. /// <code>
  244. /// try
  245. /// {
  246. /// using (ZipFile zip = new ZipFile())
  247. /// {
  248. /// zip.AddFile("c:\\photos\\personal\\7440-N49th.png");
  249. /// zip.AddFile("c:\\Desktop\\2008-Regional-Sales-Report.pdf");
  250. /// zip.AddFile("ReadMe.txt");
  251. ///
  252. /// zip.Save("Package.zip");
  253. /// }
  254. /// }
  255. /// catch (System.Exception ex1)
  256. /// {
  257. /// System.Console.Error.WriteLine("exception: " + ex1);
  258. /// }
  259. /// </code>
  260. ///
  261. /// <code lang="VB">
  262. /// Try
  263. /// Using zip As ZipFile = New ZipFile
  264. /// zip.AddFile("c:\photos\personal\7440-N49th.png")
  265. /// zip.AddFile("c:\Desktop\2008-Regional-Sales-Report.pdf")
  266. /// zip.AddFile("ReadMe.txt")
  267. /// zip.Save("Package.zip")
  268. /// End Using
  269. /// Catch ex1 As Exception
  270. /// Console.Error.WriteLine("exception: {0}", ex1.ToString)
  271. /// End Try
  272. /// </code>
  273. /// </example>
  274. ///
  275. /// <overloads>This method has two overloads.</overloads>
  276. ///
  277. /// <seealso cref="Ionic.Zip.ZipFile.AddItem(string)"/>
  278. /// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string)"/>
  279. /// <seealso cref="Ionic.Zip.ZipFile.UpdateFile(string)"/>
  280. ///
  281. /// <param name="fileName">
  282. /// The name of the file to add. It should refer to a file in the filesystem.
  283. /// The name of the file may be a relative path or a fully-qualified path.
  284. /// </param>
  285. /// <returns>The <c>ZipEntry</c> corresponding to the File added.</returns>
  286. public ZipEntry AddFile(string fileName)
  287. {
  288. return AddFile(fileName, null);
  289. }
  290. /// <summary>
  291. /// Adds a File to a Zip file archive, potentially overriding the path to be
  292. /// used within the zip archive.
  293. /// </summary>
  294. ///
  295. /// <remarks>
  296. /// <para>
  297. /// The file added by this call to the <c>ZipFile</c> is not written to the
  298. /// zip file archive until the application calls Save() on the <c>ZipFile</c>.
  299. /// </para>
  300. ///
  301. /// <para>
  302. /// This method will throw an exception if an entry with the same name already
  303. /// exists in the <c>ZipFile</c>.
  304. /// </para>
  305. ///
  306. /// <para>
  307. /// This version of the method allows the caller to explicitly specify the
  308. /// directory path to be used in the archive.
  309. /// </para>
  310. ///
  311. /// <para>
  312. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  313. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  314. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  315. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  316. /// respective values at the time of this call will be applied to the
  317. /// <c>ZipEntry</c> added.
  318. /// </para>
  319. ///
  320. /// </remarks>
  321. ///
  322. /// <example>
  323. /// <para>
  324. /// In this example, three files are added to a Zip archive. The ReadMe.txt
  325. /// file will be placed in the root of the archive. The .png file will be
  326. /// placed in a folder within the zip called images. The pdf file will be
  327. /// included into a folder within the zip called files\docs, and will be
  328. /// encrypted with the given password.
  329. /// </para>
  330. /// <code>
  331. /// try
  332. /// {
  333. /// using (ZipFile zip = new ZipFile())
  334. /// {
  335. /// // the following entry will be inserted at the root in the archive.
  336. /// zip.AddFile("c:\\datafiles\\ReadMe.txt", "");
  337. /// // this image file will be inserted into the "images" directory in the archive.
  338. /// zip.AddFile("c:\\photos\\personal\\7440-N49th.png", "images");
  339. /// // the following will result in a password-protected file called
  340. /// // files\\docs\\2008-Regional-Sales-Report.pdf in the archive.
  341. /// zip.Password = "EncryptMe!";
  342. /// zip.AddFile("c:\\Desktop\\2008-Regional-Sales-Report.pdf", "files\\docs");
  343. /// zip.Save("Archive.zip");
  344. /// }
  345. /// }
  346. /// catch (System.Exception ex1)
  347. /// {
  348. /// System.Console.Error.WriteLine("exception: {0}", ex1);
  349. /// }
  350. /// </code>
  351. ///
  352. /// <code lang="VB">
  353. /// Try
  354. /// Using zip As ZipFile = New ZipFile
  355. /// ' the following entry will be inserted at the root in the archive.
  356. /// zip.AddFile("c:\datafiles\ReadMe.txt", "")
  357. /// ' this image file will be inserted into the "images" directory in the archive.
  358. /// zip.AddFile("c:\photos\personal\7440-N49th.png", "images")
  359. /// ' the following will result in a password-protected file called
  360. /// ' files\\docs\\2008-Regional-Sales-Report.pdf in the archive.
  361. /// zip.Password = "EncryptMe!"
  362. /// zip.AddFile("c:\Desktop\2008-Regional-Sales-Report.pdf", "files\documents")
  363. /// zip.Save("Archive.zip")
  364. /// End Using
  365. /// Catch ex1 As Exception
  366. /// Console.Error.WriteLine("exception: {0}", ex1)
  367. /// End Try
  368. /// </code>
  369. /// </example>
  370. ///
  371. /// <seealso cref="Ionic.Zip.ZipFile.AddItem(string,string)"/>
  372. /// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string, string)"/>
  373. /// <seealso cref="Ionic.Zip.ZipFile.UpdateFile(string,string)"/>
  374. ///
  375. /// <param name="fileName">
  376. /// The name of the file to add. The name of the file may be a relative path
  377. /// or a fully-qualified path.
  378. /// </param>
  379. ///
  380. /// <param name="directoryPathInArchive">
  381. /// Specifies a directory path to use to override any path in the fileName.
  382. /// This path may, or may not, correspond to a real directory in the current
  383. /// filesystem. If the files within the zip are later extracted, this is the
  384. /// path used for the extracted file. Passing <c>null</c> (<c>Nothing</c> in
  385. /// VB) will use the path on the fileName, if any. Passing the empty string
  386. /// ("") will insert the item at the root path within the archive.
  387. /// </param>
  388. ///
  389. /// <returns>The <c>ZipEntry</c> corresponding to the file added.</returns>
  390. public ZipEntry AddFile(string fileName, String directoryPathInArchive)
  391. {
  392. string nameInArchive = ZipEntry.NameInArchive(fileName, directoryPathInArchive);
  393. ZipEntry ze = ZipEntry.CreateFromFile(fileName, nameInArchive);
  394. if (Verbose) StatusMessageTextWriter.WriteLine("adding {0}...", fileName);
  395. return _InternalAddEntry(ze);
  396. }
  397. /// <summary>
  398. /// This method removes a collection of entries from the <c>ZipFile</c>.
  399. /// </summary>
  400. ///
  401. /// <param name="entriesToRemove">
  402. /// A collection of ZipEntry instances from this zip file to be removed. For
  403. /// example, you can pass in an array of ZipEntry instances; or you can call
  404. /// SelectEntries(), and then add or remove entries from that
  405. /// ICollection&lt;ZipEntry&gt; (ICollection(Of ZipEntry) in VB), and pass
  406. /// that ICollection to this method.
  407. /// </param>
  408. ///
  409. /// <seealso cref="Ionic.Zip.ZipFile.SelectEntries(String)" />
  410. /// <seealso cref="Ionic.Zip.ZipFile.RemoveSelectedEntries(String)" />
  411. public void RemoveEntries(System.Collections.Generic.ICollection<ZipEntry> entriesToRemove)
  412. {
  413. if (entriesToRemove == null)
  414. throw new ArgumentNullException("entriesToRemove");
  415. foreach (ZipEntry e in entriesToRemove)
  416. {
  417. this.RemoveEntry(e);
  418. }
  419. }
  420. /// <summary>
  421. /// This method removes a collection of entries from the <c>ZipFile</c>, by name.
  422. /// </summary>
  423. ///
  424. /// <param name="entriesToRemove">
  425. /// A collection of strings that refer to names of entries to be removed
  426. /// from the <c>ZipFile</c>. For example, you can pass in an array or a
  427. /// List of Strings that provide the names of entries to be removed.
  428. /// </param>
  429. ///
  430. /// <seealso cref="Ionic.Zip.ZipFile.SelectEntries(String)" />
  431. /// <seealso cref="Ionic.Zip.ZipFile.RemoveSelectedEntries(String)" />
  432. public void RemoveEntries(System.Collections.Generic.ICollection<String> entriesToRemove)
  433. {
  434. if (entriesToRemove == null)
  435. throw new ArgumentNullException("entriesToRemove");
  436. foreach (String e in entriesToRemove)
  437. {
  438. this.RemoveEntry(e);
  439. }
  440. }
  441. /// <summary>
  442. /// This method adds a set of files to the <c>ZipFile</c>.
  443. /// </summary>
  444. ///
  445. /// <remarks>
  446. /// <para>
  447. /// Use this method to add a set of files to the zip archive, in one call.
  448. /// For example, a list of files received from
  449. /// <c>System.IO.Directory.GetFiles()</c> can be added to a zip archive in one
  450. /// call.
  451. /// </para>
  452. ///
  453. /// <para>
  454. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  455. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  456. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  457. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  458. /// respective values at the time of this call will be applied to each
  459. /// ZipEntry added.
  460. /// </para>
  461. /// </remarks>
  462. ///
  463. /// <param name="fileNames">
  464. /// The collection of names of the files to add. Each string should refer to a
  465. /// file in the filesystem. The name of the file may be a relative path or a
  466. /// fully-qualified path.
  467. /// </param>
  468. ///
  469. /// <example>
  470. /// This example shows how to create a zip file, and add a few files into it.
  471. /// <code>
  472. /// String ZipFileToCreate = "archive1.zip";
  473. /// String DirectoryToZip = "c:\\reports";
  474. /// using (ZipFile zip = new ZipFile())
  475. /// {
  476. /// // Store all files found in the top level directory, into the zip archive.
  477. /// String[] filenames = System.IO.Directory.GetFiles(DirectoryToZip);
  478. /// zip.AddFiles(filenames);
  479. /// zip.Save(ZipFileToCreate);
  480. /// }
  481. /// </code>
  482. ///
  483. /// <code lang="VB">
  484. /// Dim ZipFileToCreate As String = "archive1.zip"
  485. /// Dim DirectoryToZip As String = "c:\reports"
  486. /// Using zip As ZipFile = New ZipFile
  487. /// ' Store all files found in the top level directory, into the zip archive.
  488. /// Dim filenames As String() = System.IO.Directory.GetFiles(DirectoryToZip)
  489. /// zip.AddFiles(filenames)
  490. /// zip.Save(ZipFileToCreate)
  491. /// End Using
  492. /// </code>
  493. /// </example>
  494. ///
  495. /// <seealso cref="Ionic.Zip.ZipFile.AddSelectedFiles(String, String)" />
  496. public void AddFiles(System.Collections.Generic.IEnumerable<String> fileNames)
  497. {
  498. this.AddFiles(fileNames, null);
  499. }
  500. /// <summary>
  501. /// Adds or updates a set of files in the <c>ZipFile</c>.
  502. /// </summary>
  503. ///
  504. /// <remarks>
  505. /// <para>
  506. /// Any files that already exist in the archive are updated. Any files that
  507. /// don't yet exist in the archive are added.
  508. /// </para>
  509. ///
  510. /// <para>
  511. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  512. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  513. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  514. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  515. /// respective values at the time of this call will be applied to each
  516. /// ZipEntry added.
  517. /// </para>
  518. /// </remarks>
  519. ///
  520. /// <param name="fileNames">
  521. /// The collection of names of the files to update. Each string should refer to a file in
  522. /// the filesystem. The name of the file may be a relative path or a fully-qualified path.
  523. /// </param>
  524. ///
  525. public void UpdateFiles(System.Collections.Generic.IEnumerable<String> fileNames)
  526. {
  527. this.UpdateFiles(fileNames, null);
  528. }
  529. /// <summary>
  530. /// Adds a set of files to the <c>ZipFile</c>, using the
  531. /// specified directory path in the archive.
  532. /// </summary>
  533. ///
  534. /// <remarks>
  535. /// <para>
  536. /// Any directory structure that may be present in the
  537. /// filenames contained in the list is "flattened" in the
  538. /// archive. Each file in the list is added to the archive in
  539. /// the specified top-level directory.
  540. /// </para>
  541. ///
  542. /// <para>
  543. /// For <c>ZipFile</c> properties including <see
  544. /// cref="Encryption"/>, <see cref="Password"/>, <see
  545. /// cref="SetCompression"/>, <see
  546. /// cref="ProvisionalAlternateEncoding"/>, <see
  547. /// cref="ExtractExistingFile"/>, <see
  548. /// cref="ZipErrorAction"/>, and <see
  549. /// cref="CompressionLevel"/>, their respective values at the
  550. /// time of this call will be applied to each ZipEntry added.
  551. /// </para>
  552. /// </remarks>
  553. ///
  554. /// <param name="fileNames">
  555. /// The names of the files to add. Each string should refer to
  556. /// a file in the filesystem. The name of the file may be a
  557. /// relative path or a fully-qualified path.
  558. /// </param>
  559. ///
  560. /// <param name="directoryPathInArchive">
  561. /// Specifies a directory path to use to override any path in the file name.
  562. /// Th is path may, or may not, correspond to a real directory in the current
  563. /// filesystem. If the files within the zip are later extracted, this is the
  564. /// path used for the extracted file. Passing <c>null</c> (<c>Nothing</c> in
  565. /// VB) will use the path on each of the <c>fileNames</c>, if any. Passing
  566. /// the empty string ("") will insert the item at the root path within the
  567. /// archive.
  568. /// </param>
  569. ///
  570. /// <seealso cref="Ionic.Zip.ZipFile.AddSelectedFiles(String, String)" />
  571. public void AddFiles(System.Collections.Generic.IEnumerable<String> fileNames, String directoryPathInArchive)
  572. {
  573. AddFiles(fileNames, false, directoryPathInArchive);
  574. }
  575. /// <summary>
  576. /// Adds a set of files to the <c>ZipFile</c>, using the specified directory
  577. /// path in the archive, and preserving the full directory structure in the
  578. /// filenames.
  579. /// </summary>
  580. ///
  581. /// <remarks>
  582. ///
  583. /// <para>
  584. /// Think of the <paramref name="directoryPathInArchive"/> as a "root" or
  585. /// base directory used in the archive for the files that get added. when
  586. /// <paramref name="preserveDirHierarchy"/> is true, the hierarchy of files
  587. /// found in the filesystem will be placed, with the hierarchy intact,
  588. /// starting at that root in the archive. When <c>preserveDirHierarchy</c>
  589. /// is false, the path hierarchy of files is flattned, and the flattened
  590. /// set of files gets placed in the root within the archive as specified in
  591. /// <c>directoryPathInArchive</c>.
  592. /// </para>
  593. ///
  594. /// <para>
  595. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  596. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  597. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  598. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  599. /// respective values at the time of this call will be applied to each
  600. /// ZipEntry added.
  601. /// </para>
  602. ///
  603. /// </remarks>
  604. ///
  605. /// <param name="fileNames">
  606. /// The names of the files to add. Each string should refer to a file in the
  607. /// filesystem. The name of the file may be a relative path or a
  608. /// fully-qualified path.
  609. /// </param>
  610. ///
  611. /// <param name="directoryPathInArchive">
  612. /// Specifies a directory path to use as a prefix for each entry name.
  613. /// This path may, or may not, correspond to a real directory in the current
  614. /// filesystem. If the files within the zip are later extracted, this is the
  615. /// path used for the extracted file. Passing <c>null</c> (<c>Nothing</c> in
  616. /// VB) will use the path on each of the <c>fileNames</c>, if any. Passing
  617. /// the empty string ("") will insert the item at the root path within the
  618. /// archive.
  619. /// </param>
  620. ///
  621. /// <param name="preserveDirHierarchy">
  622. /// whether the entries in the zip archive will reflect the directory
  623. /// hierarchy that is present in the various filenames. For example, if
  624. /// <paramref name="fileNames"/> includes two paths,
  625. /// \Animalia\Chordata\Mammalia\Info.txt and
  626. /// \Plantae\Magnoliophyta\Dicotyledon\Info.txt, then calling this method
  627. /// with <paramref name="preserveDirHierarchy"/> = <c>false</c> will
  628. /// result in an exception because of a duplicate entry name, while
  629. /// calling this method with <paramref name="preserveDirHierarchy"/> =
  630. /// <c>true</c> will result in the full direcory paths being included in
  631. /// the entries added to the ZipFile.
  632. /// </param>
  633. /// <seealso cref="Ionic.Zip.ZipFile.AddSelectedFiles(String, String)" />
  634. public void AddFiles(System.Collections.Generic.IEnumerable<String> fileNames,
  635. bool preserveDirHierarchy,
  636. String directoryPathInArchive)
  637. {
  638. if (fileNames == null)
  639. throw new ArgumentNullException("fileNames");
  640. _addOperationCanceled = false;
  641. OnAddStarted();
  642. if (preserveDirHierarchy)
  643. {
  644. foreach (var f in fileNames)
  645. {
  646. if (_addOperationCanceled) break;
  647. if (directoryPathInArchive != null)
  648. {
  649. //string s = SharedUtilities.NormalizePath(Path.Combine(directoryPathInArchive, Path.GetDirectoryName(f)));
  650. string s = Path.GetFullPath(Path.Combine(directoryPathInArchive, Path.GetDirectoryName(f)));
  651. this.AddFile(f, s);
  652. }
  653. else
  654. this.AddFile(f, null);
  655. }
  656. }
  657. else
  658. {
  659. foreach (var f in fileNames)
  660. {
  661. if (_addOperationCanceled) break;
  662. this.AddFile(f, directoryPathInArchive);
  663. }
  664. }
  665. if (!_addOperationCanceled)
  666. OnAddCompleted();
  667. }
  668. /// <summary>
  669. /// Adds or updates a set of files to the <c>ZipFile</c>, using the specified
  670. /// directory path in the archive.
  671. /// </summary>
  672. ///
  673. /// <remarks>
  674. ///
  675. /// <para>
  676. /// Any files that already exist in the archive are updated. Any files that
  677. /// don't yet exist in the archive are added.
  678. /// </para>
  679. ///
  680. /// <para>
  681. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  682. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  683. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  684. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  685. /// respective values at the time of this call will be applied to each
  686. /// ZipEntry added.
  687. /// </para>
  688. /// </remarks>
  689. ///
  690. /// <param name="fileNames">
  691. /// The names of the files to add or update. Each string should refer to a
  692. /// file in the filesystem. The name of the file may be a relative path or a
  693. /// fully-qualified path.
  694. /// </param>
  695. ///
  696. /// <param name="directoryPathInArchive">
  697. /// Specifies a directory path to use to override any path in the file name.
  698. /// This path may, or may not, correspond to a real directory in the current
  699. /// filesystem. If the files within the zip are later extracted, this is the
  700. /// path used for the extracted file. Passing <c>null</c> (<c>Nothing</c> in
  701. /// VB) will use the path on each of the <c>fileNames</c>, if any. Passing
  702. /// the empty string ("") will insert the item at the root path within the
  703. /// archive.
  704. /// </param>
  705. ///
  706. /// <seealso cref="Ionic.Zip.ZipFile.AddSelectedFiles(String, String)" />
  707. public void UpdateFiles(System.Collections.Generic.IEnumerable<String> fileNames, String directoryPathInArchive)
  708. {
  709. if (fileNames == null)
  710. throw new ArgumentNullException("fileNames");
  711. OnAddStarted();
  712. foreach (var f in fileNames)
  713. this.UpdateFile(f, directoryPathInArchive);
  714. OnAddCompleted();
  715. }
  716. /// <summary>
  717. /// Adds or Updates a File in a Zip file archive.
  718. /// </summary>
  719. ///
  720. /// <remarks>
  721. /// <para>
  722. /// This method adds a file to a zip archive, or, if the file already exists
  723. /// in the zip archive, this method Updates the content of that given filename
  724. /// in the zip archive. The <c>UpdateFile</c> method might more accurately be
  725. /// called "AddOrUpdateFile".
  726. /// </para>
  727. ///
  728. /// <para>
  729. /// Upon success, there is no way for the application to learn whether the file
  730. /// was added versus updated.
  731. /// </para>
  732. ///
  733. /// <para>
  734. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  735. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  736. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  737. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  738. /// respective values at the time of this call will be applied to the
  739. /// <c>ZipEntry</c> added.
  740. /// </para>
  741. /// </remarks>
  742. ///
  743. /// <example>
  744. ///
  745. /// This example shows how to Update an existing entry in a zipfile. The first
  746. /// call to UpdateFile adds the file to the newly-created zip archive. The
  747. /// second call to UpdateFile updates the content for that file in the zip
  748. /// archive.
  749. ///
  750. /// <code>
  751. /// using (ZipFile zip1 = new ZipFile())
  752. /// {
  753. /// // UpdateFile might more accurately be called "AddOrUpdateFile"
  754. /// zip1.UpdateFile("MyDocuments\\Readme.txt");
  755. /// zip1.UpdateFile("CustomerList.csv");
  756. /// zip1.Comment = "This zip archive has been created.";
  757. /// zip1.Save("Content.zip");
  758. /// }
  759. ///
  760. /// using (ZipFile zip2 = ZipFile.Read("Content.zip"))
  761. /// {
  762. /// zip2.UpdateFile("Updates\\Readme.txt");
  763. /// zip2.Comment = "This zip archive has been updated: The Readme.txt file has been changed.";
  764. /// zip2.Save();
  765. /// }
  766. ///
  767. /// </code>
  768. /// <code lang="VB">
  769. /// Using zip1 As New ZipFile
  770. /// ' UpdateFile might more accurately be called "AddOrUpdateFile"
  771. /// zip1.UpdateFile("MyDocuments\Readme.txt")
  772. /// zip1.UpdateFile("CustomerList.csv")
  773. /// zip1.Comment = "This zip archive has been created."
  774. /// zip1.Save("Content.zip")
  775. /// End Using
  776. ///
  777. /// Using zip2 As ZipFile = ZipFile.Read("Content.zip")
  778. /// zip2.UpdateFile("Updates\Readme.txt")
  779. /// zip2.Comment = "This zip archive has been updated: The Readme.txt file has been changed."
  780. /// zip2.Save
  781. /// End Using
  782. /// </code>
  783. /// </example>
  784. ///
  785. /// <seealso cref="Ionic.Zip.ZipFile.AddFile(string)"/>
  786. /// <seealso cref="Ionic.Zip.ZipFile.UpdateDirectory(string)"/>
  787. /// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string)"/>
  788. ///
  789. /// <param name="fileName">
  790. /// The name of the file to add or update. It should refer to a file in the
  791. /// filesystem. The name of the file may be a relative path or a
  792. /// fully-qualified path.
  793. /// </param>
  794. ///
  795. /// <returns>
  796. /// The <c>ZipEntry</c> corresponding to the File that was added or updated.
  797. /// </returns>
  798. public ZipEntry UpdateFile(string fileName)
  799. {
  800. return UpdateFile(fileName, null);
  801. }
  802. /// <summary>
  803. /// Adds or Updates a File in a Zip file archive.
  804. /// </summary>
  805. ///
  806. /// <remarks>
  807. /// <para>
  808. /// This method adds a file to a zip archive, or, if the file already exists
  809. /// in the zip archive, this method Updates the content of that given filename
  810. /// in the zip archive.
  811. /// </para>
  812. ///
  813. /// <para>
  814. /// This version of the method allows the caller to explicitly specify the
  815. /// directory path to be used in the archive. The entry to be added or
  816. /// updated is found by using the specified directory path, combined with the
  817. /// basename of the specified filename.
  818. /// </para>
  819. ///
  820. /// <para>
  821. /// Upon success, there is no way for the application to learn if the file was
  822. /// added versus updated.
  823. /// </para>
  824. ///
  825. /// <para>
  826. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  827. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  828. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  829. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  830. /// respective values at the time of this call will be applied to the
  831. /// <c>ZipEntry</c> added.
  832. /// </para>
  833. /// </remarks>
  834. ///
  835. /// <seealso cref="Ionic.Zip.ZipFile.AddFile(string,string)"/>
  836. /// <seealso cref="Ionic.Zip.ZipFile.UpdateDirectory(string,string)"/>
  837. /// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string,string)"/>
  838. ///
  839. /// <param name="fileName">
  840. /// The name of the file to add or update. It should refer to a file in the
  841. /// filesystem. The name of the file may be a relative path or a
  842. /// fully-qualified path.
  843. /// </param>
  844. ///
  845. /// <param name="directoryPathInArchive">
  846. /// Specifies a directory path to use to override any path in the
  847. /// <c>fileName</c>. This path may, or may not, correspond to a real
  848. /// directory in the current filesystem. If the files within the zip are
  849. /// later extracted, this is the path used for the extracted file. Passing
  850. /// <c>null</c> (<c>Nothing</c> in VB) will use the path on the
  851. /// <c>fileName</c>, if any. Passing the empty string ("") will insert the
  852. /// item at the root path within the archive.
  853. /// </param>
  854. ///
  855. /// <returns>
  856. /// The <c>ZipEntry</c> corresponding to the File that was added or updated.
  857. /// </returns>
  858. public ZipEntry UpdateFile(string fileName, String directoryPathInArchive)
  859. {
  860. // ideally this would all be transactional!
  861. var key = ZipEntry.NameInArchive(fileName, directoryPathInArchive);
  862. if (this[key] != null)
  863. this.RemoveEntry(key);
  864. return this.AddFile(fileName, directoryPathInArchive);
  865. }
  866. /// <summary>
  867. /// Add or update a directory in a zip archive.
  868. /// </summary>
  869. ///
  870. /// <remarks>
  871. /// If the specified directory does not exist in the archive, then this method
  872. /// is equivalent to calling <c>AddDirectory()</c>. If the specified
  873. /// directory already exists in the archive, then this method updates any
  874. /// existing entries, and adds any new entries. Any entries that are in the
  875. /// zip archive but not in the specified directory, are left alone. In other
  876. /// words, the contents of the zip file will be a union of the previous
  877. /// contents and the new files.
  878. /// </remarks>
  879. ///
  880. /// <seealso cref="Ionic.Zip.ZipFile.UpdateFile(string)"/>
  881. /// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string)"/>
  882. /// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string)"/>
  883. ///
  884. /// <param name="directoryName">
  885. /// The path to the directory to be added to the zip archive, or updated in
  886. /// the zip archive.
  887. /// </param>
  888. ///
  889. /// <returns>
  890. /// The <c>ZipEntry</c> corresponding to the Directory that was added or updated.
  891. /// </returns>
  892. public ZipEntry UpdateDirectory(string directoryName)
  893. {
  894. return UpdateDirectory(directoryName, null);
  895. }
  896. /// <summary>
  897. /// Add or update a directory in the zip archive at the specified root
  898. /// directory in the archive.
  899. /// </summary>
  900. ///
  901. /// <remarks>
  902. /// If the specified directory does not exist in the archive, then this method
  903. /// is equivalent to calling <c>AddDirectory()</c>. If the specified
  904. /// directory already exists in the archive, then this method updates any
  905. /// existing entries, and adds any new entries. Any entries that are in the
  906. /// zip archive but not in the specified directory, are left alone. In other
  907. /// words, the contents of the zip file will be a union of the previous
  908. /// contents and the new files.
  909. /// </remarks>
  910. ///
  911. /// <seealso cref="Ionic.Zip.ZipFile.UpdateFile(string,string)"/>
  912. /// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string,string)"/>
  913. /// <seealso cref="Ionic.Zip.ZipFile.UpdateItem(string,string)"/>
  914. ///
  915. /// <param name="directoryName">
  916. /// The path to the directory to be added to the zip archive, or updated
  917. /// in the zip archive.
  918. /// </param>
  919. ///
  920. /// <param name="directoryPathInArchive">
  921. /// Specifies a directory path to use to override any path in the
  922. /// <c>directoryName</c>. This path may, or may not, correspond to a real
  923. /// directory in the current filesystem. If the files within the zip are
  924. /// later extracted, this is the path used for the extracted file. Passing
  925. /// <c>null</c> (<c>Nothing</c> in VB) will use the path on the
  926. /// <c>directoryName</c>, if any. Passing the empty string ("") will insert
  927. /// the item at the root path within the archive.
  928. /// </param>
  929. ///
  930. /// <returns>
  931. /// The <c>ZipEntry</c> corresponding to the Directory that was added or updated.
  932. /// </returns>
  933. public ZipEntry UpdateDirectory(string directoryName, String directoryPathInArchive)
  934. {
  935. return this.AddOrUpdateDirectoryImpl(directoryName, directoryPathInArchive, AddOrUpdateAction.AddOrUpdate);
  936. }
  937. /// <summary>
  938. /// Add or update a file or directory in the zip archive.
  939. /// </summary>
  940. ///
  941. /// <remarks>
  942. /// <para>
  943. /// This is useful when the application is not sure or does not care if the
  944. /// item to be added is a file or directory, and does not know or does not
  945. /// care if the item already exists in the <c>ZipFile</c>. Calling this method
  946. /// is equivalent to calling <c>RemoveEntry()</c> if an entry by the same name
  947. /// already exists, followed calling by <c>AddItem()</c>.
  948. /// </para>
  949. ///
  950. /// <para>
  951. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  952. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  953. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  954. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  955. /// respective values at the time of this call will be applied to the
  956. /// <c>ZipEntry</c> added.
  957. /// </para>
  958. /// </remarks>
  959. ///
  960. /// <seealso cref="Ionic.Zip.ZipFile.AddItem(string)"/>
  961. /// <seealso cref="Ionic.Zip.ZipFile.UpdateFile(string)"/>
  962. /// <seealso cref="Ionic.Zip.ZipFile.UpdateDirectory(string)"/>
  963. ///
  964. /// <param name="itemName">
  965. /// the path to the file or directory to be added or updated.
  966. /// </param>
  967. public void UpdateItem(string itemName)
  968. {
  969. UpdateItem(itemName, null);
  970. }
  971. /// <summary>
  972. /// Add or update a file or directory.
  973. /// </summary>
  974. ///
  975. /// <remarks>
  976. /// <para>
  977. /// This method is useful when the application is not sure or does not care if
  978. /// the item to be added is a file or directory, and does not know or does not
  979. /// care if the item already exists in the <c>ZipFile</c>. Calling this method
  980. /// is equivalent to calling <c>RemoveEntry()</c>, if an entry by that name
  981. /// exists, and then calling <c>AddItem()</c>.
  982. /// </para>
  983. ///
  984. /// <para>
  985. /// This version of the method allows the caller to explicitly specify the
  986. /// directory path to be used for the item being added to the archive. The
  987. /// entry or entries that are added or updated will use the specified
  988. /// <c>DirectoryPathInArchive</c>. Extracting the entry from the archive will
  989. /// result in a file stored in that directory path.
  990. /// </para>
  991. ///
  992. /// <para>
  993. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  994. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  995. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  996. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  997. /// respective values at the time of this call will be applied to the
  998. /// <c>ZipEntry</c> added.
  999. /// </para>
  1000. /// </remarks>
  1001. ///
  1002. /// <seealso cref="Ionic.Zip.ZipFile.AddItem(string, string)"/>
  1003. /// <seealso cref="Ionic.Zip.ZipFile.UpdateFile(string, string)"/>
  1004. /// <seealso cref="Ionic.Zip.ZipFile.UpdateDirectory(string, string)"/>
  1005. ///
  1006. /// <param name="itemName">
  1007. /// The path for the File or Directory to be added or updated.
  1008. /// </param>
  1009. /// <param name="directoryPathInArchive">
  1010. /// Specifies a directory path to use to override any path in the
  1011. /// <c>itemName</c>. This path may, or may not, correspond to a real
  1012. /// directory in the current filesystem. If the files within the zip are
  1013. /// later extracted, this is the path used for the extracted file. Passing
  1014. /// <c>null</c> (<c>Nothing</c> in VB) will use the path on the
  1015. /// <c>itemName</c>, if any. Passing the empty string ("") will insert the
  1016. /// item at the root path within the archive.
  1017. /// </param>
  1018. public void UpdateItem(string itemName, string directoryPathInArchive)
  1019. {
  1020. if (File.Exists(itemName))
  1021. UpdateFile(itemName, directoryPathInArchive);
  1022. else if (Directory.Exists(itemName))
  1023. UpdateDirectory(itemName, directoryPathInArchive);
  1024. else
  1025. throw new FileNotFoundException(String.Format("That file or directory ({0}) does not exist!", itemName));
  1026. }
  1027. /// <summary>
  1028. /// Adds a named entry into the zip archive, taking content for the entry
  1029. /// from a string.
  1030. /// </summary>
  1031. ///
  1032. /// <remarks>
  1033. /// Calling this method creates an entry using the given fileName and
  1034. /// directory path within the archive. There is no need for a file by the
  1035. /// given name to exist in the filesystem; the name is used within the zip
  1036. /// archive only. The content for the entry is encoded using the default text
  1037. /// encoding for the machine, or on Silverlight, using UTF-8.
  1038. /// </remarks>
  1039. ///
  1040. /// <param name="content">
  1041. /// The content of the file, should it be extracted from the zip.
  1042. /// </param>
  1043. ///
  1044. /// <param name="entryName">
  1045. /// The name, including any path, to use for the entry within the archive.
  1046. /// </param>
  1047. ///
  1048. /// <returns>The <c>ZipEntry</c> added.</returns>
  1049. ///
  1050. /// <example>
  1051. ///
  1052. /// This example shows how to add an entry to the zipfile, using a string as
  1053. /// content for that entry.
  1054. ///
  1055. /// <code lang="C#">
  1056. /// string Content = "This string will be the content of the Readme.txt file in the zip archive.";
  1057. /// using (ZipFile zip1 = new ZipFile())
  1058. /// {
  1059. /// zip1.AddFile("MyDocuments\\Resume.doc", "files");
  1060. /// zip1.AddEntry("Readme.txt", Content);
  1061. /// zip1.Comment = "This zip file was created at " + System.DateTime.Now.ToString("G");
  1062. /// zip1.Save("Content.zip");
  1063. /// }
  1064. ///
  1065. /// </code>
  1066. /// <code lang="VB">
  1067. /// Public Sub Run()
  1068. /// Dim Content As String = "This string will be the content of the Readme.txt file in the zip archive."
  1069. /// Using zip1 As ZipFile = New ZipFile
  1070. /// zip1.AddEntry("Readme.txt", Content)
  1071. /// zip1.AddFile("MyDocuments\Resume.doc", "files")
  1072. /// zip1.Comment = ("This zip file was created at " &amp; DateTime.Now.ToString("G"))
  1073. /// zip1.Save("Content.zip")
  1074. /// End Using
  1075. /// End Sub
  1076. /// </code>
  1077. /// </example>
  1078. public ZipEntry AddEntry(string entryName, string content)
  1079. {
  1080. #if SILVERLIGHT
  1081. return AddEntry(entryName, content, System.Text.Encoding.UTF8);
  1082. #else
  1083. return AddEntry(entryName, content, System.Text.Encoding.Default);
  1084. #endif
  1085. }
  1086. /// <summary>
  1087. /// Adds a named entry into the zip archive, taking content for the entry
  1088. /// from a string, and using the specified text encoding.
  1089. /// </summary>
  1090. ///
  1091. /// <remarks>
  1092. ///
  1093. /// <para>
  1094. /// Calling this method creates an entry using the given fileName and
  1095. /// directory path within the archive. There is no need for a file by the
  1096. /// given name to exist in the filesystem; the name is used within the zip
  1097. /// archive only.
  1098. /// </para>
  1099. ///
  1100. /// <para>
  1101. /// The content for the entry, a string value, is encoded using the given
  1102. /// text encoding. A BOM (byte-order-mark) is emitted into the file, if the
  1103. /// Encoding parameter is set for that.
  1104. /// </para>
  1105. ///
  1106. /// <para>
  1107. /// Most Encoding classes support a constructor that accepts a boolean,
  1108. /// indicating whether to emit a BOM or not. For example see <see
  1109. /// cref="System.Text.UTF8Encoding(bool)"/>.
  1110. /// </para>
  1111. ///
  1112. /// </remarks>
  1113. ///
  1114. /// <param name="entryName">
  1115. /// The name, including any path, to use within the archive for the entry.
  1116. /// </param>
  1117. ///
  1118. /// <param name="content">
  1119. /// The content of the file, should it be extracted from the zip.
  1120. /// </param>
  1121. ///
  1122. /// <param name="encoding">
  1123. /// The text encoding to use when encoding the string. Be aware: This is
  1124. /// distinct from the text encoding used to encode the fileName, as specified
  1125. /// in <see cref="ProvisionalAlternateEncoding" />.
  1126. /// </param>
  1127. ///
  1128. /// <returns>The <c>ZipEntry</c> added.</returns>
  1129. ///
  1130. public ZipEntry AddEntry(string entryName, string content, System.Text.Encoding encoding)
  1131. {
  1132. // cannot employ a using clause here. We need the stream to
  1133. // persist after exit from this method.
  1134. var ms = new MemoryStream();
  1135. // cannot use a using clause here; StreamWriter takes
  1136. // ownership of the stream and Disposes it before we are ready.
  1137. var sw = new StreamWriter(ms, encoding);
  1138. sw.Write(content);
  1139. sw.Flush();
  1140. // reset to allow reading later
  1141. ms.Seek(0, SeekOrigin.Begin);
  1142. return AddEntry(entryName, ms);
  1143. // must not dispose the MemoryStream - it will be used later.
  1144. }
  1145. /// <summary>
  1146. /// Create an entry in the <c>ZipFile</c> using the given <c>Stream</c>
  1147. /// as input. The entry will have the given filename.
  1148. /// </summary>
  1149. ///
  1150. /// <remarks>
  1151. ///
  1152. /// <para>
  1153. /// The application should provide an open, readable stream; in this case it
  1154. /// will be read during the call to <see cref="ZipFile.Save()"/> or one of
  1155. /// its overloads.
  1156. /// </para>
  1157. ///
  1158. /// <para>
  1159. /// The passed stream will be read from its current position. If
  1160. /// necessary, callers should set the position in the stream before
  1161. /// calling AddEntry(). This might be appropriate when using this method
  1162. /// with a MemoryStream, for example.
  1163. /// </para>
  1164. ///
  1165. /// <para>
  1166. /// In cases where a large number of streams will be added to the
  1167. /// <c>ZipFile</c>, the application may wish to avoid maintaining all of the
  1168. /// streams open simultaneously. To handle this situation, the application
  1169. /// should use the <see cref="AddEntry(string, OpenDelegate, CloseDelegate)"/>
  1170. /// overload.
  1171. /// </para>
  1172. ///
  1173. /// <para>
  1174. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  1175. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  1176. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  1177. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  1178. /// respective values at the time of this call will be applied to the
  1179. /// <c>ZipEntry</c> added.
  1180. /// </para>
  1181. ///
  1182. /// </remarks>
  1183. ///
  1184. /// <example>
  1185. /// <para>
  1186. /// This example adds a single entry to a <c>ZipFile</c> via a <c>Stream</c>.
  1187. /// </para>
  1188. ///
  1189. /// <code lang="C#">
  1190. /// String zipToCreate = "Content.zip";
  1191. /// String fileNameInArchive = "Content-From-Stream.bin";
  1192. /// using (System.IO.Stream streamToRead = MyStreamOpener())
  1193. /// {
  1194. /// using (ZipFile zip = new ZipFile())
  1195. /// {
  1196. /// ZipEntry entry= zip.AddEntry(fileNameInArchive, streamToRead);
  1197. /// zip.AddFile("Readme.txt");
  1198. /// zip.Save(zipToCreate); // the stream is read implicitly here
  1199. /// }
  1200. /// }
  1201. /// </code>
  1202. ///
  1203. /// <code lang="VB">
  1204. /// Dim zipToCreate As String = "Content.zip"
  1205. /// Dim fileNameInArchive As String = "Content-From-Stream.bin"
  1206. /// Using streamToRead as System.IO.Stream = MyStreamOpener()
  1207. /// Using zip As ZipFile = New ZipFile()
  1208. /// Dim entry as ZipEntry = zip.AddEntry(fileNameInArchive, streamToRead)
  1209. /// zip.AddFile("Readme.txt")
  1210. /// zip.Save(zipToCreate) '' the stream is read implicitly, here
  1211. /// End Using
  1212. /// End Using
  1213. /// </code>
  1214. /// </example>
  1215. ///
  1216. /// <seealso cref="Ionic.Zip.ZipFile.UpdateEntry(string, System.IO.Stream)"/>
  1217. ///
  1218. /// <param name="entryName">
  1219. /// The name, including any path, which is shown in the zip file for the added
  1220. /// entry.
  1221. /// </param>
  1222. /// <param name="stream">
  1223. /// The input stream from which to grab content for the file
  1224. /// </param>
  1225. /// <returns>The <c>ZipEntry</c> added.</returns>
  1226. public ZipEntry AddEntry(string entryName, Stream stream)
  1227. {
  1228. ZipEntry ze = ZipEntry.CreateForStream(entryName, stream);
  1229. ze.SetEntryTimes(DateTime.Now,DateTime.Now,DateTime.Now);
  1230. if (Verbose) StatusMessageTextWriter.WriteLine("adding {0}...", entryName);
  1231. return _InternalAddEntry(ze);
  1232. }
  1233. /// <summary>
  1234. /// Add a ZipEntry for which content is written directly by the application.
  1235. /// </summary>
  1236. ///
  1237. /// <remarks>
  1238. /// <para>
  1239. /// When the application needs to write the zip entry data, use this
  1240. /// method to add the ZipEntry. For example, in the case that the
  1241. /// application wishes to write the XML representation of a DataSet into
  1242. /// a ZipEntry, the application can use this method to do so.
  1243. /// </para>
  1244. ///
  1245. /// <para>
  1246. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  1247. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  1248. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  1249. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  1250. /// respective values at the time of this call will be applied to the
  1251. /// <c>ZipEntry</c> added.
  1252. /// </para>
  1253. ///
  1254. /// <para>
  1255. /// About progress events: When using the WriteDelegate, DotNetZip does
  1256. /// not issue any SaveProgress events with <c>EventType</c> = <see
  1257. /// cref="ZipProgressEventType.Saving_EntryBytesRead">
  1258. /// Saving_EntryBytesRead</see>. (This is because it is the
  1259. /// application's code that runs in WriteDelegate - there's no way for
  1260. /// DotNetZip to know when to issue a EntryBytesRead event.)
  1261. /// Applications that want to update a progress bar or similar status
  1262. /// indicator should do so from within the WriteDelegate
  1263. /// itself. DotNetZip will issue the other SaveProgress events,
  1264. /// including <see cref="ZipProgressEventType.Saving_Started">
  1265. /// Saving_Started</see>,
  1266. /// <see cref="ZipProgressEventType.Saving_BeforeWriteEntry">
  1267. /// Saving_BeforeWriteEntry</see>, and <see
  1268. /// cref="ZipProgressEventType.Saving_AfterWriteEntry">
  1269. /// Saving_AfterWriteEntry</see>.
  1270. /// </para>
  1271. ///
  1272. /// <para>
  1273. /// Note: When you use PKZip encryption, it's normally necessary to
  1274. /// compute the CRC of the content to be encrypted, before compressing or
  1275. /// encrypting it. Therefore, when using PKZip encryption with a
  1276. /// WriteDelegate, the WriteDelegate CAN BE called twice: once to compute
  1277. /// the CRC, and the second time to potentially compress and
  1278. /// encrypt. Surprising, but true. This is because PKWARE specified that
  1279. /// the encryption initialization data depends on the CRC.
  1280. /// If this happens, for each call of the delegate, your
  1281. /// application must stream the same entry data in its entirety. If your
  1282. /// application writes different data during the second call, it will
  1283. /// result in a corrupt zip file.
  1284. /// </para>
  1285. ///
  1286. /// <para>
  1287. /// The double-read behavior happens with all types of entries, not only
  1288. /// those that use WriteDelegate. It happens if you add an entry from a
  1289. /// filesystem file, or using a string, or a stream, or an opener/closer
  1290. /// pair. But in those cases, DotNetZip takes care of reading twice; in
  1291. /// the case of the WriteDelegate, the application code gets invoked
  1292. /// twice. Be aware.
  1293. /// </para>
  1294. ///
  1295. /// <para>
  1296. /// As you can imagine, this can cause performance problems for large
  1297. /// streams, and it can lead to correctness problems when you use a
  1298. /// <c>WriteDelegate</c>. This is a pretty big pitfall. There are two
  1299. /// ways to avoid it. First, and most preferred: don't use PKZIP
  1300. /// encryption. If you use the WinZip AES encryption, this problem
  1301. /// doesn't occur, because the encryption protocol doesn't require the CRC
  1302. /// up front. Second: if you do choose to use PKZIP encryption, write out
  1303. /// to a non-seekable stream (like standard output, or the
  1304. /// Response.OutputStream in an ASP.NET application). In this case,
  1305. /// DotNetZip will use an alternative encryption protocol that does not
  1306. /// rely on the CRC of the content. This also implies setting bit 3 in
  1307. /// the zip entry, which still presents problems for some zip tools.
  1308. /// </para>
  1309. ///
  1310. /// <para>
  1311. /// In the future I may modify DotNetZip to *always* use bit 3 when PKZIP
  1312. /// encryption is in use. This seems like a win overall, but there will
  1313. /// be some work involved. If you feel strongly about it, visit the
  1314. /// DotNetZip forums and vote up <see
  1315. /// href="http://dotnetzip.codeplex.com/workitem/13686">the Workitem
  1316. /// tracking this issue</see>.
  1317. /// </para>
  1318. ///
  1319. /// </remarks>
  1320. ///
  1321. /// <param name="entryName">the name of the entry to add</param>
  1322. /// <param name="writer">the delegate which will write the entry content</param>
  1323. /// <returns>the ZipEntry added</returns>
  1324. ///
  1325. /// <example>
  1326. ///
  1327. /// This example shows an application filling a DataSet, then saving the
  1328. /// contents of that DataSet as XML, into a ZipEntry in a ZipFile, using an
  1329. /// anonymous delegate in C#. The DataSet XML is never saved to a disk file.
  1330. ///
  1331. /// <code lang="C#">
  1332. /// var c1= new System.Data.SqlClient.SqlConnection(connstring1);
  1333. /// var da = new System.Data.SqlClient.SqlDataAdapter()
  1334. /// {
  1335. /// SelectCommand= new System.Data.SqlClient.SqlCommand(strSelect, c1)
  1336. /// };
  1337. ///
  1338. /// DataSet ds1 = new DataSet();
  1339. /// da.Fill(ds1, "Invoices");
  1340. ///
  1341. /// using(Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
  1342. /// {
  1343. /// zip.AddEntry(zipEntryName, (name,stream) => ds1.WriteXml(stream) );
  1344. /// zip.Save(zipFileName);
  1345. /// }
  1346. /// </code>
  1347. /// </example>
  1348. ///
  1349. /// <example>
  1350. ///
  1351. /// This example uses an anonymous method in C# as the WriteDelegate to provide
  1352. /// the data for the ZipEntry. The example is a bit contrived - the
  1353. /// <c>AddFile()</c> method is a simpler way to insert the contents of a file
  1354. /// into an entry in a zip file. On the other hand, if there is some sort of
  1355. /// processing or transformation of the file contents required before writing,
  1356. /// the application could use the <c>WriteDelegate</c> to do it, in this way.
  1357. ///
  1358. /// <code lang="C#">
  1359. /// using (var input = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ))
  1360. /// {
  1361. /// using(Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
  1362. /// {
  1363. /// zip.AddEntry(zipEntryName, (name,output) =>
  1364. /// {
  1365. /// byte[] buffer = new byte[BufferSize];
  1366. /// int n;
  1367. /// while ((n = input.Read(buffer, 0, buffer.Length)) != 0)
  1368. /// {
  1369. /// // could transform the data here...
  1370. /// output.Write(buffer, 0, n);
  1371. /// // could update a progress bar here
  1372. /// }
  1373. /// });
  1374. ///
  1375. /// zip.Save(zipFileName);
  1376. /// }
  1377. /// }
  1378. /// </code>
  1379. /// </example>
  1380. ///
  1381. /// <example>
  1382. ///
  1383. /// This example uses a named delegate in VB to write data for the given
  1384. /// ZipEntry (VB9 does not have anonymous delegates). The example here is a bit
  1385. /// contrived - a simpler way to add the contents of a file to a ZipEntry is to
  1386. /// simply use the appropriate <c>AddFile()</c> method. The key scenario for
  1387. /// which the <c>WriteDelegate</c> makes sense is saving a DataSet, in XML
  1388. /// format, to the zip file. The DataSet can write XML to a stream, and the
  1389. /// WriteDelegate is the perfect place to write into the zip file. There may be
  1390. /// other data structures that can write to a stream, but cannot be read as a
  1391. /// stream. The <c>WriteDelegate</c> would be appropriate for those cases as
  1392. /// well.
  1393. ///
  1394. /// <code lang="VB">
  1395. /// Private Sub WriteEntry (ByVal name As String, ByVal output As Stream)
  1396. /// Using input As FileStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
  1397. /// Dim n As Integer = -1
  1398. /// Dim buffer As Byte() = New Byte(BufferSize){}
  1399. /// Do While n &lt;&gt; 0
  1400. /// n = input.Read(buffer, 0, buffer.Length)
  1401. /// output.Write(buffer, 0, n)
  1402. /// Loop
  1403. /// End Using
  1404. /// End Sub
  1405. ///
  1406. /// Public Sub Run()
  1407. /// Using zip = New ZipFile
  1408. /// zip.AddEntry(zipEntryName, New WriteDelegate(AddressOf WriteEntry))
  1409. /// zip.Save(zipFileName)
  1410. /// End Using
  1411. /// End Sub
  1412. /// </code>
  1413. /// </example>
  1414. public ZipEntry AddEntry(string entryName, WriteDelegate writer)
  1415. {
  1416. ZipEntry ze = ZipEntry.CreateForWriter(entryName, writer);
  1417. if (Verbose) StatusMessageTextWriter.WriteLine("adding {0}...", entryName);
  1418. return _InternalAddEntry(ze);
  1419. }
  1420. /// <summary>
  1421. /// Add an entry, for which the application will provide a stream
  1422. /// containing the entry data, on a just-in-time basis.
  1423. /// </summary>
  1424. ///
  1425. /// <remarks>
  1426. /// <para>
  1427. /// In cases where the application wishes to open the stream that
  1428. /// holds the content for the ZipEntry, on a just-in-time basis, the
  1429. /// application can use this method. The application provides an
  1430. /// opener delegate that will be called by the DotNetZip library to
  1431. /// obtain a readable stream that can be read to get the bytes for
  1432. /// the given entry. Typically, this delegate opens a stream.
  1433. /// Optionally, the application can provide a closer delegate as
  1434. /// well, which will be called by DotNetZip when all bytes have been
  1435. /// read from the entry.
  1436. /// </para>
  1437. ///
  1438. /// <para>
  1439. /// These delegates are called from within the scope of the call to
  1440. /// ZipFile.Save().
  1441. /// </para>
  1442. ///
  1443. /// <para>
  1444. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  1445. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  1446. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  1447. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  1448. /// respective values at the time of this call will be applied to the
  1449. /// <c>ZipEntry</c> added.
  1450. /// </para>
  1451. ///
  1452. /// </remarks>
  1453. ///
  1454. /// <example>
  1455. ///
  1456. /// This example uses anonymous methods in C# to open and close the
  1457. /// source stream for the content for a zip entry.
  1458. ///
  1459. /// <code lang="C#">
  1460. /// using(Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
  1461. /// {
  1462. /// zip.AddEntry(zipEntryName,
  1463. /// (name) => File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ),
  1464. /// (name, stream) => stream.Close()
  1465. /// );
  1466. ///
  1467. /// zip.Save(zipFileName);
  1468. /// }
  1469. /// </code>
  1470. ///
  1471. /// </example>
  1472. ///
  1473. /// <example>
  1474. ///
  1475. /// This example uses delegates in VB.NET to open and close the
  1476. /// the source stream for the content for a zip entry. VB 9.0 lacks
  1477. /// support for "Sub" lambda expressions, and so the CloseDelegate must
  1478. /// be an actual, named Sub.
  1479. ///
  1480. /// <code lang="VB">
  1481. ///
  1482. /// Function MyStreamOpener(ByVal entryName As String) As Stream
  1483. /// '' This simply opens a file. You probably want to do somethinig
  1484. /// '' more involved here: open a stream to read from a database,
  1485. /// '' open a stream on an HTTP connection, and so on.
  1486. /// Return File.OpenRead(entryName)
  1487. /// End Function
  1488. ///
  1489. /// Sub MyStreamCloser(entryName As String, stream As Stream)
  1490. /// stream.Close()
  1491. /// End Sub
  1492. ///
  1493. /// Public Sub Run()
  1494. /// Dim dirToZip As String = "fodder"
  1495. /// Dim zipFileToCreate As String = "Archive.zip"
  1496. /// Dim opener As OpenDelegate = AddressOf MyStreamOpener
  1497. /// Dim closer As CloseDelegate = AddressOf MyStreamCloser
  1498. /// Dim numFilestoAdd As Int32 = 4
  1499. /// Using zip As ZipFile = New ZipFile
  1500. /// Dim i As Integer
  1501. /// For i = 0 To numFilesToAdd - 1
  1502. /// zip.AddEntry(String.Format("content-{0:000}.txt"), opener, closer)
  1503. /// Next i
  1504. /// zip.Save(zipFileToCreate)
  1505. /// End Using
  1506. /// End Sub
  1507. ///
  1508. /// </code>
  1509. /// </example>
  1510. ///
  1511. /// <param name="entryName">the name of the entry to add</param>
  1512. /// <param name="opener">
  1513. /// the delegate that will be invoked by ZipFile.Save() to get the
  1514. /// readable stream for the given entry. ZipFile.Save() will call
  1515. /// read on this stream to obtain the data for the entry. This data
  1516. /// will then be compressed and written to the newly created zip
  1517. /// file.
  1518. /// </param>
  1519. /// <param name="closer">
  1520. /// the delegate that will be invoked to close the stream. This may
  1521. /// be null (Nothing in VB), in which case no call is makde to close
  1522. /// the stream.
  1523. /// </param>
  1524. /// <returns>the ZipEntry added</returns>
  1525. ///
  1526. public ZipEntry AddEntry(string entryName, OpenDelegate opener, CloseDelegate closer)
  1527. {
  1528. ZipEntry ze = ZipEntry.CreateForJitStreamProvider(entryName, opener, closer);
  1529. ze.SetEntryTimes(DateTime.Now,DateTime.Now,DateTime.Now);
  1530. if (Verbose) StatusMessageTextWriter.WriteLine("adding {0}...", entryName);
  1531. return _InternalAddEntry(ze);
  1532. }
  1533. private ZipEntry _InternalAddEntry(ZipEntry ze)
  1534. {
  1535. // stamp all the props onto the entry
  1536. ze._container = new ZipContainer(this);
  1537. ze.CompressionMethod = this.CompressionMethod;
  1538. ze.CompressionLevel = this.CompressionLevel;
  1539. ze.ExtractExistingFile = this.ExtractExistingFile;
  1540. ze.ZipErrorAction = this.ZipErrorAction;
  1541. ze.SetCompression = this.SetCompression;
  1542. ze.AlternateEncoding = this.AlternateEncoding;
  1543. ze.AlternateEncodingUsage = this.AlternateEncodingUsage;
  1544. ze.Password = this._Password;
  1545. ze.Encryption = this.Encryption;
  1546. ze.EmitTimesInWindowsFormatWhenSaving = this._emitNtfsTimes;
  1547. ze.EmitTimesInUnixFormatWhenSaving = this._emitUnixTimes;
  1548. //string key = DictionaryKeyForEntry(ze);
  1549. InternalAddEntry(ze.FileName,ze);
  1550. AfterAddEntry(ze);
  1551. return ze;
  1552. }
  1553. /// <summary>
  1554. /// Updates the given entry in the <c>ZipFile</c>, using the given
  1555. /// string as content for the <c>ZipEntry</c>.
  1556. /// </summary>
  1557. ///
  1558. /// <remarks>
  1559. ///
  1560. /// <para>
  1561. /// Calling this method is equivalent to removing the <c>ZipEntry</c> for
  1562. /// the given file name and directory path, if it exists, and then calling
  1563. /// <see cref="AddEntry(String,String)" />. See the documentation for
  1564. /// that method for further explanation. The string content is encoded
  1565. /// using the default encoding for the machine, or on Silverlight, using
  1566. /// UTF-8. This encoding is distinct from the encoding used for the
  1567. /// filename itself. See <see cref="AlternateEncoding"/>.
  1568. /// </para>
  1569. ///
  1570. /// </remarks>
  1571. ///
  1572. /// <param name="entryName">
  1573. /// The name, including any path, to use within the archive for the entry.
  1574. /// </param>
  1575. ///
  1576. /// <param name="content">
  1577. /// The content of the file, should it be extracted from the zip.
  1578. /// </param>
  1579. ///
  1580. /// <returns>The <c>ZipEntry</c> added.</returns>
  1581. ///
  1582. public ZipEntry UpdateEntry(string entryName, string content)
  1583. {
  1584. #if SILVERLIGHT
  1585. return UpdateEntry(entryName, content, System.Text.Encoding.UTF8);
  1586. #else
  1587. return UpdateEntry(entryName, content, System.Text.Encoding.Default);
  1588. #endif
  1589. }
  1590. /// <summary>
  1591. /// Updates the given entry in the <c>ZipFile</c>, using the given string as
  1592. /// content for the <c>ZipEntry</c>.
  1593. /// </summary>
  1594. ///
  1595. /// <remarks>
  1596. /// Calling this method is equivalent to removing the <c>ZipEntry</c> for the
  1597. /// given file name and directory path, if it exists, and then calling <see
  1598. /// cref="AddEntry(String,String, System.Text.Encoding)" />. See the
  1599. /// documentation for that method for further explanation.
  1600. /// </remarks>
  1601. ///
  1602. /// <param name="entryName">
  1603. /// The name, including any path, to use within the archive for the entry.
  1604. /// </param>
  1605. ///
  1606. /// <param name="content">
  1607. /// The content of the file, should it be extracted from the zip.
  1608. /// </param>
  1609. ///
  1610. /// <param name="encoding">
  1611. /// The text encoding to use when encoding the string. Be aware: This is
  1612. /// distinct from the text encoding used to encode the filename. See <see
  1613. /// cref="AlternateEncoding" />.
  1614. /// </param>
  1615. ///
  1616. /// <returns>The <c>ZipEntry</c> added.</returns>
  1617. ///
  1618. public ZipEntry UpdateEntry(string entryName, string content, System.Text.Encoding encoding)
  1619. {
  1620. RemoveEntryForUpdate(entryName);
  1621. return AddEntry(entryName, content, encoding);
  1622. }
  1623. /// <summary>
  1624. /// Updates the given entry in the <c>ZipFile</c>, using the given delegate
  1625. /// as the source for content for the <c>ZipEntry</c>.
  1626. /// </summary>
  1627. ///
  1628. /// <remarks>
  1629. /// Calling this method is equivalent to removing the <c>ZipEntry</c> for the
  1630. /// given file name and directory path, if it exists, and then calling <see
  1631. /// cref="AddEntry(String,WriteDelegate)" />. See the
  1632. /// documentation for that method for further explanation.
  1633. /// </remarks>
  1634. ///
  1635. /// <param name="entryName">
  1636. /// The name, including any path, to use within the archive for the entry.
  1637. /// </param>
  1638. ///
  1639. /// <param name="writer">the delegate which will write the entry content.</param>
  1640. ///
  1641. /// <returns>The <c>ZipEntry</c> added.</returns>
  1642. ///
  1643. public ZipEntry UpdateEntry(string entryName, WriteDelegate writer)
  1644. {
  1645. RemoveEntryForUpdate(entryName);
  1646. return AddEntry(entryName, writer);
  1647. }
  1648. /// <summary>
  1649. /// Updates the given entry in the <c>ZipFile</c>, using the given delegates
  1650. /// to open and close the stream that provides the content for the <c>ZipEntry</c>.
  1651. /// </summary>
  1652. ///
  1653. /// <remarks>
  1654. /// Calling this method is equivalent to removing the <c>ZipEntry</c> for the
  1655. /// given file name and directory path, if it exists, and then calling <see
  1656. /// cref="AddEntry(String,OpenDelegate, CloseDelegate)" />. See the
  1657. /// documentation for that method for further explanation.
  1658. /// </remarks>
  1659. ///
  1660. /// <param name="entryName">
  1661. /// The name, including any path, to use within the archive for the entry.
  1662. /// </param>
  1663. ///
  1664. /// <param name="opener">
  1665. /// the delegate that will be invoked to open the stream
  1666. /// </param>
  1667. /// <param name="closer">
  1668. /// the delegate that will be invoked to close the stream
  1669. /// </param>
  1670. ///
  1671. /// <returns>The <c>ZipEntry</c> added or updated.</returns>
  1672. ///
  1673. public ZipEntry UpdateEntry(string entryName, OpenDelegate opener, CloseDelegate closer)
  1674. {
  1675. RemoveEntryForUpdate(entryName);
  1676. return AddEntry(entryName, opener, closer);
  1677. }
  1678. /// <summary>
  1679. /// Updates the given entry in the <c>ZipFile</c>, using the given stream as
  1680. /// input, and the given filename and given directory Path.
  1681. /// </summary>
  1682. ///
  1683. /// <remarks>
  1684. /// <para>
  1685. /// Calling the method is equivalent to calling <c>RemoveEntry()</c> if an
  1686. /// entry by the same name already exists, and then calling <c>AddEntry()</c>
  1687. /// with the given <c>fileName</c> and stream.
  1688. /// </para>
  1689. ///
  1690. /// <para>
  1691. /// The stream must be open and readable during the call to
  1692. /// <c>ZipFile.Save</c>. You can dispense the stream on a just-in-time basis
  1693. /// using the <see cref="ZipEntry.InputStream"/> property. Check the
  1694. /// documentation of that property for more information.
  1695. /// </para>
  1696. ///
  1697. /// <para>
  1698. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  1699. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  1700. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  1701. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  1702. /// respective values at the time of this call will be applied to the
  1703. /// <c>ZipEntry</c> added.
  1704. /// </para>
  1705. ///
  1706. /// </remarks>
  1707. ///
  1708. /// <seealso cref="Ionic.Zip.ZipFile.AddEntry(string, System.IO.Stream)"/>
  1709. /// <seealso cref="Ionic.Zip.ZipEntry.InputStream"/>
  1710. ///
  1711. /// <param name="entryName">
  1712. /// The name, including any path, to use within the archive for the entry.
  1713. /// </param>
  1714. ///
  1715. /// <param name="stream">The input stream from which to read file data.</param>
  1716. /// <returns>The <c>ZipEntry</c> added.</returns>
  1717. public ZipEntry UpdateEntry(string entryName, Stream stream)
  1718. {
  1719. RemoveEntryForUpdate(entryName);
  1720. return AddEntry(entryName, stream);
  1721. }
  1722. private void RemoveEntryForUpdate(string entryName)
  1723. {
  1724. if (String.IsNullOrEmpty(entryName))
  1725. throw new ArgumentNullException("entryName");
  1726. string directoryPathInArchive = null;
  1727. if (entryName.IndexOf('\\') != -1)
  1728. {
  1729. directoryPathInArchive = Path.GetDirectoryName(entryName);
  1730. entryName = Path.GetFileName(entryName);
  1731. }
  1732. var key = ZipEntry.NameInArchive(entryName, directoryPathInArchive);
  1733. if (this[key] != null)
  1734. this.RemoveEntry(key);
  1735. }
  1736. /// <summary>
  1737. /// Add an entry into the zip archive using the given filename and
  1738. /// directory path within the archive, and the given content for the
  1739. /// file. No file is created in the filesystem.
  1740. /// </summary>
  1741. ///
  1742. /// <param name="byteContent">The data to use for the entry.</param>
  1743. ///
  1744. /// <param name="entryName">
  1745. /// The name, including any path, to use within the archive for the entry.
  1746. /// </param>
  1747. ///
  1748. /// <returns>The <c>ZipEntry</c> added.</returns>
  1749. public ZipEntry AddEntry(string entryName, byte[] byteContent)
  1750. {
  1751. if (byteContent == null) throw new ArgumentException("bad argument", "byteContent");
  1752. var ms = new MemoryStream(byteContent);
  1753. return AddEntry(entryName, ms);
  1754. }
  1755. /// <summary>
  1756. /// Updates the given entry in the <c>ZipFile</c>, using the given byte
  1757. /// array as content for the entry.
  1758. /// </summary>
  1759. ///
  1760. /// <remarks>
  1761. /// Calling this method is equivalent to removing the <c>ZipEntry</c>
  1762. /// for the given filename and directory path, if it exists, and then
  1763. /// calling <see cref="AddEntry(String,byte[])" />. See the
  1764. /// documentation for that method for further explanation.
  1765. /// </remarks>
  1766. ///
  1767. /// <param name="entryName">
  1768. /// The name, including any path, to use within the archive for the entry.
  1769. /// </param>
  1770. ///
  1771. /// <param name="byteContent">The content to use for the <c>ZipEntry</c>.</param>
  1772. ///
  1773. /// <returns>The <c>ZipEntry</c> added.</returns>
  1774. ///
  1775. public ZipEntry UpdateEntry(string entryName, byte[] byteContent)
  1776. {
  1777. RemoveEntryForUpdate(entryName);
  1778. return AddEntry(entryName, byteContent);
  1779. }
  1780. // private string DictionaryKeyForEntry(ZipEntry ze1)
  1781. // {
  1782. // var filename = SharedUtilities.NormalizePathForUseInZipFile(ze1.FileName);
  1783. // return filename;
  1784. // }
  1785. /// <summary>
  1786. /// Adds the contents of a filesystem directory to a Zip file archive.
  1787. /// </summary>
  1788. ///
  1789. /// <remarks>
  1790. ///
  1791. /// <para>
  1792. /// The name of the directory may be a relative path or a fully-qualified
  1793. /// path. Any files within the named directory are added to the archive. Any
  1794. /// subdirectories within the named directory are also added to the archive,
  1795. /// recursively.
  1796. /// </para>
  1797. ///
  1798. /// <para>
  1799. /// Top-level entries in the named directory will appear as top-level entries
  1800. /// in the zip archive. Entries in subdirectories in the named directory will
  1801. /// result in entries in subdirectories in the zip archive.
  1802. /// </para>
  1803. ///
  1804. /// <para>
  1805. /// If you want the entries to appear in a containing directory in the zip
  1806. /// archive itself, then you should call the AddDirectory() overload that
  1807. /// allows you to explicitly specify a directory path for use in the archive.
  1808. /// </para>
  1809. ///
  1810. /// <para>
  1811. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  1812. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  1813. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  1814. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  1815. /// respective values at the time of this call will be applied to each
  1816. /// ZipEntry added.
  1817. /// </para>
  1818. ///
  1819. /// </remarks>
  1820. ///
  1821. /// <seealso cref="Ionic.Zip.ZipFile.AddItem(string)"/>
  1822. /// <seealso cref="Ionic.Zip.ZipFile.AddFile(string)"/>
  1823. /// <seealso cref="Ionic.Zip.ZipFile.UpdateDirectory(string)"/>
  1824. /// <seealso cref="Ionic.Zip.ZipFile.AddDirectory(string, string)"/>
  1825. ///
  1826. /// <overloads>This method has 2 overloads.</overloads>
  1827. ///
  1828. /// <param name="directoryName">The name of the directory to add.</param>
  1829. /// <returns>The <c>ZipEntry</c> added.</returns>
  1830. public ZipEntry AddDirectory(string directoryName)
  1831. {
  1832. return AddDirectory(directoryName, null);
  1833. }
  1834. /// <summary>
  1835. /// Adds the contents of a filesystem directory to a Zip file archive,
  1836. /// overriding the path to be used for entries in the archive.
  1837. /// </summary>
  1838. ///
  1839. /// <remarks>
  1840. /// <para>
  1841. /// The name of the directory may be a relative path or a fully-qualified
  1842. /// path. The add operation is recursive, so that any files or subdirectories
  1843. /// within the name directory are also added to the archive.
  1844. /// </para>
  1845. ///
  1846. /// <para>
  1847. /// Top-level entries in the named directory will appear as top-level entries
  1848. /// in the zip archive. Entries in subdirectories in the named directory will
  1849. /// result in entries in subdirectories in the zip archive.
  1850. /// </para>
  1851. ///
  1852. /// <para>
  1853. /// For <c>ZipFile</c> properties including <see cref="Encryption"/>, <see
  1854. /// cref="Password"/>, <see cref="SetCompression"/>, <see
  1855. /// cref="ProvisionalAlternateEncoding"/>, <see cref="ExtractExistingFile"/>,
  1856. /// <see cref="ZipErrorAction"/>, and <see cref="CompressionLevel"/>, their
  1857. /// respective values at the time of this call will be applied to each
  1858. /// ZipEntry added.
  1859. /// </para>
  1860. ///
  1861. /// </remarks>
  1862. ///
  1863. /// <example>
  1864. /// <para>
  1865. /// In this code, calling the ZipUp() method with a value of "c:\reports" for
  1866. /// the directory parameter will result in a zip file structure in which all
  1867. /// entries are contained in a toplevel "reports" directory.
  1868. /// </para>
  1869. ///
  1870. /// <code lang="C#">
  1871. /// public void ZipUp(string targetZip, string directory)
  1872. /// {
  1873. /// using (var zip = new ZipFile())
  1874. /// {
  1875. /// zip.AddDirectory(directory, System.IO.Path.GetFileName(directory));
  1876. /// zip.Save(targetZip);
  1877. /// }
  1878. /// }
  1879. /// </code>
  1880. /// </example>
  1881. ///
  1882. /// <seealso cref="Ionic.Zip.ZipFile.AddItem(string, string)"/>
  1883. /// <seealso cref="Ionic.Zip.ZipFile.AddFile(string, string)"/>
  1884. /// <seealso cref="Ionic.Zip.ZipFile.UpdateDirectory(string, string)"/>
  1885. ///
  1886. /// <param name="directoryName">The name of the directory to add.</param>
  1887. ///
  1888. /// <param name="directoryPathInArchive">
  1889. /// Specifies a directory path to use to override any path in the
  1890. /// DirectoryName. This path may, or may not, correspond to a real directory
  1891. /// in the current filesystem. If the zip is later extracted, this is the
  1892. /// path used for the extracted file or directory. Passing <c>null</c>
  1893. /// (<c>Nothing</c> in VB) or the empty string ("") will insert the items at
  1894. /// the root path within the archive.
  1895. /// </param>
  1896. ///
  1897. /// <returns>The <c>ZipEntry</c> added.</returns>
  1898. public ZipEntry AddDirectory(string directoryName, string directoryPathInArchive)
  1899. {
  1900. return AddOrUpdateDirectoryImpl(directoryName, directoryPathInArchive, AddOrUpdateAction.AddOnly);
  1901. }
  1902. /// <summary>
  1903. /// Creates a directory in the zip archive.
  1904. /// </summary>
  1905. ///
  1906. /// <remarks>
  1907. ///
  1908. /// <para>
  1909. /// Use this when you want to create a directory in the archive but there is
  1910. /// no corresponding filesystem representation for that directory.
  1911. /// </para>
  1912. ///
  1913. /// <para>
  1914. /// You will probably not need to do this in your code. One of the only times
  1915. /// you will want to do this is if you want an empty directory in the zip
  1916. /// archive. The reason: if you add a file to a zip archive that is stored
  1917. /// within a multi-level directory, all of the directory tree is implicitly
  1918. /// created in the zip archive.
  1919. /// </para>
  1920. ///
  1921. /// </remarks>
  1922. ///
  1923. /// <param name="directoryNameInArchive">
  1924. /// The name of the directory to create in the archive.
  1925. /// </param>
  1926. /// <returns>The <c>ZipEntry</c> added.</returns>
  1927. public ZipEntry AddDirectoryByName(string directoryNameInArchive)
  1928. {
  1929. // workitem 9073
  1930. ZipEntry dir = ZipEntry.CreateFromNothing(directoryNameInArchive);
  1931. dir._container = new ZipContainer(this);
  1932. dir.MarkAsDirectory();
  1933. dir.AlternateEncoding = this.AlternateEncoding; // workitem 8984
  1934. dir.AlternateEncodingUsage = this.AlternateEncodingUsage;
  1935. dir.SetEntryTimes(DateTime.Now,DateTime.Now,DateTime.Now);
  1936. dir.EmitTimesInWindowsFormatWhenSaving = _emitNtfsTimes;
  1937. dir.EmitTimesInUnixFormatWhenSaving = _emitUnixTimes;
  1938. dir._Source = ZipEntrySource.Stream;
  1939. //string key = DictionaryKeyForEntry(dir);
  1940. InternalAddEntry(dir.FileName,dir);
  1941. AfterAddEntry(dir);
  1942. return dir;
  1943. }
  1944. private ZipEntry AddOrUpdateDirectoryImpl(string directoryName,
  1945. string rootDirectoryPathInArchive,
  1946. AddOrUpdateAction action)
  1947. {
  1948. if (rootDirectoryPathInArchive == null)
  1949. {
  1950. rootDirectoryPathInArchive = "";
  1951. }
  1952. return AddOrUpdateDirectoryImpl(directoryName, rootDirectoryPathInArchive, action, true, 0);
  1953. }
  1954. internal void InternalAddEntry(String name, ZipEntry entry)
  1955. {
  1956. _entries.Add(name, entry);
  1957. if (!_entriesInsensitive.ContainsKey(name))
  1958. _entriesInsensitive.Add(name, entry);
  1959. _zipEntriesAsList = null;
  1960. _contentsChanged = true;
  1961. }
  1962. private ZipEntry AddOrUpdateDirectoryImpl(string directoryName,
  1963. string rootDirectoryPathInArchive,
  1964. AddOrUpdateAction action,
  1965. bool recurse,
  1966. int level)
  1967. {
  1968. if (Verbose)
  1969. StatusMessageTextWriter.WriteLine("{0} {1}...",
  1970. (action == AddOrUpdateAction.AddOnly) ? "adding" : "Adding or updating",
  1971. directoryName);
  1972. if (level == 0)
  1973. {
  1974. _addOperationCanceled = false;
  1975. OnAddStarted();
  1976. }
  1977. // workitem 13371
  1978. if (_addOperationCanceled)
  1979. return null;
  1980. string dirForEntries = rootDirectoryPathInArchive;
  1981. ZipEntry baseDir = null;
  1982. if (level > 0)
  1983. {
  1984. int f = directoryName.Length;
  1985. for (int i = level; i > 0; i--)
  1986. f = directoryName.LastIndexOfAny("/\\".ToCharArray(), f - 1, f - 1);
  1987. dirForEntries = directoryName.Substring(f + 1);
  1988. dirForEntries = Path.Combine(rootDirectoryPathInArchive, dirForEntries);
  1989. }
  1990. // if not top level, or if the root is non-empty, then explicitly add the directory
  1991. if (level > 0 || rootDirectoryPathInArchive != "")
  1992. {
  1993. baseDir = ZipEntry.CreateFromFile(directoryName, dirForEntries);
  1994. baseDir._container = new ZipContainer(this);
  1995. baseDir.AlternateEncoding = this.AlternateEncoding; // workitem 6410
  1996. baseDir.AlternateEncodingUsage = this.AlternateEncodingUsage;
  1997. baseDir.MarkAsDirectory();
  1998. baseDir.EmitTimesInWindowsFormatWhenSaving = _emitNtfsTimes;
  1999. baseDir.EmitTimesInUnixFormatWhenSaving = _emitUnixTimes;
  2000. // add the directory only if it does not exist.
  2001. // It's not an error if it already exists.
  2002. if (!_entries.ContainsKey(baseDir.FileName))
  2003. {
  2004. InternalAddEntry(baseDir.FileName,baseDir);
  2005. AfterAddEntry(baseDir);
  2006. }
  2007. dirForEntries = baseDir.FileName;
  2008. }
  2009. if (!_addOperationCanceled)
  2010. {
  2011. String[] filenames = Directory.GetFiles(directoryName);
  2012. if (recurse)
  2013. {
  2014. // add the files:
  2015. foreach (String filename in filenames)
  2016. {
  2017. if (_addOperationCanceled) break;
  2018. if (action == AddOrUpdateAction.AddOnly)
  2019. AddFile(filename, dirForEntries);
  2020. else
  2021. UpdateFile(filename, dirForEntries);
  2022. }
  2023. if (!_addOperationCanceled)
  2024. {
  2025. // add the subdirectories:
  2026. String[] dirnames = Directory.GetDirectories(directoryName);
  2027. foreach (String dir in dirnames)
  2028. {
  2029. // workitem 8617: Optionally traverse reparse points
  2030. #if SILVERLIGHT
  2031. #elif NETCF
  2032. FileAttributes fileAttrs = (FileAttributes) NetCfFile.GetAttributes(dir);
  2033. #else
  2034. FileAttributes fileAttrs = System.IO.File.GetAttributes(dir);
  2035. #endif
  2036. if (this.AddDirectoryWillTraverseReparsePoints
  2037. #if !SILVERLIGHT
  2038. || ((fileAttrs & FileAttributes.ReparsePoint) == 0)
  2039. #endif
  2040. )
  2041. AddOrUpdateDirectoryImpl(dir, rootDirectoryPathInArchive, action, recurse, level + 1);
  2042. }
  2043. }
  2044. }
  2045. }
  2046. if (level == 0)
  2047. OnAddCompleted();
  2048. return baseDir;
  2049. }
  2050. }
  2051. }