【发布时间】:2016-12-14 00:44:32
【问题描述】:
我正在尝试在 F# 项目中实例化 System.IO.Compression 中的 ZipArchive 类:
open System.IO
open System.IO.Compression
open System.IO.Compression.FileSystem //this line errors, as expected
// Define your library scripting code here
let directoryPath = @"C:\Users\max\Downloads"
let dirInfo = new DirectoryInfo(directoryPath)
let zippedFiles = dirInfo.GetFiles()
|> Array.filter (fun x -> x.Extension.Equals(".zip"))
let fileStream = new FileStream(((Array.head zippedFiles).FullName), System.IO.FileMode.Open)
//this line does not compile, because ZipArchive is not defined
let archive = new System.IO.Compression.ZipArchive(fileStream)
我可以在 C# 中的正确命名空间中创建相同的东西:
var unzipper = new System.IO.Compression.ZipArchive(null);
(这会产生一堆错误,因为我传递的是 null,但至少我可以尝试访问它)。
我确实在我的 F# 项目中有 System.IO.Compression.FileSystem 引用(以及父命名空间 System.IO.Compression。但是,在加载 .FileSystem 命名空间时,我收到一条错误消息“命名空间 'FileSystem' 未定义”。
编辑
添加了我正在尝试执行的完整脚本文件以重现问题。
如open 语句所示,我的项目引用了这两个库:
System.IO.Compression
System.IO.Compression.FileSystem
我正在运行:
- F# 4.4
- .NET 4.6.1
- Visual Studio 2015
- Windows 10 64 位
编辑 2:修复!
我在 F# 脚本文件 .fsx 中执行所有这些操作,这需要告诉交互式环境像这样加载 DLL:
#if INTERACTIVE
#r "System.IO.Compression.dll"
#r "System.IO.Compression.FileSystem.dll"
#endif
【问题讨论】:
-
虽然您需要同时引用 Compression 和 Compression.Filesystem,但您通常只需
open System.IO.Compression。然后你可以说new ZipArchive(x))并进一步操作它。 -
请描述您遇到的问题。编译失败了吗?如果是这样,错误信息是什么?它在运行时会失败吗?如果有,有哪些例外?
-
@MarkSeemann 我已经用更多细节更新了我的问题。
标签: f# zip compression unzip