// 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 { /// /// Delegate in which the application writes the ZipEntry content for the named entry. /// /// /// The name of the entry that must be written. /// The stream to which the entry data should be written. /// /// /// When you add an entry and specify a WriteDelegate, via , the application /// code provides the logic that writes the entry data directly into the zip file. /// /// /// /// /// 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. /// /// /// 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); /// } /// } /// /// /// /// 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 /// /// /// public delegate void WriteDelegate(string entryName, System.IO.Stream stream); /// /// Delegate in which the application opens the stream, just-in-time, for the named entry. /// /// /// /// The name of the ZipEntry that the application should open the stream for. /// /// /// /// When you add an entry via , the application code provides the logic that /// opens and closes the stream for the given ZipEntry. /// /// /// public delegate System.IO.Stream OpenDelegate(string entryName); /// /// Delegate in which the application closes the stream, just-in-time, for the named entry. /// /// /// /// The name of the ZipEntry that the application should close the stream for. /// /// /// The stream to be closed. /// /// /// When you add an entry via , the application code provides the logic that /// opens and closes the stream for the given ZipEntry. /// /// /// public delegate void CloseDelegate(string entryName, System.IO.Stream stream); /// /// Delegate for the callback by which the application tells the /// library the CompressionLevel to use for a file. /// /// /// /// /// Using this callback, the application can, for example, specify that /// previously-compressed files (.mp3, .png, .docx, etc) should use a /// CompressionLevel of None, or can set the compression level based /// on any other factor. /// /// /// public delegate Ionic.Zlib.CompressionLevel SetCompressionCallback(string localFileName, string fileNameInArchive); /// /// In an EventArgs type, indicates which sort of progress event is being /// reported. /// /// /// 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. /// public enum ZipProgressEventType { /// /// Indicates that a Add() operation has started. /// Adding_Started, /// /// Indicates that an individual entry in the archive has been added. /// Adding_AfterAddEntry, /// /// Indicates that a Add() operation has completed. /// Adding_Completed, /// /// Indicates that a Read() operation has started. /// Reading_Started, /// /// Indicates that an individual entry in the archive is about to be read. /// Reading_BeforeReadEntry, /// /// Indicates that an individual entry in the archive has just been read. /// Reading_AfterReadEntry, /// /// Indicates that a Read() operation has completed. /// Reading_Completed, /// /// The given event reports the number of bytes read so far /// during a Read() operation. /// Reading_ArchiveBytesRead, /// /// Indicates that a Save() operation has started. /// Saving_Started, /// /// Indicates that an individual entry in the archive is about to be written. /// Saving_BeforeWriteEntry, /// /// Indicates that an individual entry in the archive has just been saved. /// Saving_AfterWriteEntry, /// /// Indicates that a Save() operation has completed. /// Saving_Completed, /// /// Indicates that the zip archive has been created in a /// temporary location during a Save() operation. /// Saving_AfterSaveTempArchive, /// /// Indicates that the temporary file is about to be renamed to the final archive /// name during a Save() operation. /// Saving_BeforeRenameTempArchive, /// /// Indicates that the temporary file is has just been renamed to the final archive /// name during a Save() operation. /// Saving_AfterRenameTempArchive, /// /// Indicates that the self-extracting archive has been compiled /// during a Save() operation. /// Saving_AfterCompileSelfExtractor, /// /// The given event is reporting the number of source bytes that have run through the compressor so far /// during a Save() operation. /// Saving_EntryBytesRead, /// /// Indicates that an entry is about to be extracted. /// Extracting_BeforeExtractEntry, /// /// Indicates that an entry has just been extracted. /// Extracting_AfterExtractEntry, /// /// Indicates that extraction of an entry would overwrite an existing /// filesystem file. You must use /// /// ExtractExistingFileAction.InvokeExtractProgressEvent in the call /// to ZipEntry.Extract() in order to receive this event. /// Extracting_ExtractEntryWouldOverwrite, /// /// The given event is reporting the number of bytes written so far for /// the current entry during an Extract() operation. /// Extracting_EntryBytesWritten, /// /// Indicates that an ExtractAll operation is about to begin. /// Extracting_BeforeExtractAll, /// /// Indicates that an ExtractAll operation has completed. /// Extracting_AfterExtractAll, /// /// 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. /// Error_Saving, } /// /// 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. /// 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; } /// /// The total number of entries to be saved or extracted. /// public int EntriesTotal { get { return _entriesTotal; } set { _entriesTotal = value; } } /// /// The name of the last entry saved or extracted. /// public ZipEntry CurrentEntry { get { return _latestEntry; } set { _latestEntry = value; } } /// /// In an event handler, set this to cancel the save or extract /// operation that is in progress. /// public bool Cancel { get { return _cancel; } set { _cancel = _cancel || value; } } /// /// The type of event being reported. /// public ZipProgressEventType EventType { get { return _flavor; } set { _flavor = value; } } /// /// Returns the archive name associated to this event. /// public String ArchiveName { get { return _archiveName; } set { _archiveName = value; } } /// /// The number of bytes read or written so far for this entry. /// public Int64 BytesTransferred { get { return _bytesTransferred; } set { _bytesTransferred = value; } } /// /// Total number of bytes that will be read or written for this entry. /// This number will be -1 if the value cannot be determined. /// public Int64 TotalBytesToTransfer { get { return _totalBytesToTransfer; } set { _totalBytesToTransfer = value; } } } /// /// Provides information about the progress of a Read operation. /// 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; } } /// /// Provides information about the progress of a Add operation. /// 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; } } /// /// Provides information about the progress of a save operation. /// public class SaveProgressEventArgs : ZipProgressEventArgs { private int _entriesSaved; /// /// Constructor for the SaveProgressEventArgs. /// /// the name of the zip archive. /// whether this is before saving the entry, or after /// The total number of entries in the zip archive. /// Number of entries that have been saved. /// The entry involved in the event. 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; } /// /// Number of entries saved so far. /// public int EntriesSaved { get { return _entriesSaved; } } } /// /// Provides information about the progress of the extract operation. /// public class ExtractProgressEventArgs : ZipProgressEventArgs { private int _entriesExtracted; private string _target; /// /// Constructor for the ExtractProgressEventArgs. /// /// the name of the zip archive. /// whether this is before saving the entry, or after /// The total number of entries in the zip archive. /// Number of entries that have been extracted. /// The entry involved in the event. /// The location to which entries are extracted. 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; } /// /// 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(). /// public int EntriesExtracted { get { return _entriesExtracted; } } /// /// Returns the extraction target location, a filesystem path. /// public String ExtractLocation { get { return _target; } } } /// /// Provides information about the an error that occurred while zipping. /// 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; } /// /// Returns the exception that occurred, if any. /// public Exception @Exception { get { return _exc; } } /// /// Returns the name of the file that caused the exception, if any. /// public String FileName { get { return CurrentEntry.LocalFileName; } } } }