Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3026_IntegrationIntoSymSpace/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Packaging/DotNetZip/Events.cs @ 17228

Last change on this file since 17228 was 12074, checked in by sraggl, 10 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 24.3 KB
Line 
1// Events.cs
2// ------------------------------------------------------------------
3//
4// Copyright (c) 2006, 2007, 2008, 2009 Dino Chiesa and Microsoft Corporation.
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-August-06 12:26:24>
19//
20// ------------------------------------------------------------------
21//
22// This module defines events used by the ZipFile class.
23//
24//
25
26using System;
27using System.Collections.Generic;
28using System.Text;
29
30namespace OfficeOpenXml.Packaging.Ionic.Zip
31{
32    /// <summary>
33    ///   Delegate in which the application writes the <c>ZipEntry</c> content for the named entry.
34    /// </summary>
35    ///
36    /// <param name="entryName">The name of the entry that must be written.</param>
37    /// <param name="stream">The stream to which the entry data should be written.</param>
38    ///
39    /// <remarks>
40    ///   When you add an entry and specify a <c>WriteDelegate</c>, via <see
41    ///   cref="Ionic.Zip.ZipFile.AddEntry(string, WriteDelegate)"/>, the application
42    ///   code provides the logic that writes the entry data directly into the zip file.
43    /// </remarks>
44    ///
45    /// <example>
46    ///
47    /// This example shows how to define a WriteDelegate that obtains a DataSet, and then
48    /// writes the XML for the DataSet into the zip archive.  There's no need to
49    /// save the XML to a disk file first.
50    ///
51    /// <code lang="C#">
52    /// private void WriteEntry (String filename, Stream output)
53    /// {
54    ///     DataSet ds1 = ObtainDataSet();
55    ///     ds1.WriteXml(output);
56    /// }
57    ///
58    /// private void Run()
59    /// {
60    ///     using (var zip = new ZipFile())
61    ///     {
62    ///         zip.AddEntry(zipEntryName, WriteEntry);
63    ///         zip.Save(zipFileName);
64    ///     }
65    /// }
66    /// </code>
67    ///
68    /// <code lang="vb">
69    /// Private Sub WriteEntry (ByVal filename As String, ByVal output As Stream)
70    ///     DataSet ds1 = ObtainDataSet()
71    ///     ds1.WriteXml(stream)
72    /// End Sub
73    ///
74    /// Public Sub Run()
75    ///     Using zip = New ZipFile
76    ///         zip.AddEntry(zipEntryName, New WriteDelegate(AddressOf WriteEntry))
77    ///         zip.Save(zipFileName)
78    ///     End Using
79    /// End Sub
80    /// </code>
81    /// </example>
82    /// <seealso cref="Ionic.Zip.ZipFile.AddEntry(string, WriteDelegate)"/>
83    public delegate void WriteDelegate(string entryName, System.IO.Stream stream);
84
85
86    /// <summary>
87    ///   Delegate in which the application opens the stream, just-in-time, for the named entry.
88    /// </summary>
89    ///
90    /// <param name="entryName">
91    /// The name of the ZipEntry that the application should open the stream for.
92    /// </param>
93    ///
94    /// <remarks>
95    ///   When you add an entry via <see cref="Ionic.Zip.ZipFile.AddEntry(string,
96    ///   OpenDelegate, CloseDelegate)"/>, the application code provides the logic that
97    ///   opens and closes the stream for the given ZipEntry.
98    /// </remarks>
99    ///
100    /// <seealso cref="Ionic.Zip.ZipFile.AddEntry(string, OpenDelegate, CloseDelegate)"/>
101    public delegate System.IO.Stream OpenDelegate(string entryName);
102
103    /// <summary>
104    ///   Delegate in which the application closes the stream, just-in-time, for the named entry.
105    /// </summary>
106    ///
107    /// <param name="entryName">
108    /// The name of the ZipEntry that the application should close the stream for.
109    /// </param>
110    ///
111    /// <param name="stream">The stream to be closed.</param>
112    ///
113    /// <remarks>
114    ///   When you add an entry via <see cref="Ionic.Zip.ZipFile.AddEntry(string,
115    ///   OpenDelegate, CloseDelegate)"/>, the application code provides the logic that
116    ///   opens and closes the stream for the given ZipEntry.
117    /// </remarks>
118    ///
119    /// <seealso cref="Ionic.Zip.ZipFile.AddEntry(string, OpenDelegate, CloseDelegate)"/>
120    public delegate void CloseDelegate(string entryName, System.IO.Stream stream);
121
122    /// <summary>
123    ///   Delegate for the callback by which the application tells the
124    ///   library the CompressionLevel to use for a file.
125    /// </summary>
126    ///
127    /// <remarks>
128    /// <para>
129    ///   Using this callback, the application can, for example, specify that
130    ///   previously-compressed files (.mp3, .png, .docx, etc) should use a
131    ///   <c>CompressionLevel</c> of <c>None</c>, or can set the compression level based
132    ///   on any other factor.
133    /// </para>
134    /// </remarks>
135    /// <seealso cref="Ionic.Zip.ZipFile.SetCompression"/>
136    public delegate OfficeOpenXml.Packaging.Ionic.Zlib.CompressionLevel SetCompressionCallback(string localFileName, string fileNameInArchive);
137
138    /// <summary>
139    ///   In an EventArgs type, indicates which sort of progress event is being
140    ///   reported.
141    /// </summary>
142    /// <remarks>
143    ///   There are events for reading, events for saving, and events for
144    ///   extracting. This enumeration allows a single EventArgs type to be sued to
145    ///   describe one of multiple subevents. For example, a SaveProgress event is
146    ///   invoked before, after, and during the saving of a single entry.  The value
147    ///   of an enum with this type, specifies which event is being triggered.  The
148    ///   same applies to Extraction, Reading and Adding events.
149    /// </remarks>
150    internal enum ZipProgressEventType
151    {
152        /// <summary>
153        /// Indicates that a Add() operation has started.
154        /// </summary>
155        Adding_Started,
156
157        /// <summary>
158        /// Indicates that an individual entry in the archive has been added.
159        /// </summary>
160        Adding_AfterAddEntry,
161
162        /// <summary>
163        /// Indicates that a Add() operation has completed.
164        /// </summary>
165        Adding_Completed,
166
167        /// <summary>
168        /// Indicates that a Read() operation has started.
169        /// </summary>
170        Reading_Started,
171
172        /// <summary>
173        /// Indicates that an individual entry in the archive is about to be read.
174        /// </summary>
175        Reading_BeforeReadEntry,
176
177        /// <summary>
178        /// Indicates that an individual entry in the archive has just been read.
179        /// </summary>
180        Reading_AfterReadEntry,
181
182        /// <summary>
183        /// Indicates that a Read() operation has completed.
184        /// </summary>
185        Reading_Completed,
186
187        /// <summary>
188        /// The given event reports the number of bytes read so far
189        /// during a Read() operation.
190        /// </summary>
191        Reading_ArchiveBytesRead,
192
193        /// <summary>
194        /// Indicates that a Save() operation has started.
195        /// </summary>
196        Saving_Started,
197
198        /// <summary>
199        /// Indicates that an individual entry in the archive is about to be written.
200        /// </summary>
201        Saving_BeforeWriteEntry,
202
203        /// <summary>
204        /// Indicates that an individual entry in the archive has just been saved.
205        /// </summary>
206        Saving_AfterWriteEntry,
207
208        /// <summary>
209        /// Indicates that a Save() operation has completed.
210        /// </summary>
211        Saving_Completed,
212
213        /// <summary>
214        /// Indicates that the zip archive has been created in a
215        /// temporary location during a Save() operation.
216        /// </summary>
217        Saving_AfterSaveTempArchive,
218
219        /// <summary>
220        /// Indicates that the temporary file is about to be renamed to the final archive
221        /// name during a Save() operation.
222        /// </summary>
223        Saving_BeforeRenameTempArchive,
224
225        /// <summary>
226        /// Indicates that the temporary file is has just been renamed to the final archive
227        /// name during a Save() operation.
228        /// </summary>
229        Saving_AfterRenameTempArchive,
230
231        /// <summary>
232        /// Indicates that the self-extracting archive has been compiled
233        /// during a Save() operation.
234        /// </summary>
235        Saving_AfterCompileSelfExtractor,
236
237        /// <summary>
238        /// The given event is reporting the number of source bytes that have run through the compressor so far
239        /// during a Save() operation.
240        /// </summary>
241        Saving_EntryBytesRead,
242
243        /// <summary>
244        /// Indicates that an entry is about to be extracted.
245        /// </summary>
246        Extracting_BeforeExtractEntry,
247
248        /// <summary>
249        /// Indicates that an entry has just been extracted.
250        /// </summary>
251        Extracting_AfterExtractEntry,
252
253        /// <summary>
254        ///   Indicates that extraction of an entry would overwrite an existing
255        ///   filesystem file. You must use
256        ///   <see cref="ExtractExistingFileAction.InvokeExtractProgressEvent">
257        ///   ExtractExistingFileAction.InvokeExtractProgressEvent</see> in the call
258        ///   to <c>ZipEntry.Extract()</c> in order to receive this event.
259        /// </summary>
260        Extracting_ExtractEntryWouldOverwrite,
261
262        /// <summary>
263        ///   The given event is reporting the number of bytes written so far for
264        ///   the current entry during an Extract() operation.
265        /// </summary>
266        Extracting_EntryBytesWritten,
267
268        /// <summary>
269        /// Indicates that an ExtractAll operation is about to begin.
270        /// </summary>
271        Extracting_BeforeExtractAll,
272
273        /// <summary>
274        /// Indicates that an ExtractAll operation has completed.
275        /// </summary>
276        Extracting_AfterExtractAll,
277
278        /// <summary>
279        /// Indicates that an error has occurred while saving a zip file.
280        /// This generally means the file cannot be opened, because it has been
281        /// removed, or because it is locked by another process.  It can also
282        /// mean that the file cannot be Read, because of a range lock conflict.
283        /// </summary>
284        Error_Saving,
285    }
286
287
288    /// <summary>
289    /// Provides information about the progress of a save, read, or extract operation.
290    /// This is a base class; you will probably use one of the classes derived from this one.
291    /// </summary>
292    internal class ZipProgressEventArgs : EventArgs
293    {
294        private int _entriesTotal;
295        private bool _cancel;
296        private ZipEntry _latestEntry;
297        private ZipProgressEventType _flavor;
298        private String _archiveName;
299        private Int64 _bytesTransferred;
300        private Int64 _totalBytesToTransfer;
301
302
303        internal ZipProgressEventArgs() { }
304
305        internal ZipProgressEventArgs(string archiveName, ZipProgressEventType flavor)
306        {
307            this._archiveName = archiveName;
308            this._flavor = flavor;
309        }
310
311        /// <summary>
312        /// The total number of entries to be saved or extracted.
313        /// </summary>
314        public int EntriesTotal
315        {
316            get { return _entriesTotal; }
317            set { _entriesTotal = value; }
318        }
319
320        /// <summary>
321        /// The name of the last entry saved or extracted.
322        /// </summary>
323        public ZipEntry CurrentEntry
324        {
325            get { return _latestEntry; }
326            set { _latestEntry = value; }
327        }
328
329        /// <summary>
330        /// In an event handler, set this to cancel the save or extract
331        /// operation that is in progress.
332        /// </summary>
333        public bool Cancel
334        {
335            get { return _cancel; }
336            set { _cancel = _cancel || value; }
337        }
338
339        /// <summary>
340        /// The type of event being reported.
341        /// </summary>
342        public ZipProgressEventType EventType
343        {
344            get { return _flavor; }
345            set { _flavor = value; }
346        }
347
348        /// <summary>
349        /// Returns the archive name associated to this event.
350        /// </summary>
351        public String ArchiveName
352        {
353            get { return _archiveName; }
354            set { _archiveName = value; }
355        }
356
357
358        /// <summary>
359        /// The number of bytes read or written so far for this entry.
360        /// </summary>
361        public Int64 BytesTransferred
362        {
363            get { return _bytesTransferred; }
364            set { _bytesTransferred = value; }
365        }
366
367
368
369        /// <summary>
370        /// Total number of bytes that will be read or written for this entry.
371        /// This number will be -1 if the value cannot be determined.
372        /// </summary>
373        public Int64 TotalBytesToTransfer
374        {
375            get { return _totalBytesToTransfer; }
376            set { _totalBytesToTransfer = value; }
377        }
378    }
379
380
381
382    /// <summary>
383    /// Provides information about the progress of a Read operation.
384    /// </summary>
385    internal class ReadProgressEventArgs : ZipProgressEventArgs
386    {
387
388        internal ReadProgressEventArgs() { }
389
390        private ReadProgressEventArgs(string archiveName, ZipProgressEventType flavor)
391            : base(archiveName, flavor)
392        { }
393
394        internal static ReadProgressEventArgs Before(string archiveName, int entriesTotal)
395        {
396            var x = new ReadProgressEventArgs(archiveName, ZipProgressEventType.Reading_BeforeReadEntry);
397            x.EntriesTotal = entriesTotal;
398            return x;
399        }
400
401        internal static ReadProgressEventArgs After(string archiveName, ZipEntry entry, int entriesTotal)
402        {
403            var x = new ReadProgressEventArgs(archiveName, ZipProgressEventType.Reading_AfterReadEntry);
404            x.EntriesTotal = entriesTotal;
405            x.CurrentEntry = entry;
406            return x;
407        }
408
409        internal static ReadProgressEventArgs Started(string archiveName)
410        {
411            var x = new ReadProgressEventArgs(archiveName, ZipProgressEventType.Reading_Started);
412            return x;
413        }
414
415        internal static ReadProgressEventArgs ByteUpdate(string archiveName, ZipEntry entry, Int64 bytesXferred, Int64 totalBytes)
416        {
417            var x = new ReadProgressEventArgs(archiveName, ZipProgressEventType.Reading_ArchiveBytesRead);
418            x.CurrentEntry = entry;
419            x.BytesTransferred = bytesXferred;
420            x.TotalBytesToTransfer = totalBytes;
421            return x;
422        }
423
424        internal static ReadProgressEventArgs Completed(string archiveName)
425        {
426            var x = new ReadProgressEventArgs(archiveName, ZipProgressEventType.Reading_Completed);
427            return x;
428        }
429
430    }
431
432
433    /// <summary>
434    /// Provides information about the progress of a Add operation.
435    /// </summary>
436    internal class AddProgressEventArgs : ZipProgressEventArgs
437    {
438        internal AddProgressEventArgs() { }
439
440        private AddProgressEventArgs(string archiveName, ZipProgressEventType flavor)
441            : base(archiveName, flavor)
442        { }
443
444        internal static AddProgressEventArgs AfterEntry(string archiveName, ZipEntry entry, int entriesTotal)
445        {
446            var x = new AddProgressEventArgs(archiveName, ZipProgressEventType.Adding_AfterAddEntry);
447            x.EntriesTotal = entriesTotal;
448            x.CurrentEntry = entry;
449            return x;
450        }
451
452        internal static AddProgressEventArgs Started(string archiveName)
453        {
454            var x = new AddProgressEventArgs(archiveName, ZipProgressEventType.Adding_Started);
455            return x;
456        }
457
458        internal static AddProgressEventArgs Completed(string archiveName)
459        {
460            var x = new AddProgressEventArgs(archiveName, ZipProgressEventType.Adding_Completed);
461            return x;
462        }
463
464    }
465
466    /// <summary>
467    /// Provides information about the progress of a save operation.
468    /// </summary>
469    internal class SaveProgressEventArgs : ZipProgressEventArgs
470    {
471        private int _entriesSaved;
472
473        /// <summary>
474        /// Constructor for the SaveProgressEventArgs.
475        /// </summary>
476        /// <param name="archiveName">the name of the zip archive.</param>
477        /// <param name="before">whether this is before saving the entry, or after</param>
478        /// <param name="entriesTotal">The total number of entries in the zip archive.</param>
479        /// <param name="entriesSaved">Number of entries that have been saved.</param>
480        /// <param name="entry">The entry involved in the event.</param>
481        internal SaveProgressEventArgs(string archiveName, bool before, int entriesTotal, int entriesSaved, ZipEntry entry)
482            : base(archiveName, (before) ? ZipProgressEventType.Saving_BeforeWriteEntry : ZipProgressEventType.Saving_AfterWriteEntry)
483        {
484            this.EntriesTotal = entriesTotal;
485            this.CurrentEntry = entry;
486            this._entriesSaved = entriesSaved;
487        }
488
489        internal SaveProgressEventArgs() { }
490
491        internal SaveProgressEventArgs(string archiveName, ZipProgressEventType flavor)
492            : base(archiveName, flavor)
493        { }
494
495
496        internal static SaveProgressEventArgs ByteUpdate(string archiveName, ZipEntry entry, Int64 bytesXferred, Int64 totalBytes)
497        {
498            var x = new SaveProgressEventArgs(archiveName, ZipProgressEventType.Saving_EntryBytesRead);
499            x.ArchiveName = archiveName;
500            x.CurrentEntry = entry;
501            x.BytesTransferred = bytesXferred;
502            x.TotalBytesToTransfer = totalBytes;
503            return x;
504        }
505
506        internal static SaveProgressEventArgs Started(string archiveName)
507        {
508            var x = new SaveProgressEventArgs(archiveName, ZipProgressEventType.Saving_Started);
509            return x;
510        }
511
512        internal static SaveProgressEventArgs Completed(string archiveName)
513        {
514            var x = new SaveProgressEventArgs(archiveName, ZipProgressEventType.Saving_Completed);
515            return x;
516        }
517
518        /// <summary>
519        /// Number of entries saved so far.
520        /// </summary>
521        public int EntriesSaved
522        {
523            get { return _entriesSaved; }
524        }
525    }
526
527
528    /// <summary>
529    /// Provides information about the progress of the extract operation.
530    /// </summary>
531    internal class ExtractProgressEventArgs : ZipProgressEventArgs
532    {
533        private int _entriesExtracted;
534        private string _target;
535
536        /// <summary>
537        /// Constructor for the ExtractProgressEventArgs.
538        /// </summary>
539        /// <param name="archiveName">the name of the zip archive.</param>
540        /// <param name="before">whether this is before saving the entry, or after</param>
541        /// <param name="entriesTotal">The total number of entries in the zip archive.</param>
542        /// <param name="entriesExtracted">Number of entries that have been extracted.</param>
543        /// <param name="entry">The entry involved in the event.</param>
544        /// <param name="extractLocation">The location to which entries are extracted.</param>
545        internal ExtractProgressEventArgs(string archiveName, bool before, int entriesTotal, int entriesExtracted, ZipEntry entry, string extractLocation)
546            : base(archiveName, (before) ? ZipProgressEventType.Extracting_BeforeExtractEntry : ZipProgressEventType.Extracting_AfterExtractEntry)
547        {
548            this.EntriesTotal = entriesTotal;
549            this.CurrentEntry = entry;
550            this._entriesExtracted = entriesExtracted;
551            this._target = extractLocation;
552        }
553
554        internal ExtractProgressEventArgs(string archiveName, ZipProgressEventType flavor)
555            : base(archiveName, flavor)
556        { }
557
558        internal ExtractProgressEventArgs()
559        { }
560
561
562        internal static ExtractProgressEventArgs BeforeExtractEntry(string archiveName, ZipEntry entry, string extractLocation)
563        {
564            var x = new ExtractProgressEventArgs
565                {
566                    ArchiveName = archiveName,
567                    EventType = ZipProgressEventType.Extracting_BeforeExtractEntry,
568                    CurrentEntry = entry,
569                    _target = extractLocation,
570                };
571            return x;
572        }
573
574        internal static ExtractProgressEventArgs ExtractExisting(string archiveName, ZipEntry entry, string extractLocation)
575        {
576            var x = new ExtractProgressEventArgs
577                {
578                    ArchiveName = archiveName,
579                    EventType = ZipProgressEventType.Extracting_ExtractEntryWouldOverwrite,
580                    CurrentEntry = entry,
581                    _target = extractLocation,
582                };
583            return x;
584        }
585
586        internal static ExtractProgressEventArgs AfterExtractEntry(string archiveName, ZipEntry entry, string extractLocation)
587        {
588            var x = new ExtractProgressEventArgs
589                {
590                    ArchiveName = archiveName,
591                    EventType = ZipProgressEventType.Extracting_AfterExtractEntry,
592                    CurrentEntry = entry,
593                    _target = extractLocation,
594                };
595            return x;
596        }
597
598        internal static ExtractProgressEventArgs ExtractAllStarted(string archiveName, string extractLocation)
599        {
600            var x = new ExtractProgressEventArgs(archiveName, ZipProgressEventType.Extracting_BeforeExtractAll);
601            x._target = extractLocation;
602            return x;
603        }
604
605        internal static ExtractProgressEventArgs ExtractAllCompleted(string archiveName, string extractLocation)
606        {
607            var x = new ExtractProgressEventArgs(archiveName, ZipProgressEventType.Extracting_AfterExtractAll);
608            x._target = extractLocation;
609            return x;
610        }
611
612
613        internal static ExtractProgressEventArgs ByteUpdate(string archiveName, ZipEntry entry, Int64 bytesWritten, Int64 totalBytes)
614        {
615            var x = new ExtractProgressEventArgs(archiveName, ZipProgressEventType.Extracting_EntryBytesWritten);
616            x.ArchiveName = archiveName;
617            x.CurrentEntry = entry;
618            x.BytesTransferred = bytesWritten;
619            x.TotalBytesToTransfer = totalBytes;
620            return x;
621        }
622
623
624
625        /// <summary>
626        /// Number of entries extracted so far.  This is set only if the
627        /// EventType is Extracting_BeforeExtractEntry or Extracting_AfterExtractEntry, and
628        /// the Extract() is occurring witin the scope of a call to ExtractAll().
629        /// </summary>
630        public int EntriesExtracted
631        {
632            get { return _entriesExtracted; }
633        }
634
635        /// <summary>
636        /// Returns the extraction target location, a filesystem path.
637        /// </summary>
638        public String ExtractLocation
639        {
640            get { return _target; }
641        }
642
643    }
644
645
646
647    /// <summary>
648    /// Provides information about the an error that occurred while zipping.
649    /// </summary>
650    internal class ZipErrorEventArgs : ZipProgressEventArgs
651    {
652        private Exception _exc;
653        private ZipErrorEventArgs() { }
654        internal static ZipErrorEventArgs Saving(string archiveName, ZipEntry entry, Exception exception)
655        {
656            var x = new ZipErrorEventArgs
657                {
658                    EventType = ZipProgressEventType.Error_Saving,
659                    ArchiveName = archiveName,
660                    CurrentEntry = entry,
661                    _exc = exception
662                };
663            return x;
664        }
665
666        /// <summary>
667        /// Returns the exception that occurred, if any.
668        /// </summary>
669        public Exception @Exception
670        {
671            get { return _exc; }
672        }
673
674        /// <summary>
675        /// Returns the name of the file that caused the exception, if any.
676        /// </summary>
677        public String FileName
678        {
679            get { return CurrentEntry.LocalFileName; }
680        }
681    }
682
683
684}
Note: See TracBrowser for help on using the repository browser.