简体   繁体   中英

Count number of files in a Zip File with c#

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?

You seem to be looking for something like DotNetZip .

EDIT : For example:

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM