Count number of files in a Zip File with c#

纵然是瞬间 提交于 2019-12-10 12:20:53

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!