/* This file is part of SevenZipSharp.
SevenZipSharp is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SevenZipSharp is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with SevenZipSharp. If not, see .
*/
using System;
using System.IO;
namespace SevenZip
{
///
/// The definition of the interface which supports the cancellation of a process.
///
public interface ICancellable
{
///
/// Gets or sets whether to stop the current archive operation.
///
bool Cancel { get; set; }
}
///
/// EventArgs for storing PercentDone property.
///
public class PercentDoneEventArgs : EventArgs
{
private readonly byte _percentDone;
///
/// Initializes a new instance of the PercentDoneEventArgs class.
///
/// The percent of finished work.
///
public PercentDoneEventArgs(byte percentDone)
{
if (percentDone > 100 || percentDone < 0)
{
throw new ArgumentOutOfRangeException("percentDone",
"The percent of finished work must be between 0 and 100.");
}
_percentDone = percentDone;
}
///
/// Gets the percent of finished work.
///
public byte PercentDone
{
get
{
return _percentDone;
}
}
///
/// Converts a [0, 1] rate to its percent equivalent.
///
/// The rate of the done work.
/// Percent integer equivalent.
///
internal static byte ProducePercentDone(float doneRate)
{
#if !WINCE
return (byte) Math.Round(Math.Min(100*doneRate, 100), MidpointRounding.AwayFromZero);
#else
return (byte) Math.Round(Math.Min(100*doneRate, 100));
#endif
}
}
///
/// The EventArgs class for accurate progress handling.
///
public sealed class ProgressEventArgs : PercentDoneEventArgs
{
private readonly byte _delta;
///
/// Initializes a new instance of the ProgressEventArgs class.
///
/// The percent of finished work.
/// The percent of work done after the previous event.
public ProgressEventArgs(byte percentDone, byte percentDelta)
: base(percentDone)
{
_delta = percentDelta;
}
///
/// Gets the change in done work percentage.
///
public byte PercentDelta
{
get
{
return _delta;
}
}
}
#if UNMANAGED
///
/// EventArgs used to report the file information which is going to be packed.
///
public sealed class FileInfoEventArgs : PercentDoneEventArgs, ICancellable
{
private readonly ArchiveFileInfo _fileInfo;
///
/// Initializes a new instance of the FileInfoEventArgs class.
///
/// The current ArchiveFileInfo.
/// The percent of finished work.
public FileInfoEventArgs(ArchiveFileInfo fileInfo, byte percentDone)
: base(percentDone)
{
_fileInfo = fileInfo;
}
///
/// Gets or sets whether to stop the current archive operation.
///
public bool Cancel { get; set; }
///
/// Gets the corresponding FileInfo to the event.
///
public ArchiveFileInfo FileInfo
{
get
{
return _fileInfo;
}
}
}
///
/// EventArgs used to report the size of unpacked archive data
///
public sealed class OpenEventArgs : EventArgs
{
private readonly ulong _totalSize;
///
/// Initializes a new instance of the OpenEventArgs class
///
/// Size of unpacked archive data
public OpenEventArgs(ulong totalSize)
{
_totalSize = totalSize;
}
///
/// Gets the size of unpacked archive data
///
public ulong TotalSize
{
get
{
return _totalSize;
}
}
}
///
/// Stores an int number
///
public sealed class IntEventArgs : EventArgs
{
private readonly int _value;
///
/// Initializes a new instance of the IntEventArgs class
///
/// Useful data carried by the IntEventArgs class
public IntEventArgs(int value)
{
_value = value;
}
///
/// Gets the value of the IntEventArgs class
///
public int Value
{
get
{
return _value;
}
}
}
///
/// EventArgs class which stores the file name.
///
public sealed class FileNameEventArgs : PercentDoneEventArgs, ICancellable
{
private readonly string _fileName;
///
/// Initializes a new instance of the FileNameEventArgs class.
///
/// The file name.
/// The percent of finished work
public FileNameEventArgs(string fileName, byte percentDone) :
base(percentDone)
{
_fileName = fileName;
}
///
/// Gets or sets whether to stop the current archive operation.
///
public bool Cancel { get; set; }
///
/// Gets the file name.
///
public string FileName
{
get
{
return _fileName;
}
}
}
///
/// EventArgs for FileExists event, stores the file name and asks whether to overwrite it in case it already exists.
///
public sealed class FileOverwriteEventArgs : EventArgs
{
///
/// Initializes a new instance of the FileOverwriteEventArgs class
///
/// The file name.
public FileOverwriteEventArgs(string fileName)
{
FileName = fileName;
}
///
/// Gets or sets the value indicating whether to cancel the extraction.
///
public bool Cancel { get; set; }
///
/// Gets or sets the file name to extract to. Null means skip.
///
public string FileName { get; set; }
}
///
/// The reason for calling .
///
public enum ExtractFileCallbackReason
{
///
/// is called the first time for a file.
///
Start,
///
/// All data has been written to the target without any exceptions.
///
Done,
///
/// An exception occured during extraction of the file.
///
Failure
}
///
/// The arguments passed to .
///
///
/// For each file, is first called with
/// set to . If the callback chooses to extract the
/// file data by setting or , the callback
/// will be called a second time with set to
/// or
/// to allow for any cleanup task like closing the stream.
///
public class ExtractFileCallbackArgs : EventArgs
{
private readonly ArchiveFileInfo _archiveFileInfo;
private Stream _extractToStream;
///
/// Initializes a new instance of the class.
///
/// The information about file in the archive.
public ExtractFileCallbackArgs(ArchiveFileInfo archiveFileInfo)
{
Reason = ExtractFileCallbackReason.Start;
_archiveFileInfo = archiveFileInfo;
}
///
/// Information about file in the archive.
///
/// Information about file in the archive.
public ArchiveFileInfo ArchiveFileInfo
{
get
{
return _archiveFileInfo;
}
}
///
/// The reason for calling .
///
///
/// If neither nor is set,
/// will not be called after .
///
/// The reason.
public ExtractFileCallbackReason Reason { get; internal set; }
///
/// The exception that occurred during extraction.
///
/// The _Exception.
///
/// If the callback is called with set to ,
/// this member contains the _Exception that occurred.
/// The default behavior is to rethrow the _Exception after return of the callback.
/// However the callback can set to null to swallow the _Exception
/// and continue extraction with the next file.
///
public Exception Exception { get; set; }
///
/// Gets or sets a value indicating whether to cancel the extraction.
///
/// true to cancel the extraction; false to continue. The default is false.
public bool CancelExtraction { get; set; }
///
/// Gets or sets whether and where to extract the file.
///
/// The path where to extract the file to.
///
/// If is set, this mmember will be ignored.
///
public string ExtractToFile { get; set; }
///
/// Gets or sets whether and where to extract the file.
///
/// The the extracted data is written to.
///
/// If both this member and are null (the defualt), the file
/// will not be extracted and the callback will be be executed a second time with the
/// set to or .
///
public Stream ExtractToStream
{
get
{
return _extractToStream;
}
set
{
if (_extractToStream != null && !_extractToStream.CanWrite)
{
throw new ExtractionFailedException("The specified stream is not writable!");
}
_extractToStream = value;
}
}
///
/// Gets or sets any data that will be preserved between the callback call
/// and the or calls.
///
/// The data.
public object ObjectData { get; set; }
}
///
/// Callback delegate for .
///
public delegate void ExtractFileCallback(ExtractFileCallbackArgs extractFileCallbackArgs);
#endif
}