简体   繁体   English

使用C#计算Zip文件中的文件数

[英]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. 我正在生成一些.csv文件,因此需要在Zip文件中对其进行压缩。 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! 正如TDD所说,经过一些测试,我就可以编写代码!

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? 我的第一个测试听起来很简单,但是在读取Zip文件时遇到了一些问题,有人知道一种简单的方法来计算zip文件中文件的数量吗?

You seem to be looking for something like DotNetZip . 您似乎正在寻找类似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. 我不认为这是您提出的TDD问题,但我还是会回答。

[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. 现在,使用您选择的库,使FileCounter工作。 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. 一旦它起作用并且您通过了测试,则可以选择重构代码,以便它使用模拟框架模拟对zip库的调用。 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) (除非磁盘I / O大大降低了测试速度,否则我可能不会走得太远)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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