Mock System.IO.Compression.ZipFile

See original GitHub issue

Hello guys,

For one of my project I started to write a basic wrapper around the methods of System.IO.Compression.ZipFile. To achieve that I’m of course using System.IO.Abstractions.

I started to introduce 4 new types IZipFileFactory, ZipFileFactory, MockZipFileFactory, MockZipFileData. Now I would like to have a property ZipFile of type IZipFileFactory in IFileSystem so I can write something like

var fileSystem = new FileSystem();
fileSystem.ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName);
...
fileSystem.ZipFile.ExtractToDirectory(destinationArchiveFileName, destinationDirectoryName);

Would you like to see a pull request for that feature?

Thanks

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
herebebeastiescommented, Sep 13, 2022

We did something like this at the company I work for (will hopefully open source some of this stuff soon).

It’s actually quite nice to be able to take a ZipArchive on one IFileSystem and extract it to another (e.g. take a real ZIP on disk and populate a MockFileSystem with it), so we implemented this as extension methods on FileBase and ZipArchive:

public static ZipArchive CreateZip(this FileBase file, string path);
public static void ExtractToDirectory(this ZipArchive zipArchive, IFileSystem destFs, string destPath);

etc. Note that ZipArchive already works against the Stream APIs instead of files directly, so this is all pretty easy.

The advantage of that is that it works against any IFileSystem implementation (and across them), and that you don’t need to implement this stuff in the IFileSystem implementations themselves.

1reaction
herebebeastiescommented, Sep 13, 2022

Or that, with a destination IFileSystem also passed in. Sorry, that was missing in my original, have updated. Anyway, lots of ways to skin this particular cat!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mock System.IO.Compression.ZipFile · Issue #396
Hello guys, For one of my project I started to write a basic wrapper around the methods of System.IO.Compression.ZipFile.
Read more >
c# - Unit Testing using Shims for ZipFile
There isn't a way to mock a static class like ZipFile using mock, ... Note: We need to Fake System.IO.Compression.FileSystem (i.e. the dll) ......
Read more >
How to Mock the File System for Unit Testing in .NET
In this article, we are going to learn how to mock the file system and unit test a component that interacts with the...
Read more >
ZipFile.CreateFromDirectory Method
Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level and character encoding for ......
Read more >
Mocking System.IO filesystem in unit tests in ASP.NET Core
Now when it comes to unit testing, it is not possible to mock these classes and methods for the simple reason they are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found