Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pause building of modified collection. Fixes #256 #484

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/SharpCompress/Archives/AbstractWritableArchive.cs
Expand Up @@ -17,6 +17,7 @@ public abstract class AbstractWritableArchive<TEntry, TVolume> : AbstractArchive

private readonly List<TEntry> modifiedEntries = new List<TEntry>();
private bool hasModifications;
private bool shouldRebuildModifiedCollection = true;

internal AbstractWritableArchive(ArchiveType type)
: base(type)
Expand Down Expand Up @@ -96,10 +97,22 @@ IArchiveEntry IWritableArchive.AddEntry(string key, Stream source, bool closeStr
}
var entry = CreateEntry(key, source, size, modified, closeStream);
newEntries.Add(entry);
RebuildModifiedCollection();
if(shouldRebuildModifiedCollection)
RebuildModifiedCollection();
return entry;
}

public void PauseInternalEntryUpdates()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd make a IDisposable object that would flip the boolean back to true on Dispose so that way a using statement can be used in the implementation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that. Such a pause-object would need to know the instance of the AbstractWritableArchive in order to flip the boolean and to call RebuildModifiedCollection on that instance when the pause-object is disposed.
This requires the user to use the using statement - if the user don't and the user doesn't dispose the pause-object himself, then it will be disposed some time later where the AbstractWritableArchive might have been disposed. It is difficult to get right.
Therefore I opted for the pattern used in WinForms where SuspendLayout/ResumeLayout is commonly used for performance reasons when larger updates of lists or tree structures are updated.

So I don't see how to do this in a nice way (but I am happy to be educated on the issue ;-) if you can show me how)

{
shouldRebuildModifiedCollection = false;
}

public void ResumeInternalEntryUpdates()
{
shouldRebuildModifiedCollection = true;
RebuildModifiedCollection();
}

private bool DoesKeyMatchExisting(string key)
{
foreach (var path in Entries.Select(x => x.Key))
Expand Down
4 changes: 4 additions & 0 deletions src/SharpCompress/Archives/IWritableArchive.cs
Expand Up @@ -11,5 +11,9 @@ public interface IWritableArchive : IArchive
IArchiveEntry AddEntry(string key, Stream source, bool closeStream, long size = 0, DateTime? modified = null);

void SaveTo(Stream stream, WriterOptions options);

void PauseInternalEntryUpdates();

void ResumeInternalEntryUpdates();
}
}
3 changes: 3 additions & 0 deletions src/SharpCompress/Archives/IWritableArchiveExtensions.cs
Expand Up @@ -39,6 +39,7 @@ public static void SaveTo(this IWritableArchive writableArchive, FileInfo fileIn
this IWritableArchive writableArchive,
string filePath, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
{
writableArchive.PauseInternalEntryUpdates();
#if NET35
foreach (var path in Directory.GetFiles(filePath, searchPattern, searchOption))
#else
Expand All @@ -49,7 +50,9 @@ public static void SaveTo(this IWritableArchive writableArchive, FileInfo fileIn
writableArchive.AddEntry(path.Substring(filePath.Length), fileInfo.OpenRead(), true, fileInfo.Length,
fileInfo.LastWriteTime);
}
writableArchive.ResumeInternalEntryUpdates();
}

public static IArchiveEntry AddEntry(this IWritableArchive writableArchive, string key, FileInfo fileInfo)
{
if (!fileInfo.Exists)
Expand Down