Unable to create ZipArchive in F #

I am trying to instantiate a class ZipArchivein System.IO.Compressionin an F # project:

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)

I can create the same in C # in the correct namespace:

var unzipper = new System.IO.Compression.ZipArchive(null);

(This gives a bunch of errors because I am missing zero, but at least I can try to access it).

I do have a link System.IO.Compression.FileSystemin my F # project (as well as the parent namespace System.IO.Compression). However, when I load the namespace, .FileSystemI get the error "The namespace" FileSystem "is not defined."

EDIT

Added a full script file that I am trying to execute that reproduces the problem.

As shown in the instructions open, my project references both of these libraries:

System.IO.Compression System.IO.Compression.FileSystem

I run:

  • F # 4.4
  • .NET 4.6.1
  • Visual Studio 2015
  • Windows 10 64

2: !

F # script, .fsx, , DLL :

#if INTERACTIVE
#r "System.IO.Compression.dll"
#r "System.IO.Compression.FileSystem.dll"
#endif
+4
2

System.IO.Compression zip .NET 4.5. . .

FileStream ZipArchive, . ExtractToDirectory . FileStream, ZipArchive, , , CreateEntryFromFile. use , writeZipFile.

zip :

#if INTERACTIVE
#r "System.IO.Compression.dll"
#r "System.IO.Compression.FileSystem.dll"
#endif

open System.IO
open System.IO.Compression

let zipfile = @"c:\tmp\test.zip"
File.Exists zipfile //true

let readZipFile (x:string) = 
    let x = new FileStream(x,FileMode.Open,FileAccess.Read)
    new ZipArchive(x)

let z = readZipFile zipfile
z.ExtractToDirectory(@"c:\tmp\test")
File.Exists @"c:\tmp\test\test.txt" // true

let writeZipFile (x:string) =
    use newFile = new FileStream(@"c:\tmp\newzip.zip",FileMode.Create)
    use newZip =  new ZipArchive(newFile,ZipArchiveMode.Create)
    newZip.CreateEntryFromFile(@"c:\tmp\test.txt","test.txt")

writeZipFile @"c:\tmp\test.txt"
File.Exists @"c:\tmp\newzip.zip"  // true
+3

, System.IO.Compression.dll.

GZipStream System.IO.Compression, System.dll.

ZipArchive System.IO.Compression, System.IO.Compression.dll, , , ​​ F # .

+2

Source: https://habr.com/ru/post/1663693/


All Articles