123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684 |
- // Events.cs
- // ------------------------------------------------------------------
- //
- // Copyright (c) 2006, 2007, 2008, 2009 Dino Chiesa and Microsoft Corporation.
- // All rights reserved.
- //
- // This code module is part of DotNetZip, a zipfile class library.
- //
- // ------------------------------------------------------------------
- //
- // This code is licensed under the Microsoft Public License.
- // See the file License.txt for the license details.
- // More info on: http://dotnetzip.codeplex.com
- //
- // ------------------------------------------------------------------
- //
- // last saved (in emacs):
- // Time-stamp: <2011-August-06 12:26:24>
- //
- // ------------------------------------------------------------------
- //
- // This module defines events used by the ZipFile class.
- //
- //
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace Ionic.Zip
- {
- /// <summary>
- /// Delegate in which the application writes the <c>ZipEntry</c> content for the named entry.
- /// </summary>
- ///
- /// <param name="entryName">The name of the entry that must be written.</param>
- /// <param name="stream">The stream to which the entry data should be written.</param>
- ///
- /// <remarks>
- /// When you add an entry and specify a <c>WriteDelegate</c>, via <see
- /// cref="Ionic.Zip.ZipFile.AddEntry(string, WriteDelegate)"/>, the application
- /// code provides the logic that writes the entry data directly into the zip file.
- /// </remarks>
- ///
- /// <example>
- ///
- /// This example shows how to define a WriteDelegate that obtains a DataSet, and then
- /// writes the XML for the DataSet into the zip archive. There's no need to
- /// save the XML to a disk file first.
- ///
- /// <code lang="C#">
- /// private void WriteEntry (String filename, Stream output)
- /// {
- /// DataSet ds1 = ObtainDataSet();
- /// ds1.WriteXml(output);
- /// }
- ///
- /// private void Run()
- /// {
- /// using (var zip = new ZipFile())
- /// {
- /// zip.AddEntry(zipEntryName, WriteEntry);
- /// zip.Save(zipFileName);
- /// }
- /// }
- /// </code>
- ///
- /// <code lang="vb">
- /// Private Sub WriteEntry (ByVal filename As String, ByVal output As Stream)
- /// DataSet ds1 = ObtainDataSet()
- /// ds1.WriteXml(stream)
- /// End Sub
- ///
- /// Public Sub Run()
- /// Using zip = New ZipFile
- /// zip.AddEntry(zipEntryName, New WriteDelegate(AddressOf WriteEntry))
- /// zip.Save(zipFileName)
- /// End Using
- /// End Sub
- /// </code>
- /// </example>
- /// <seealso cref="Ionic.Zip.ZipFile.AddEntry(string, WriteDelegate)"/>
- public delegate void WriteDelegate(string entryName, System.IO.Stream stream);
- /// <summary>
- /// Delegate in which the application opens the stream, just-in-time, for the named entry.
- /// </summary>
- ///
- /// <param name="entryName">
- /// The name of the ZipEntry that the application should open the stream for.
- /// </param>
- ///
- /// <remarks>
- /// When you add an entry via <see cref="Ionic.Zip.ZipFile.AddEntry(string,
- /// OpenDelegate, CloseDelegate)"/>, the application code provides the logic that
- /// opens and closes the stream for the given ZipEntry.
- /// </remarks>
- ///
- /// <seealso cref="Ionic.Zip.ZipFile.AddEntry(string, OpenDelegate, CloseDelegate)"/>
- public delegate System.IO.Stream OpenDelegate(string entryName);
- /// <summary>
- /// Delegate in which the application closes the stream, just-in-time, for the named entry.
- /// </summary>
- ///
- /// <param name="entryName">
- /// The name of the ZipEntry that the application should close the stream for.
- /// </param>
- ///
- /// <param name="stream">The stream to be closed.</param>
- ///
- /// <remarks>
- /// When you add an entry via <see cref="Ionic.Zip.ZipFile.AddEntry(string,
- /// OpenDelegate, CloseDelegate)"/>, the application code provides the logic that
- /// opens and closes the stream for the given ZipEntry.
- /// </remarks>
- ///
- /// <seealso cref="Ionic.Zip.ZipFile.AddEntry(string, OpenDelegate, CloseDelegate)"/>
- public delegate void CloseDelegate(string entryName, System.IO.Stream stream);
- /// <summary>
- /// Delegate for the callback by which the application tells the
- /// library the CompressionLevel to use for a file.
- /// </summary>
- ///
- /// <remarks>
- /// <para>
- /// Using this callback, the application can, for example, specify that
- /// previously-compressed files (.mp3, .png, .docx, etc) should use a
- /// <c>CompressionLevel</c> of <c>None</c>, or can set the compression level based
- /// on any other factor.
- /// </para>
- /// </remarks>
- /// <seealso cref="Ionic.Zip.ZipFile.SetCompression"/>
- public delegate Ionic.Zlib.CompressionLevel SetCompressionCallback(string localFileName, string fileNameInArchive);
- /// <summary>
- /// In an EventArgs type, indicates which sort of progress event is being
- /// reported.
- /// </summary>
- /// <remarks>
- /// There are events for reading, events for saving, and events for
- /// extracting. This enumeration allows a single EventArgs type to be sued to
- /// describe one of multiple subevents. For example, a SaveProgress event is
- /// invoked before, after, and during the saving of a single entry. The value
- /// of an enum with this type, specifies which event is being triggered. The
- /// same applies to Extraction, Reading and Adding events.
- /// </remarks>
- public enum ZipProgressEventType
- {
- /// <summary>
- /// Indicates that a Add() operation has started.
- /// </summary>
- Adding_Started,
- /// <summary>
- /// Indicates that an individual entry in the archive has been added.
- /// </summary>
- Adding_AfterAddEntry,
- /// <summary>
- /// Indicates that a Add() operation has completed.
- /// </summary>
- Adding_Completed,
- /// <summary>
- /// Indicates that a Read() operation has started.
- /// </summary>
- Reading_Started,
- /// <summary>
- /// Indicates that an individual entry in the archive is about to be read.
- /// </summary>
- Reading_BeforeReadEntry,
- /// <summary>
- /// Indicates that an individual entry in the archive has just been read.
- /// </summary>
- Reading_AfterReadEntry,
- /// <summary>
- /// Indicates that a Read() operation has completed.
- /// </summary>
- Reading_Completed,
- /// <summary>
- /// The given event reports the number of bytes read so far
- /// during a Read() operation.
- /// </summary>
- Reading_ArchiveBytesRead,
- /// <summary>
- /// Indicates that a Save() operation has started.
- /// </summary>
- Saving_Started,
- /// <summary>
- /// Indicates that an individual entry in the archive is about to be written.
- /// </summary>
- Saving_BeforeWriteEntry,
- /// <summary>
- /// Indicates that an individual entry in the archive has just been saved.
- /// </summary>
- Saving_AfterWriteEntry,
- /// <summary>
- /// Indicates that a Save() operation has completed.
- /// </summary>
- Saving_Completed,
- /// <summary>
- /// Indicates that the zip archive has been created in a
- /// temporary location during a Save() operation.
- /// </summary>
- Saving_AfterSaveTempArchive,
- /// <summary>
- /// Indicates that the temporary file is about to be renamed to the final archive
- /// name during a Save() operation.
- /// </summary>
- Saving_BeforeRenameTempArchive,
- /// <summary>
- /// Indicates that the temporary file is has just been renamed to the final archive
- /// name during a Save() operation.
- /// </summary>
- Saving_AfterRenameTempArchive,
- /// <summary>
- /// Indicates that the self-extracting archive has been compiled
- /// during a Save() operation.
- /// </summary>
- Saving_AfterCompileSelfExtractor,
- /// <summary>
- /// The given event is reporting the number of source bytes that have run through the compressor so far
- /// during a Save() operation.
- /// </summary>
- Saving_EntryBytesRead,
- /// <summary>
- /// Indicates that an entry is about to be extracted.
- /// </summary>
- Extracting_BeforeExtractEntry,
- /// <summary>
- /// Indicates that an entry has just been extracted.
- /// </summary>
- Extracting_AfterExtractEntry,
- /// <summary>
- /// Indicates that extraction of an entry would overwrite an existing
- /// filesystem file. You must use
- /// <see cref="ExtractExistingFileAction.InvokeExtractProgressEvent">
- /// ExtractExistingFileAction.InvokeExtractProgressEvent</see> in the call
- /// to <c>ZipEntry.Extract()</c> in order to receive this event.
- /// </summary>
- Extracting_ExtractEntryWouldOverwrite,
- /// <summary>
- /// The given event is reporting the number of bytes written so far for
- /// the current entry during an Extract() operation.
- /// </summary>
- Extracting_EntryBytesWritten,
- /// <summary>
- /// Indicates that an ExtractAll operation is about to begin.
- /// </summary>
- Extracting_BeforeExtractAll,
- /// <summary>
- /// Indicates that an ExtractAll operation has completed.
- /// </summary>
- Extracting_AfterExtractAll,
- /// <summary>
- /// Indicates that an error has occurred while saving a zip file.
- /// This generally means the file cannot be opened, because it has been
- /// removed, or because it is locked by another process. It can also
- /// mean that the file cannot be Read, because of a range lock conflict.
- /// </summary>
- Error_Saving,
- }
- /// <summary>
- /// Provides information about the progress of a save, read, or extract operation.
- /// This is a base class; you will probably use one of the classes derived from this one.
- /// </summary>
- public class ZipProgressEventArgs : EventArgs
- {
- private int _entriesTotal;
- private bool _cancel;
- private ZipEntry _latestEntry;
- private ZipProgressEventType _flavor;
- private String _archiveName;
- private Int64 _bytesTransferred;
- private Int64 _totalBytesToTransfer;
- internal ZipProgressEventArgs() { }
- internal ZipProgressEventArgs(string archiveName, ZipProgressEventType flavor)
- {
- this._archiveName = archiveName;
- this._flavor = flavor;
- }
- /// <summary>
- /// The total number of entries to be saved or extracted.
- /// </summary>
- public int EntriesTotal
- {
- get { return _entriesTotal; }
- set { _entriesTotal = value; }
- }
- /// <summary>
- /// The name of the last entry saved or extracted.
- /// </summary>
- public ZipEntry CurrentEntry
- {
- get { return _latestEntry; }
- set { _latestEntry = value; }
- }
- /// <summary>
- /// In an event handler, set this to cancel the save or extract
- /// operation that is in progress.
- /// </summary>
- public bool Cancel
- {
- get { return _cancel; }
- set { _cancel = _cancel || value; }
- }
- /// <summary>
- /// The type of event being reported.
- /// </summary>
- public ZipProgressEventType EventType
- {
- get { return _flavor; }
- set { _flavor = value; }
- }
- /// <summary>
- /// Returns the archive name associated to this event.
- /// </summary>
- public String ArchiveName
- {
- get { return _archiveName; }
- set { _archiveName = value; }
- }
- /// <summary>
- /// The number of bytes read or written so far for this entry.
- /// </summary>
- public Int64 BytesTransferred
- {
- get { return _bytesTransferred; }
- set { _bytesTransferred = value; }
- }
- /// <summary>
- /// Total number of bytes that will be read or written for this entry.
- /// This number will be -1 if the value cannot be determined.
- /// </summary>
- public Int64 TotalBytesToTransfer
- {
- get { return _totalBytesToTransfer; }
- set { _totalBytesToTransfer = value; }
- }
- }
- /// <summary>
- /// Provides information about the progress of a Read operation.
- /// </summary>
- public class ReadProgressEventArgs : ZipProgressEventArgs
- {
- internal ReadProgressEventArgs() { }
- private ReadProgressEventArgs(string archiveName, ZipProgressEventType flavor)
- : base(archiveName, flavor)
- { }
- internal static ReadProgressEventArgs Before(string archiveName, int entriesTotal)
- {
- var x = new ReadProgressEventArgs(archiveName, ZipProgressEventType.Reading_BeforeReadEntry);
- x.EntriesTotal = entriesTotal;
- return x;
- }
- internal static ReadProgressEventArgs After(string archiveName, ZipEntry entry, int entriesTotal)
- {
- var x = new ReadProgressEventArgs(archiveName, ZipProgressEventType.Reading_AfterReadEntry);
- x.EntriesTotal = entriesTotal;
- x.CurrentEntry = entry;
- return x;
- }
- internal static ReadProgressEventArgs Started(string archiveName)
- {
- var x = new ReadProgressEventArgs(archiveName, ZipProgressEventType.Reading_Started);
- return x;
- }
- internal static ReadProgressEventArgs ByteUpdate(string archiveName, ZipEntry entry, Int64 bytesXferred, Int64 totalBytes)
- {
- var x = new ReadProgressEventArgs(archiveName, ZipProgressEventType.Reading_ArchiveBytesRead);
- x.CurrentEntry = entry;
- x.BytesTransferred = bytesXferred;
- x.TotalBytesToTransfer = totalBytes;
- return x;
- }
- internal static ReadProgressEventArgs Completed(string archiveName)
- {
- var x = new ReadProgressEventArgs(archiveName, ZipProgressEventType.Reading_Completed);
- return x;
- }
- }
- /// <summary>
- /// Provides information about the progress of a Add operation.
- /// </summary>
- public class AddProgressEventArgs : ZipProgressEventArgs
- {
- internal AddProgressEventArgs() { }
- private AddProgressEventArgs(string archiveName, ZipProgressEventType flavor)
- : base(archiveName, flavor)
- { }
- internal static AddProgressEventArgs AfterEntry(string archiveName, ZipEntry entry, int entriesTotal)
- {
- var x = new AddProgressEventArgs(archiveName, ZipProgressEventType.Adding_AfterAddEntry);
- x.EntriesTotal = entriesTotal;
- x.CurrentEntry = entry;
- return x;
- }
- internal static AddProgressEventArgs Started(string archiveName)
- {
- var x = new AddProgressEventArgs(archiveName, ZipProgressEventType.Adding_Started);
- return x;
- }
- internal static AddProgressEventArgs Completed(string archiveName)
- {
- var x = new AddProgressEventArgs(archiveName, ZipProgressEventType.Adding_Completed);
- return x;
- }
- }
- /// <summary>
- /// Provides information about the progress of a save operation.
- /// </summary>
- public class SaveProgressEventArgs : ZipProgressEventArgs
- {
- private int _entriesSaved;
- /// <summary>
- /// Constructor for the SaveProgressEventArgs.
- /// </summary>
- /// <param name="archiveName">the name of the zip archive.</param>
- /// <param name="before">whether this is before saving the entry, or after</param>
- /// <param name="entriesTotal">The total number of entries in the zip archive.</param>
- /// <param name="entriesSaved">Number of entries that have been saved.</param>
- /// <param name="entry">The entry involved in the event.</param>
- internal SaveProgressEventArgs(string archiveName, bool before, int entriesTotal, int entriesSaved, ZipEntry entry)
- : base(archiveName, (before) ? ZipProgressEventType.Saving_BeforeWriteEntry : ZipProgressEventType.Saving_AfterWriteEntry)
- {
- this.EntriesTotal = entriesTotal;
- this.CurrentEntry = entry;
- this._entriesSaved = entriesSaved;
- }
- internal SaveProgressEventArgs() { }
- internal SaveProgressEventArgs(string archiveName, ZipProgressEventType flavor)
- : base(archiveName, flavor)
- { }
- internal static SaveProgressEventArgs ByteUpdate(string archiveName, ZipEntry entry, Int64 bytesXferred, Int64 totalBytes)
- {
- var x = new SaveProgressEventArgs(archiveName, ZipProgressEventType.Saving_EntryBytesRead);
- x.ArchiveName = archiveName;
- x.CurrentEntry = entry;
- x.BytesTransferred = bytesXferred;
- x.TotalBytesToTransfer = totalBytes;
- return x;
- }
- internal static SaveProgressEventArgs Started(string archiveName)
- {
- var x = new SaveProgressEventArgs(archiveName, ZipProgressEventType.Saving_Started);
- return x;
- }
- internal static SaveProgressEventArgs Completed(string archiveName)
- {
- var x = new SaveProgressEventArgs(archiveName, ZipProgressEventType.Saving_Completed);
- return x;
- }
- /// <summary>
- /// Number of entries saved so far.
- /// </summary>
- public int EntriesSaved
- {
- get { return _entriesSaved; }
- }
- }
- /// <summary>
- /// Provides information about the progress of the extract operation.
- /// </summary>
- public class ExtractProgressEventArgs : ZipProgressEventArgs
- {
- private int _entriesExtracted;
- private string _target;
- /// <summary>
- /// Constructor for the ExtractProgressEventArgs.
- /// </summary>
- /// <param name="archiveName">the name of the zip archive.</param>
- /// <param name="before">whether this is before saving the entry, or after</param>
- /// <param name="entriesTotal">The total number of entries in the zip archive.</param>
- /// <param name="entriesExtracted">Number of entries that have been extracted.</param>
- /// <param name="entry">The entry involved in the event.</param>
- /// <param name="extractLocation">The location to which entries are extracted.</param>
- internal ExtractProgressEventArgs(string archiveName, bool before, int entriesTotal, int entriesExtracted, ZipEntry entry, string extractLocation)
- : base(archiveName, (before) ? ZipProgressEventType.Extracting_BeforeExtractEntry : ZipProgressEventType.Extracting_AfterExtractEntry)
- {
- this.EntriesTotal = entriesTotal;
- this.CurrentEntry = entry;
- this._entriesExtracted = entriesExtracted;
- this._target = extractLocation;
- }
- internal ExtractProgressEventArgs(string archiveName, ZipProgressEventType flavor)
- : base(archiveName, flavor)
- { }
- internal ExtractProgressEventArgs()
- { }
- internal static ExtractProgressEventArgs BeforeExtractEntry(string archiveName, ZipEntry entry, string extractLocation)
- {
- var x = new ExtractProgressEventArgs
- {
- ArchiveName = archiveName,
- EventType = ZipProgressEventType.Extracting_BeforeExtractEntry,
- CurrentEntry = entry,
- _target = extractLocation,
- };
- return x;
- }
- internal static ExtractProgressEventArgs ExtractExisting(string archiveName, ZipEntry entry, string extractLocation)
- {
- var x = new ExtractProgressEventArgs
- {
- ArchiveName = archiveName,
- EventType = ZipProgressEventType.Extracting_ExtractEntryWouldOverwrite,
- CurrentEntry = entry,
- _target = extractLocation,
- };
- return x;
- }
- internal static ExtractProgressEventArgs AfterExtractEntry(string archiveName, ZipEntry entry, string extractLocation)
- {
- var x = new ExtractProgressEventArgs
- {
- ArchiveName = archiveName,
- EventType = ZipProgressEventType.Extracting_AfterExtractEntry,
- CurrentEntry = entry,
- _target = extractLocation,
- };
- return x;
- }
- internal static ExtractProgressEventArgs ExtractAllStarted(string archiveName, string extractLocation)
- {
- var x = new ExtractProgressEventArgs(archiveName, ZipProgressEventType.Extracting_BeforeExtractAll);
- x._target = extractLocation;
- return x;
- }
- internal static ExtractProgressEventArgs ExtractAllCompleted(string archiveName, string extractLocation)
- {
- var x = new ExtractProgressEventArgs(archiveName, ZipProgressEventType.Extracting_AfterExtractAll);
- x._target = extractLocation;
- return x;
- }
- internal static ExtractProgressEventArgs ByteUpdate(string archiveName, ZipEntry entry, Int64 bytesWritten, Int64 totalBytes)
- {
- var x = new ExtractProgressEventArgs(archiveName, ZipProgressEventType.Extracting_EntryBytesWritten);
- x.ArchiveName = archiveName;
- x.CurrentEntry = entry;
- x.BytesTransferred = bytesWritten;
- x.TotalBytesToTransfer = totalBytes;
- return x;
- }
- /// <summary>
- /// Number of entries extracted so far. This is set only if the
- /// EventType is Extracting_BeforeExtractEntry or Extracting_AfterExtractEntry, and
- /// the Extract() is occurring witin the scope of a call to ExtractAll().
- /// </summary>
- public int EntriesExtracted
- {
- get { return _entriesExtracted; }
- }
- /// <summary>
- /// Returns the extraction target location, a filesystem path.
- /// </summary>
- public String ExtractLocation
- {
- get { return _target; }
- }
- }
- /// <summary>
- /// Provides information about the an error that occurred while zipping.
- /// </summary>
- public class ZipErrorEventArgs : ZipProgressEventArgs
- {
- private Exception _exc;
- private ZipErrorEventArgs() { }
- internal static ZipErrorEventArgs Saving(string archiveName, ZipEntry entry, Exception exception)
- {
- var x = new ZipErrorEventArgs
- {
- EventType = ZipProgressEventType.Error_Saving,
- ArchiveName = archiveName,
- CurrentEntry = entry,
- _exc = exception
- };
- return x;
- }
- /// <summary>
- /// Returns the exception that occurred, if any.
- /// </summary>
- public Exception @Exception
- {
- get { return _exc; }
- }
- /// <summary>
- /// Returns the name of the file that caused the exception, if any.
- /// </summary>
- public String FileName
- {
- get { return CurrentEntry.LocalFileName; }
- }
- }
- }
|