开发者

How to zip files in FSharp?

开发者 https://www.devze.com 2023-02-01 18:31 出处:网络
Cursory Google search didn\'t return anything simple enough to understand (i\'m pretty new to functional programming).

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. M开发者_JAVA百科ore 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!


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#:

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");
}

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:

  • https://github.com/DinoChiesa/DotNetZip. Looks to be the original author's repo.
  • https://github.com/haf/DotNetZip.Semverd. This looks to be the currently maintained version. It's also packaged up an available via Nuget at https://www.nuget.org/packages/DotNetZip/


I haven't used the zip library that Nicholas mentioned, but this may be the F# version of what you want.

let create_zip_file (files: seq<string>) zipfile_name = 
    use zipfile = new ZipFile()
    files
    |> Seq.iter (fun f -> zip.AddFile(f))

    zipfile.Save(zipfile_name)

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.

create_zip_file ["first_file"] "first_file.zip"
create_zip_file ["second_file"] "second_file.zip"
create_zip_file ["first_file.zip"; "second_file.zip"] "big_file.zip"


As of .NET 4.6.2, there is ZipArchive class available:

namespace FSharpBasics

module ZipThis =

    open System.IO
    open System.IO.Compression
    open System.Reflection
    open System

    let create (zipName: string) (files: seq<FileInfo>) =
        use s = File.Create(zipName)
        use z = new ZipArchive(s, ZipArchiveMode.Create)
        files
            |> Seq.map (fun item -> (item.FullName, item.Name))
            |> Seq.iter (fun (path, name) -> z.CreateEntryFromFile (path, name) |> ignore)

    [<EntryPoint>]
    let main argv =
        let di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory)
        printfn "Creating test.zip on current directory"
        create "test.zip" (di.GetFiles "*.dll")
        printfn "Created"
        0
0

精彩评论

暂无评论...
验证码 换一张
取 消