Skip to content

Commit 8a96799

Browse files
authored
fix: zipped file remains locked after ZipFile().CreateFromDirectory (#766)
1 parent c8d2c32 commit 8a96799

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

Source/Testably.Abstractions.Compression/Internal/ZipUtilities.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ internal static void CreateFromDirectory(IFileSystem fileSystem,
9696
? archive.CreateEntry(entryName, compressionLevel.Value)
9797
: archive.CreateEntry(entryName);
9898
using Stream stream = entry.Open();
99-
fileInfo.OpenRead().CopyTo(stream);
99+
using FileSystemStream fileStream = fileInfo.OpenRead();
100+
fileStream.CopyTo(stream);
100101
}
101102
else if (file is IDirectoryInfo directoryInfo &&
102103
directoryInfo.GetFileSystemInfos().Length == 0)
@@ -172,7 +173,8 @@ internal static void CreateFromDirectory(
172173
? archive.CreateEntry(entryName, compressionLevel.Value)
173174
: archive.CreateEntry(entryName);
174175
using Stream stream = entry.Open();
175-
fileInfo.OpenRead().CopyTo(stream);
176+
using FileSystemStream fileStream = fileInfo.OpenRead();
177+
fileStream.CopyTo(stream);
176178
}
177179
else if (file is IDirectoryInfo directoryInfo &&
178180
directoryInfo.GetFileSystemInfos().Length == 0)

Tests/Testably.Abstractions.Compression.Tests/ZipFile/CreateFromDirectoryTests.cs

+15
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,21 @@ await That(FileSystem).HasFile("destination/bar/test.txt")
374374
}
375375
#endif
376376

377+
[Fact]
378+
public async Task ShouldNotLock()
379+
{
380+
string directory = "ToBeZipped";
381+
string archive = "zippedDirectory.zip";
382+
FileSystem.Directory.CreateDirectory(directory);
383+
FileSystem.File.WriteAllText(FileSystem.Path.Combine(directory, "file.txt"),
384+
"Some content");
385+
void Act() => FileSystem.Directory.Delete(directory, true);
386+
387+
FileSystem.ZipFile().CreateFromDirectory(directory, archive);
388+
389+
await That(Act).DoesNotThrow();
390+
}
391+
377392
#region Helpers
378393

379394
#pragma warning disable MA0018

0 commit comments

Comments
 (0)