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

Create and using PauseEntryRebuilding for adding large numbers of ent… #485

Merged
merged 3 commits into from Jan 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions src/SharpCompress/Archives/AbstractWritableArchive.cs
Expand Up @@ -12,11 +12,28 @@ public abstract class AbstractWritableArchive<TEntry, TVolume> : AbstractArchive
where TEntry : IArchiveEntry
where TVolume : IVolume
{
private class RebuildPauseDisposable: IDisposable
{
private readonly AbstractWritableArchive<TEntry, TVolume> archive;

public RebuildPauseDisposable(AbstractWritableArchive<TEntry, TVolume> archive)
{
this.archive = archive;
archive.pauseRebuilding = true;
}

public void Dispose()
{
archive.pauseRebuilding = false;
adamhathcock marked this conversation as resolved.
Show resolved Hide resolved
archive.RebuildModifiedCollection();
}
}
private readonly List<TEntry> newEntries = new List<TEntry>();
private readonly List<TEntry> removedEntries = new List<TEntry>();

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

internal AbstractWritableArchive(ArchiveType type)
: base(type)
Expand Down Expand Up @@ -47,8 +64,17 @@ public override ICollection<TEntry> Entries
}
}

public IDisposable PauseEntryRebuilding()
{
return new RebuildPauseDisposable(this);
}

private void RebuildModifiedCollection()
{
if (pauseRebuilding)
{
return;
}
hasModifications = true;
newEntries.RemoveAll(v => removedEntries.Contains(v));
modifiedEntries.Clear();
Expand Down
6 changes: 6 additions & 0 deletions src/SharpCompress/Archives/IWritableArchive.cs
Expand Up @@ -11,5 +11,11 @@ public interface IWritableArchive : IArchive
IArchiveEntry AddEntry(string key, Stream source, bool closeStream, long size = 0, DateTime? modified = null);

void SaveTo(Stream stream, WriterOptions options);

/// <summary>
/// Use this to pause entry rebuilding when adding large collections of entries. Dispose when complete. A using statement is recommended.
/// </summary>
/// <returns>IDisposeable to resume entry rebuilding</returns>
IDisposable PauseEntryRebuilding();
}
}
18 changes: 12 additions & 6 deletions src/SharpCompress/Archives/IWritableArchiveExtensions.cs
Expand Up @@ -39,15 +39,21 @@ public static void SaveTo(this IWritableArchive writableArchive, FileInfo fileIn
this IWritableArchive writableArchive,
string filePath, string searchPattern = "*.*", SearchOption searchOption = SearchOption.AllDirectories)
{
using (writableArchive.PauseEntryRebuilding())
{
#if NET35
foreach (var path in Directory.GetFiles(filePath, searchPattern, searchOption))
foreach (var path in Directory.GetFiles(filePath, searchPattern, searchOption))
#else
foreach (var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption))
foreach (var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption))
#endif
{
var fileInfo = new FileInfo(path);
writableArchive.AddEntry(path.Substring(filePath.Length), fileInfo.OpenRead(), true, fileInfo.Length,
fileInfo.LastWriteTime);
{
var fileInfo = new FileInfo(path);
writableArchive.AddEntry(path.Substring(filePath.Length),
fileInfo.OpenRead(),
true,
fileInfo.Length,
fileInfo.LastWriteTime);
}
adamhathcock marked this conversation as resolved.
Show resolved Hide resolved
}
}
public static IArchiveEntry AddEntry(this IWritableArchive writableArchive, string key, FileInfo fileInfo)
Expand Down