if (_mode == ZipArchiveMode.Read)
throw new NotSupportedException(SR.CreateInReadMode);
+ if (_entriesDictionary.ContainsKey(entryName))
+ {
+ throw new InvalidOperationException(string.Format(SR.EntryNameAlreadyExists, entryName));
+ }
+
ThrowIfDisposed();
private void AddEntry(ZipArchiveEntry entry)
{
_entries.Add(entry);
-
- string entryName = entry.FullName;
- if (!_entriesDictionary.ContainsKey(entryName))
- {
- _entriesDictionary.Add(entryName, entry);
- }
+ _entriesDictionary.TryAdd(entry.FullName, entry);
}
[Conditional("DEBUG")]
AssertDataDescriptor(memoryStream, false);
}
+ [Fact]
+ public static void CreateNormal_With2SameEntries_ThrowException()
+ {
+ using var memoryStream = new MemoryStream();
+ // We need an non-seekable stream so the data descriptor bit is turned on when saving
+ var wrappedStream = new WrappedStream(memoryStream);
+
+ // Creation will go through the path that sets the data descriptor bit when the stream is unseekable
+ using (var archive = new ZipArchive(wrappedStream, ZipArchiveMode.Create))
+ {
+ string entryName = "duplicate.txt";
+ CreateEntry(archive, entryName, "xxx");
+ AssertExtensions.ThrowsContains<InvalidOperationException>(() => CreateEntry(archive, entryName, "yyy"),
+ entryName);
+ }
+ }
+
private static string ReadStringFromSpan(Span<byte> input)
{
return Text.Encoding.UTF8.GetString(input.ToArray());
baseline = baseline.Clone();
using (ZipArchive archive = new ZipArchive(baseline, mode))
{
- AddEntry(archive, "data1.txt", data1, lastWrite);
+ if (mode == ZipArchiveMode.Create)
+ {
+ AddEntry(archive, "data1.txt", data1, lastWrite);
- ZipArchiveEntry e = archive.CreateEntry("empty.txt");
- e.LastWriteTime = lastWrite;
- using (Stream s = e.Open()) { }
+ ZipArchiveEntry e = archive.CreateEntry("empty.txt");
+ e.LastWriteTime = lastWrite;
+ using (Stream s = e.Open()) { }
+ }
+ else
+ {
+ Assert.Throws<InvalidOperationException>(() => AddEntry(archive, "data1.txt", data1, lastWrite));
+
+ Assert.Throws<InvalidOperationException>(() => archive.CreateEntry("empty.txt"));
+ }
}
test = test.Clone();
using (ZipArchive archive = new ZipArchive(test, mode))
{
- AddEntry(archive, "data1.txt", data1, lastWrite);
+ if (mode == ZipArchiveMode.Create)
+ {
+ AddEntry(archive, "data1.txt", data1, lastWrite);
- ZipArchiveEntry e = archive.CreateEntry("empty.txt");
- e.LastWriteTime = lastWrite;
+ ZipArchiveEntry e = archive.CreateEntry("empty.txt");
+ e.LastWriteTime = lastWrite;
+ }
+ else
+ {
+ Assert.Throws<InvalidOperationException>(() => AddEntry(archive, "data1.txt", data1, lastWrite));
+
+ Assert.Throws<InvalidOperationException>(() => archive.CreateEntry("empty.txt"));
+ }
}
//compare
Assert.True(ArraysEqual(baseline.ToArray(), test.ToArray()), "Arrays didn't match after update");