Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions SevenZip/ArchiveExtractCallback.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace SevenZip
{
using SevenZip.EventArguments;
using System;
using System.Collections.Generic;
using System.Globalization;
Expand Down Expand Up @@ -157,6 +158,11 @@ private void CommonInit(IInArchive archive, int filesCount, SevenZipExtractor ex
/// </summary>
public event EventHandler<ProgressEventArgs> Extracting;

/// <summary>
/// Occurs when 7z library has made progress on an operation
/// </summary>
public event EventHandler<DetailedProgressEventArgs> Progressing;

/// <summary>
/// Occurs during the extraction when a file already exists
/// </summary>
Expand Down Expand Up @@ -202,6 +208,14 @@ private void OnExtracting(ProgressEventArgs e)
}
}

private void OnProgressing(DetailedProgressEventArgs e)
{
if (Progressing != null)
{
Progressing(this, e);
}
}

private void IntEventArgsHandler(object sender, IntEventArgs e)
{
// If _bytesCount is not set, we can't update the progress.
Expand Down Expand Up @@ -240,7 +254,14 @@ public void SetTotal(ulong total)
OnOpen(new OpenEventArgs(total));
}

public void SetCompleted(ref ulong completeValue) { }
/// <summary>
/// Sets the amount of work that has been completed.
/// </summary>
/// <param name="completeValue">Amount of work that has been completed (in bytes)</param>
public void SetCompleted(ref ulong completeValue)
{
OnProgressing(new DetailedProgressEventArgs(completeValue, (ulong)_bytesCount));
}

/// <summary>
/// Sets output stream for writing unpacked data
Expand Down Expand Up @@ -292,7 +313,7 @@ public int GetStream(uint index, out ISequentialOutStream outStream, AskMode ask
}

#endregion

try
{
fileName = Path.Combine(RemoveIllegalCharacters(_directory, true), RemoveIllegalCharacters(_directoryStructure ? entryName : Path.GetFileName(entryName)));
Expand Down Expand Up @@ -452,7 +473,7 @@ public void SetOperationResult(OperationResult operationResult)
case OperationResult.UnexpectedEnd:
AddException(new ExtractionFailedException("Unexpected end of file."));
break;
case OperationResult.DataAfterEnd:
case OperationResult.DataAfterEnd:
AddException(new ExtractionFailedException("Data after end of archive."));
break;
case OperationResult.IsNotArc:
Expand Down Expand Up @@ -482,7 +503,7 @@ public void SetOperationResult(OperationResult operationResult)
_fileStream = null;
}
var iea = new FileInfoEventArgs(
_extractor.ArchiveFileData[_currentIndex], PercentDoneEventArgs.ProducePercentDone(_doneRate));
_extractor.ArchiveFileData[_currentIndex], PercentDoneEventArgs.ProducePercentDone(_doneRate));
OnFileExtractionFinished(iea);
if (iea.Cancel)
{
Expand Down
101 changes: 65 additions & 36 deletions SevenZip/ArchiveUpdateCallback.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace SevenZip
{
using SevenZip.EventArguments;
using System;
using System.Collections.Generic;
using System.IO;
Expand Down Expand Up @@ -73,7 +74,7 @@ internal sealed class ArchiveUpdateCallback : CallbackBase, IArchiveUpdateCallba
/// <summary>
/// Gets or sets the value indicating whether to compress as fast as possible, without calling events.
/// </summary>
public bool FastCompression { private get; set; }
public bool FastCompression { private get; set; }

private int _memoryPressure;

Expand Down Expand Up @@ -183,7 +184,7 @@ private void CommonInit(SevenZipCompressor compressor, UpdateData updateData, bo
}
_updateData = updateData;
_directoryStructure = directoryStructure;
DefaultItemName = "default";
DefaultItemName = "default";
}

private void Init(
Expand Down Expand Up @@ -282,7 +283,7 @@ private bool EventsForGetStream(uint index)
_fileStream.BytesRead += IntEventArgsHandler;
}
_doneRate += 1.0f / _actualFilesCount;
var fiea = new FileNameEventArgs(_files != null? _files[index].Name : _entries[index],
var fiea = new FileNameEventArgs(_files != null ? _files[index].Name : _entries[index],
PercentDoneEventArgs.ProducePercentDone(_doneRate));
OnFileCompression(fiea);
if (fiea.Cancel)
Expand All @@ -307,6 +308,11 @@ private bool EventsForGetStream(uint index)
/// </summary>
public event EventHandler<ProgressEventArgs> Compressing;

/// <summary>
/// Occurs when 7z library has made progress on an operation
/// </summary>
public event EventHandler<DetailedProgressEventArgs> Progressing;

/// <summary>
/// Occurs when the current file was compressed.
/// </summary>
Expand All @@ -327,6 +333,13 @@ private void OnCompressing(ProgressEventArgs e)
Compressing(this, e);
}
}
private void OnProgressing(DetailedProgressEventArgs e)
{
if (Progressing != null)
{
Progressing(this, e);
}
}

private void OnFileCompressionFinished(EventArgs e)
{
Expand All @@ -340,9 +353,25 @@ private void OnFileCompressionFinished(EventArgs e)

#region IArchiveUpdateCallback Members

public void SetTotal(ulong total) {}
private ulong _total;
/// <summary>
/// Gives the total size of data that will be compressed
/// </summary>
/// <param name="total">Amount of data to compress (in bytes)</param>
public void SetTotal(ulong total)
{
_total = total;
}

public void SetCompleted(ref ulong completeValue) {}

/// <summary>
/// Sets the amount of work that has been completed.
/// </summary>
/// <param name="completeValue">Amount of work that has been completed (in bytes)</param>
public void SetCompleted(ref ulong completeValue)
{
OnProgressing(new DetailedProgressEventArgs(completeValue, (ulong)_bytesCount));
}

public int GetUpdateItemInfo(uint index, ref int newData, ref int newProperties, ref uint indexInArchive)
{
Expand Down Expand Up @@ -442,7 +471,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
val = _updateData.FileNamesToModify[(int) index];
val = _updateData.FileNamesToModify[(int)index];
}
value.Value = Marshal.StringToBSTR(val);
#endregion
Expand All @@ -469,7 +498,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
value.UInt64Value = Convert.ToUInt64(_updateData.ArchiveFileData[(int) index].IsDirectory);
value.UInt64Value = Convert.ToUInt64(_updateData.ArchiveFileData[(int)index].IsDirectory);
}
break;
case ItemPropId.Size:
Expand All @@ -483,23 +512,23 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
{
if (_streams == null)
{
size = _bytesCount > 0 ? (ulong) _bytesCount : 0;
size = _bytesCount > 0 ? (ulong)_bytesCount : 0;
}
else
{
size = (ulong) (_streams[index] == null? 0 : _streams[index].Length);
size = (ulong)(_streams[index] == null ? 0 : _streams[index].Length);
}
}
else
{
size = (_files[index].Attributes & FileAttributes.Directory) == 0
? (ulong) _files[index].Length
? (ulong)_files[index].Length
: 0;
}
}
else
{
size = _updateData.ArchiveFileData[(int) index].Size;
size = _updateData.ArchiveFileData[(int)index].Size;
}
value.UInt64Value = size;

Expand All @@ -522,12 +551,12 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
value.UInt32Value = (uint) _files[index].Attributes;
value.UInt32Value = (uint)_files[index].Attributes;
}
}
else
{
value.UInt32Value = _updateData.ArchiveFileData[(int) index].Attributes;
value.UInt32Value = _updateData.ArchiveFileData[(int)index].Attributes;
}
break;
#region Times
Expand All @@ -541,7 +570,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
value.Int64Value = _updateData.ArchiveFileData[(int) index].CreationTime.ToFileTime();
value.Int64Value = _updateData.ArchiveFileData[(int)index].CreationTime.ToFileTime();
}
break;
case ItemPropId.LastAccessTime:
Expand All @@ -554,7 +583,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
value.Int64Value = _updateData.ArchiveFileData[(int) index].LastAccessTime.ToFileTime();
value.Int64Value = _updateData.ArchiveFileData[(int)index].LastAccessTime.ToFileTime();
}
break;
case ItemPropId.LastWriteTime:
Expand All @@ -567,7 +596,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
value.Int64Value = _updateData.ArchiveFileData[(int) index].LastWriteTime.ToFileTime();
value.Int64Value = _updateData.ArchiveFileData[(int)index].LastWriteTime.ToFileTime();
}
break;
#endregion
Expand All @@ -593,7 +622,7 @@ public int GetProperty(uint index, ItemPropId propID, ref PropVariant value)
}
else
{
val = Path.GetExtension(_updateData.ArchiveFileData[(int) index].FileName);
val = Path.GetExtension(_updateData.ArchiveFileData[(int)index].FileName);
value.Value = Marshal.StringToBSTR(val);
}

Expand Down Expand Up @@ -692,7 +721,7 @@ public void SetOperationResult(OperationResult operationResult)
case OperationResult.UnexpectedEnd:
AddException(new ExtractionFailedException("Unexpected end of file."));
break;
case OperationResult.DataAfterEnd:
case OperationResult.DataAfterEnd:
AddException(new ExtractionFailedException("Data after end of archive."));
break;
case OperationResult.IsNotArc:
Expand All @@ -711,21 +740,21 @@ public void SetOperationResult(OperationResult operationResult)
}
if (_fileStream != null)
{

_fileStream.BytesRead -= IntEventArgsHandler;
//Specific Zip implementation - can not Dispose files for Zip.
if (_compressor.ArchiveFormat != OutArchiveFormat.Zip)

_fileStream.BytesRead -= IntEventArgsHandler;
//Specific Zip implementation - can not Dispose files for Zip.
if (_compressor.ArchiveFormat != OutArchiveFormat.Zip)
{
try
{
try
{
_fileStream.Dispose();
}
catch (ObjectDisposedException) {}
_fileStream.Dispose();
}
else
{
_wrappersToDispose.Add(_fileStream);
}
catch (ObjectDisposedException) { }
}
else
{
_wrappersToDispose.Add(_fileStream);
}
_fileStream = null;
}
OnFileCompressionFinished(EventArgs.Empty);
Expand Down Expand Up @@ -756,7 +785,7 @@ public void Dispose()
{
_fileStream.Dispose();
}
catch (ObjectDisposedException) {}
catch (ObjectDisposedException) { }
}

if (_wrappersToDispose != null)
Expand All @@ -767,7 +796,7 @@ public void Dispose()
{
wrapper.Dispose();
}
catch (ObjectDisposedException) {}
catch (ObjectDisposedException) { }
}
}

Expand All @@ -778,11 +807,11 @@ public void Dispose()

private void IntEventArgsHandler(object sender, IntEventArgs e)
{
var lockObject = ((object) _files ?? _streams) ?? _fileStream;
var lockObject = ((object)_files ?? _streams) ?? _fileStream;

lock (lockObject)
{
var pold = (byte) (_bytesWrittenOld*100/_bytesCount);
var pold = (byte)(_bytesWrittenOld * 100 / _bytesCount);
_bytesWritten += e.Value;
byte pnow;

Expand All @@ -798,7 +827,7 @@ private void IntEventArgsHandler(object sender, IntEventArgs e)
if (pnow > pold)
{
_bytesWrittenOld = _bytesWritten;
OnCompressing(new ProgressEventArgs(pnow, (byte) (pnow - pold)));
OnCompressing(new ProgressEventArgs(pnow, (byte)(pnow - pold)));
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions SevenZip/EventArguments/DetailedProgressEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SevenZip.EventArguments
{
/// <summary>
/// Event args that pass a ulong total and a ulong completed value, that can be interpreted by the receiver.
/// </summary>
public sealed class DetailedProgressEventArgs : EventArgs
{
private readonly ulong _amountedCompleted;
private readonly ulong _total;

/// <summary>
/// Initializes a new instance of the DetailedProgressEventArgs class.
/// </summary>
/// <param name="amountCompleted">Amount of work that has been cumulatively completed.</param>
/// <param name="total">The total amount of work to complete.</param>
/// <exception cref="System.ArgumentOutOfRangeException"/>
public DetailedProgressEventArgs(ulong amountCompleted, ulong total)
{
if (amountCompleted < 0 || amountCompleted > total)
{
throw new ArgumentOutOfRangeException("amountCompleted",
"The amount of completed work (" + amountCompleted + ") must be less than the total (" + total + ").");
}

_amountedCompleted = amountCompleted;
_total = total;
}

/// <summary>
/// Gets the amount of work that has been completed.
/// </summary>
public ulong AmountCompleted => _amountedCompleted;
/// <summary>
/// Gets the total amount of work to do.
/// </summary>
public ulong TotalAmount => _total;
}
}
Loading