问题
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?
回答1:
You seem to be looking for something like DotNetZip.
EDIT: For example:
int count;
using (ZipFile zip = ZipFile.Read(path))
count = zip.Count;
回答2:
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)
回答3:
Or use SharpZipLib and see this for some examples
来源:https://stackoverflow.com/questions/4785391/count-number-of-files-in-a-zip-file-with-c-sharp