Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 276939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:59:45+00:00 2026-05-12T00:59:45+00:00

I have the following code for adding to/extracting from Zip. I’m trying to refactor

  • 0

I have the following code for adding to/extracting from Zip. I’m trying to refactor this to make it test-ready. Can someone provide pointers on how I can accomplish this?
Aside: I’m using Moq as my mock framework and MSTest as my Unit Testing tool

private const long BufferSize = 4096;

public static void ExtractZip(string zipFilename, string folder) {
  using (var zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)) {
    foreach (var part in zip.GetParts()) {
      using (var reader = new StreamReader(part.GetStream(FileMode.Open, FileAccess.Read))) {
        using (var writer = new FileStream(folder + "\\" + Path.GetFileName(part.Uri.OriginalString),
                                           FileMode.Create, FileAccess.Write)) {
          var buffer = System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd());
          writer.Write(buffer, 0, buffer.Length);
        }
      }
    }
  }
}

public static void AddFileToZip(string zipFilename, string fileToAdd) {
  using (var zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)) {
    var destFilename = ".\\" + Path.GetFileName(fileToAdd);
    var uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative));
    if (zip.PartExists(uri)) {
      zip.DeletePart(uri);
    }
    var part = zip.CreatePart(uri, "", CompressionOption.Normal);
    using (var fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read)) {
      using (var dest = part.GetStream()) {
        CopyStream(fileStream, dest);
      }
    }
  }
}

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:59:45+00:00Added an answer on May 12, 2026 at 12:59 am

    I would make these two static methods (ExtractZip and AddFileToZip) instance methods, and put it into an interface:

    public interface IZipper
    {
      void ExtractZip(string zipFilename, string folder);
      void AddFileToZip(string zipFilename, string fileToAdd);
    }
    
    public class Zipper : IZipper
    {
      public void ExtractZip(string zipFilename, string folder)
      { 
        //...
      }
    
      void AddFileToZip(string zipFilename, string fileToAdd)
      {
        //...
      }
    }
    
    
    // client code
    class Foo
    {
      private IZipper myZipper;
      // gets an instance of the zipper (injection), but implements only 
      // against the interface. Allows mocks on the IZipper interface.
      public Foo(IZipper zipper)
      {
        myZipper = zipper;
      }
    
    }
    

    Client code is now easy to test.

    What about the Zipper class?

    • Consider if it is worth to test anyway.
    • In our project, we distinguish between unit tests (isolated) and integration tests, where it is possible to use the database or the file system. You could declare it as an “file system integration test”. Of course, only the test target folder is allowed to be used. This shouldn’t make any problems.
    • Consider to move the file operations out, and only work with streams. Then you can easily test the zipping on memory streams. The file operations still need to be somewhere and aren’t tested.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 133k
  • Answers 133k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I have used both and valgrind is the clear winner… May 12, 2026 at 6:39 am
  • Editorial Team
    Editorial Team added an answer Emails is one of those places where whatever notion you… May 12, 2026 at 6:39 am
  • Editorial Team
    Editorial Team added an answer Translating the syntactical elements of one language and producing another… May 12, 2026 at 6:39 am

Related Questions

I have the following code for adding to/extracting from Zip. I'm trying to refactor
I'm not familiar with ADO.NET Data Services but it looks usable. All I need
I have the following code, which will retrieve a filename from a table and
I'm trying to create a dynamic popup window that is able to be re-sized.
We are planning on adding multi-language support for our ASP.NET Website. I understand the

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.