Skip to content

Commit

Permalink
merge squid-box#78
Browse files Browse the repository at this point in the history
and remove ArgumentOutOfRangeException which in DetailedProgressEventArgs constructor
  • Loading branch information
Inscientor committed May 23, 2021
2 parents 0d4d5b9 + 34c02b8 commit 52647db
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 38 deletions.
72 changes: 66 additions & 6 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 @@ -151,11 +152,64 @@ 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>
public event EventHandler<FileOverwriteEventArgs> FileExists;

private void OnFileExists(FileOverwriteEventArgs e)
{
if (FileExists != null)
{
FileExists(this, e);
}
}

private void OnOpen(OpenEventArgs e)
{
if (Open != null)
{
Open(this, e);
}
}

private void OnFileExtractionStarted(FileInfoEventArgs e)
{
if (FileExtractionStarted != null)
{
FileExtractionStarted(this, e);
}
}

private void OnFileExtractionFinished(FileInfoEventArgs e)
{
if (FileExtractionFinished != null)
{
FileExtractionFinished(this, e);
}
}

private void OnExtracting(ProgressEventArgs e)
{
if (Extracting != null)
{
Extracting(this, 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 @@ -192,7 +246,14 @@ public void SetTotal(ulong total)
Open?.Invoke(this, 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 @@ -416,7 +477,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 @@ -445,10 +506,9 @@ public void SetOperationResult(OperationResult operationResult)
catch (ObjectDisposedException) { }
_fileStream = null;
}

var iea = new FileInfoEventArgs(_extractor.ArchiveFileData[_currentIndex], PercentDoneEventArgs.ProducePercentDone(_doneRate));
FileExtractionFinished?.Invoke(this, iea);

var iea = new FileInfoEventArgs(
_extractor.ArchiveFileData[_currentIndex], PercentDoneEventArgs.ProducePercentDone(_doneRate));
OnFileExtractionFinished(iea);
if (iea.Cancel)
{
Canceled = true;
Expand Down
84 changes: 56 additions & 28 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,22 +740,21 @@ public void SetOperationResult(OperationResult operationResult)
}
if (_fileStream != null)
{
_fileStream.BytesRead -= IntEventArgsHandler;

_fileStream.BytesRead -= IntEventArgsHandler;
//Specific Zip implementation - can not Dispose files for Zip.
if (_compressor.ArchiveFormat != OutArchiveFormat.Zip)
{
try
{
_fileStream.Dispose();
_fileStream.Dispose();
}
catch (ObjectDisposedException) {}
catch (ObjectDisposedException) { }
}
else
{
_wrappersToDispose.Add(_fileStream);
}

}
_fileStream = null;
}

Expand Down Expand Up @@ -758,7 +786,7 @@ public void Dispose()
{
_fileStream.Dispose();
}
catch (ObjectDisposedException) {}
catch (ObjectDisposedException) { }
}

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

Expand All @@ -780,11 +808,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 @@ -800,7 +828,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

0 comments on commit 52647db

Please sign in to comment.