From 364d0d724d44eef8049ccd8e715b3f94b8edabf9 Mon Sep 17 00:00:00 2001 From: Michael Lund Date: Thu, 3 Oct 2019 14:08:32 +0200 Subject: [PATCH] Pause building of modified collection --- .../Archives/AbstractWritableArchive.cs | 15 ++++++++++++++- src/SharpCompress/Archives/IWritableArchive.cs | 4 ++++ .../Archives/IWritableArchiveExtensions.cs | 3 +++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/SharpCompress/Archives/AbstractWritableArchive.cs b/src/SharpCompress/Archives/AbstractWritableArchive.cs index 5f38d459e..3b24f299c 100644 --- a/src/SharpCompress/Archives/AbstractWritableArchive.cs +++ b/src/SharpCompress/Archives/AbstractWritableArchive.cs @@ -17,6 +17,7 @@ public abstract class AbstractWritableArchive : AbstractArchive private readonly List modifiedEntries = new List(); private bool hasModifications; + private bool shouldRebuildModifiedCollection = true; internal AbstractWritableArchive(ArchiveType type) : base(type) @@ -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() + { + shouldRebuildModifiedCollection = false; + } + + public void ResumeInternalEntryUpdates() + { + shouldRebuildModifiedCollection = true; + RebuildModifiedCollection(); + } + private bool DoesKeyMatchExisting(string key) { foreach (var path in Entries.Select(x => x.Key)) diff --git a/src/SharpCompress/Archives/IWritableArchive.cs b/src/SharpCompress/Archives/IWritableArchive.cs index 380d68148..dc127a9fe 100644 --- a/src/SharpCompress/Archives/IWritableArchive.cs +++ b/src/SharpCompress/Archives/IWritableArchive.cs @@ -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(); } } \ No newline at end of file diff --git a/src/SharpCompress/Archives/IWritableArchiveExtensions.cs b/src/SharpCompress/Archives/IWritableArchiveExtensions.cs index bee42a499..7ea341025 100644 --- a/src/SharpCompress/Archives/IWritableArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IWritableArchiveExtensions.cs @@ -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 @@ -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)