1
votes

I'm generating some .csv files and I need to compress this inside a Zip File. Ok, I have a framework to do this and probably everything will be fine.

But! As TDD says I just can write code, after I have some tests!

My first test sound simple, but I'm having some problems reading the Zip file, anyone know a simple way to count the number of files in my zip file?

3
You say you have a framework and I assume this means a zip library that allows you to create zip files. Does this not read zip files too? If so then you should be able to load the zip file up and then either get a count directly or possibly have to traverse the virtual directory structure to get a count (if there is one). It would depend on the implementation of your library. If you haven't got a library then others below have named some for you. :)Chris
Nice Chris! My library is a bit of legacy code, but initially I'll keep using her in main project. But in my test project I want to confirm that my code does exactly what I want. I'm using the DotNetZip, soon I'll post my code.Custodio

3 Answers

6
votes

You seem to be looking for something like DotNetZip.

EDIT: For example:

int count;
using (ZipFile zip = ZipFile.Read(path))
    count = zip.Count;
1
votes

I don't think that was a TDD question you asked but I'll answer it anyway.

[Test]
public void countsNumberOfFilesInZip() {
  var counter = new FileCounter("existing_archive_with_2_files.zip");
  AssertEqual(2, counter.count());
}

Now, using the library of your choice, make FileCounter work. Once it works and you have a passing test, if you so choose, refactor the code so that it uses a mocking framework to mock out the calls to the zip library. Now you don't have a dependency on the file system. (I probably wouldn't go this far unless your tests are slowed down too much by the disk I/O)