Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Added the ability to cancel the compression operation #166

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
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
1 change: 1 addition & 0 deletions SevenZip.Tests/SevenZip.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>SevenZipTests.snk</AssemblyOriginatorKeyFile>
<Configurations>Debug;Release;LiteDebug;LiteRelease</Configurations>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DefineConstants>SFX</DefineConstants>
Expand Down
24 changes: 22 additions & 2 deletions SevenZip.Tests/SevenZipCompressorAsynchronousTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
namespace SevenZip.Tests
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -56,7 +58,7 @@ public void AsynchronousCompressFilesTest()
{
var compressionFinishedInvoked = false;

var compressor = new SevenZipCompressor {DirectoryStructure = false};
var compressor = new SevenZipCompressor { DirectoryStructure = false };
compressor.CompressionFinished += (o, e) => compressionFinishedInvoked = true;

compressor.BeginCompressFiles(TemporaryFile, @"TestData\zip.zip", @"TestData\tar.tar");
Expand Down Expand Up @@ -129,7 +131,7 @@ public void AsynchronousModifyArchiveTest()
var compressionFinishedInvoked = false;
compressor.CompressionFinished += (o, e) => compressionFinishedInvoked = true;

compressor.BeginModifyArchive(TemporaryFile, new Dictionary<int, string>{{0, @"tartar"}});
compressor.BeginModifyArchive(TemporaryFile, new Dictionary<int, string> { { 0, @"tartar" } });

var timeToWait = 1000;
while (!compressionFinishedInvoked)
Expand Down Expand Up @@ -202,6 +204,24 @@ public async Task CompressFilesAsync()
}
}

[Test]
public void CompressFilesAsyncWithCancellationTest()
{
var compressor = new SevenZipCompressor { DirectoryStructure = false };
compressor.Compressing += Compressor_Compressing;
var task = compressor.CompressFilesAsync(TemporaryFile, @"TestData\zip.zip", @"TestData\tar.tar");

Assert.ThrowsAsync<SevenZipCompressionCanceledException>(async () => { await task; });
}

private void Compressor_Compressing(object sender, ProgressEventArgs e)
{
if (e.PercentDone > 50)
{
e.Cancel = true;
}
}

[Test]
public async Task CompressDirectoryAsync()
{
Expand Down
8 changes: 7 additions & 1 deletion SevenZip/ArchiveUpdateCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ private void OnCompressing(ProgressEventArgs e)
if (Compressing != null)
{
Compressing(this, e);
Canceled = e.Cancel;
}
}

Expand All @@ -342,7 +343,12 @@ private void OnFileCompressionFinished(EventArgs e)

public void SetTotal(ulong total) {}

public void SetCompleted(ref ulong completeValue) {}
public void SetCompleted(ref ulong completeValue) {
if (Canceled)
{
throw new SevenZipCompressionCanceledException();
}
}

public int GetUpdateItemInfo(uint index, ref int newData, ref int newProperties, ref uint indexInArchive)
{
Expand Down
4 changes: 4 additions & 0 deletions SevenZip/EventArguments/ProgressEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ public ProgressEventArgs(byte percentDone, byte percentDelta)
/// Gets the change in done work percentage.
/// </summary>
public byte PercentDelta => _delta;
/// <summary>
/// Allows you to cancel the compression operation.
/// </summary>
public bool Cancel { get; set; } = false;
}
}
17 changes: 17 additions & 0 deletions SevenZip/Exceptions/SevenZipCompressionCanceledException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace SevenZip
{
/// <summary>
/// Exception thrown when compression is canceled.
/// </summary>
public class SevenZipCompressionCanceledException: Exception
{
/// <summary>
/// Message of exception.
/// </summary>
public override string Message => "Canceled";
}
}
14 changes: 13 additions & 1 deletion SevenZip/SevenZipBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,14 @@ internal void ThrowUserException()
{
if (HasExceptions)
{
throw new SevenZipException(SevenZipException.USER_EXCEPTION_MESSAGE);
if ((_exceptions.Count == 1) && (_exceptions[0] is SevenZipCompressionCanceledException))
{
throw new SevenZipCompressionCanceledException();
}
else
{
throw new SevenZipException(SevenZipException.USER_EXCEPTION_MESSAGE);
}
}
}

Expand All @@ -178,6 +185,11 @@ internal void CheckedExecute(int hresult, string message, CallbackBase handler)
{
if (hresult != (int)OperationResult.Ok || handler.HasExceptions)
{
if ((handler is ArchiveUpdateCallback) && (handler.Canceled) && (!handler.HasExceptions))
{
AddException(new SevenZipCompressionCanceledException());
return;
}
if (!handler.HasExceptions)
{
if (hresult < -2000000000)
Expand Down