如何在 FSharp 中压缩文件?
粗略的谷歌搜索没有返回任何简单到可以理解的内容(我对函数式编程还很陌生)。
如果我有一个文件数组,我如何压缩每个文件,然后创建所有压缩文件的 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?
编辑:这是我到目前为止所拥有的,但我遇到了一些奇怪的行为。一旦我弄清楚奇怪的行为到底是什么:
use zipfile = new ZipFile()
for fileObj in files do
zipfile.AddFile(sprintf "%s%s" path fileObj.Filename) |> ignore
zipfile.Save("C:\\temp\\Compliance.zip")
更新:我不认为“奇怪的行为”与 zip 模块有关。我感谢所有的帮助!
Cursory Google search didn't return anything simple enough to understand (i'm pretty new to functional programming).
if I have an array of files, how can i zip each file and then create a zip of all zipped files?
I have something like this so far:
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?
EDIT: this is what I have so far, but I'm getting some strange behavior. More to come once I figure out what the strange behavior IS exactly:
use zipfile = new ZipFile()
for fileObj in files do
zipfile.AddFile(sprintf "%s%s" path fileObj.Filename) |> ignore
zipfile.Save("C:\\temp\\Compliance.zip")
UPDATE: I don't think the "strange behavior" is related to the zip module. I appreciate all the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否正在尝试创建 zip 压缩的独立实现?
我会使用 http://dotnetzip.codeplex.com/ 中的 DotNetZip ——它是一个单一的、托管的代码 (C#) 汇编。从 F# 使用它应该与从项目中引用程序集一样简单。
使用方法很简单。对于 C#:
如果您想要压缩 zip 文件的集合(为什么?),可以通过多种方法使用 DotNetZip 来实现这一点(例如,您可以将 zip 文件保存到流中,或者将流添加到 zip 文件中)文件)。
希望这有帮助!
编辑注意: DotNetZip 曾经住在 Codeplex。 Codeplex 已关闭。旧存档仍然[可在 Codeplex 上找到][1]。代码似乎已迁移到 Github:
Are you trying to create an independent implementation of zip compression?
I'd use DotNetZip from http://dotnetzip.codeplex.com/ -- it's a single, managed code (C#) assembly. Using it from F# should be pretty much as simple as referencing the assembly from your project.
Usage is simple. For C#:
If you want to zip a collection of zip files (why?), there's a number of ways to do that with DotNetZip (you can, for instance, save your zip file to a stream, or add a stream to a zip file).
Hope this helps!
Edited To Note: DotNetZip used to live at Codeplex. Codeplex has been shut down. The old archive is still [available at Codeplex][1]. It looks like the code has migrated to Github:
我没有使用 Nicholas 提到的 zip 库,但这可能是您想要的 F# 版本。
如果 zipfile_name 过载,您可能还需要将类型信息添加到其中。
此函数可用于创建各个文件的 zip 文件,然后用于创建包含所有较小 zip 文件的大 zip 文件。这是一个示例,尽管您实际上并不希望像它那样在各处重复文件名。
I haven't used the zip library that Nicholas mentioned, but this may be the F# version of what you want.
You may need to add type information to zipfile_name too, if it is overloaded.
This function could be used to create the zip files of the individual files, and then used to create a big zip file containing all of the smaller zip files. Here is an example, although you wouldn't actually want to duplicate the file names all over the place like it does.
从 .NET 4.6.2 开始,有 ZipArchive 类可用:
As of .NET 4.6.2, there is ZipArchive class available: