粗略的谷歌搜索没有返回任何简单易懂的东西(我对函数式编程很新).
如果我有一个文件数组,我如何压缩每个文件,然后创建所有压缩文件的zip?
到目前为止我有这样的事情:
let zip f =
f.zip //this is where I need the most direction
let zipAllAttachments f =
f
|> Seq.map zip //do I need to create another function to create a single zip of all zips?
Run Code Online (Sandbox Code Playgroud)
编辑:这是我到目前为止,但我有一些奇怪的行为.一旦我弄清楚奇怪的行为究竟是什么,我将会发生更多事情:
use zipfile = new ZipFile()
for fileObj in files do
zipfile.AddFile(sprintf "%s%s" path fileObj.Filename) |> ignore
zipfile.Save("C:\\temp\\Compliance.zip")
Run Code Online (Sandbox Code Playgroud)
更新:我不认为"奇怪的行为"与zip模块有关.我感谢所有的帮助!
您是否尝试创建zip压缩的独立实现?
我将使用http://dotnetzip.codeplex.com/中的 DotNetZip - 它是一个单一的托管代码(C#)程序集.从F#中使用它应该与引用项目中的程序集一样简单.
用法很简单.对于C#:
using (ZipFile zip = new ZipFile())
{
// add this map file into the "images" directory in the zip archive
zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
// add the report into a different directory in the archive
zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
zip.AddFile("ReadMe.txt");
zip.Save("MyZipFile.zip");
}
Run Code Online (Sandbox Code Playgroud)
如果你想压缩一组zip文件(为什么?),有很多方法可以使用DotNetZip(例如,你可以将zip文件保存到流中,或者将流添加到zip文件中).
希望这可以帮助!
编辑注: DotNetZip曾经住在Codeplex.Codeplex已关闭.旧档案仍然[可在Codeplex] [1]获得.看起来代码已迁移到Github: