From 46fc663e90effdf503c1191e3b2c22862e8dd2eb Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Fri, 20 Dec 2013 12:28:17 +0000 Subject: [PATCH] Fixed issue where adding a new entry then removing it wouldn't actually remove it. --- SharpCompress.Test/Zip/ZipArchiveTests.cs | 30 +++++++++++++++++++ SharpCompress/Archive/AbstractArchive.cs | 6 ++-- .../Archive/AbstractWritableArchive.cs | 7 +++-- SharpCompress/Archive/GZip/GZipArchive.cs | 2 +- SharpCompress/Archive/Tar/TarArchive.cs | 2 +- SharpCompress/Archive/Zip/ZipArchive.cs | 2 +- 6 files changed, 40 insertions(+), 9 deletions(-) diff --git a/SharpCompress.Test/Zip/ZipArchiveTests.cs b/SharpCompress.Test/Zip/ZipArchiveTests.cs index 634e9d29..28dd64c6 100644 --- a/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -213,6 +213,36 @@ namespace SharpCompress.Test Directory.Delete(SCRATCH_FILES_PATH, true); } + [TestMethod] + public void Zip_Create_New_Add_Remove() + { + base.ResetScratch(); + foreach (var file in Directory.EnumerateFiles(ORIGINAL_FILES_PATH, "*.*", SearchOption.AllDirectories)) + { + var newFileName = file.Substring(ORIGINAL_FILES_PATH.Length); + if (newFileName.StartsWith(Path.DirectorySeparatorChar.ToString())) + { + newFileName = newFileName.Substring(1); + } + newFileName = Path.Combine(SCRATCH_FILES_PATH, newFileName); + var newDir = Path.GetDirectoryName(newFileName); + if (!Directory.Exists(newDir)) + { + Directory.CreateDirectory(newDir); + } + File.Copy(file, newFileName); + } + string scratchPath = Path.Combine(SCRATCH2_FILES_PATH, "Zip.deflate.noEmptyDirs.zip"); + + using (var archive = ZipArchive.Create()) + { + archive.AddAllFromDirectory(SCRATCH_FILES_PATH); + archive.RemoveEntry(archive.Entries.Single(x => x.FilePath.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))); + Assert.IsFalse(archive.Entries.Any(x => x.FilePath.EndsWith("jpg"))); + } + Directory.Delete(SCRATCH_FILES_PATH, true); + } + [TestMethod] public void Zip_Deflate_WinzipAES_Read() { diff --git a/SharpCompress/Archive/AbstractArchive.cs b/SharpCompress/Archive/AbstractArchive.cs index 7042f829..5441e79e 100644 --- a/SharpCompress/Archive/AbstractArchive.cs +++ b/SharpCompress/Archive/AbstractArchive.cs @@ -39,15 +39,15 @@ namespace SharpCompress.Archive internal AbstractArchive(ArchiveType type, IEnumerable streams, Options options) { - this.Type = type; + Type = type; lazyVolumes = - new LazyReadOnlyCollection(LoadVolumes(streams.Select(CheckStreams), options)); + new LazyReadOnlyCollection(LoadVolumes(streams.Select(CheckStreams), options)); lazyEntries = new LazyReadOnlyCollection(LoadEntries(Volumes)); } internal AbstractArchive(ArchiveType type) { - this.Type = type; + Type = type; lazyVolumes = new LazyReadOnlyCollection(Enumerable.Empty()); lazyEntries = new LazyReadOnlyCollection(Enumerable.Empty()); } diff --git a/SharpCompress/Archive/AbstractWritableArchive.cs b/SharpCompress/Archive/AbstractWritableArchive.cs index d71c00a3..53bc9a75 100644 --- a/SharpCompress/Archive/AbstractWritableArchive.cs +++ b/SharpCompress/Archive/AbstractWritableArchive.cs @@ -22,10 +22,10 @@ namespace SharpCompress.Archive { } - internal AbstractWritableArchive(ArchiveType type, IEnumerable streams, Options options) - : base(type, streams, options) + internal AbstractWritableArchive(ArchiveType type, Stream stream, Options options) + : base(type, stream.AsEnumerable(), options) { - if (streams.Any(x => !x.CanWrite)) + if (!stream.CanWrite) { anyNotWritable = true; } @@ -61,6 +61,7 @@ namespace SharpCompress.Archive private void RebuildModifiedCollection() { hasModifications = true; + newEntries.RemoveAll(v => removedEntries.Contains(v)); modifiedEntries.Clear(); modifiedEntries.AddRange(OldEntries.Concat(newEntries)); } diff --git a/SharpCompress/Archive/GZip/GZipArchive.cs b/SharpCompress/Archive/GZip/GZipArchive.cs index 74f6a34d..eea5b89d 100644 --- a/SharpCompress/Archive/GZip/GZipArchive.cs +++ b/SharpCompress/Archive/GZip/GZipArchive.cs @@ -147,7 +147,7 @@ namespace SharpCompress.Archive.GZip /// /// internal GZipArchive(Stream stream, Options options) - : base(ArchiveType.GZip, stream.AsEnumerable(), options) + : base(ArchiveType.GZip, stream, options) { } diff --git a/SharpCompress/Archive/Tar/TarArchive.cs b/SharpCompress/Archive/Tar/TarArchive.cs index 0cbdbf67..f464c2da 100644 --- a/SharpCompress/Archive/Tar/TarArchive.cs +++ b/SharpCompress/Archive/Tar/TarArchive.cs @@ -133,7 +133,7 @@ namespace SharpCompress.Archive.Tar /// /// internal TarArchive(Stream stream, Options options) - : base(ArchiveType.Tar, stream.AsEnumerable(), options) + : base(ArchiveType.Tar, stream, options) { } diff --git a/SharpCompress/Archive/Zip/ZipArchive.cs b/SharpCompress/Archive/Zip/ZipArchive.cs index 38a7d9eb..4f5bece8 100644 --- a/SharpCompress/Archive/Zip/ZipArchive.cs +++ b/SharpCompress/Archive/Zip/ZipArchive.cs @@ -164,7 +164,7 @@ namespace SharpCompress.Archive.Zip /// /// internal ZipArchive(Stream stream, Options options, string password = null) - : base(ArchiveType.Zip, stream.AsEnumerable(), options) + : base(ArchiveType.Zip, stream, options) { headerFactory = new SeekableZipHeaderFactory(password); }