control file structure of a zip archive in FSharp

发布于 2022-09-06 08:56:04 字数 1094 浏览 18 评论 0

the following is a code sample that takes a list of file names and zips them into a single archive. The problem I'm having is that I'd like for the file described by filname be in the top level of the zip archive (i.e. when the archive is opened, "clientName....xml" is the first thing you see, instead of the folder "XML").

    let filename = sprintf "C:\\XML\\ClientName_%s.xml" (System.DateTime.Now.ToString("ddMMyyyy"))      
    use fs = new FileStream(filename, FileMode.Create) 
    let xmlSerializer = XmlSerializer(typeof<log>)
    xmlSerializer.Serialize(fs,logObj)
    fs.Close()
    use zipfile = new ZipFile()
    let basePath = path.Replace("/", "\\")

    for fileObj in files do
        let relativeFilePath = basePath  + (fileObj.Filename).Replace("/", "\\")
        printfn "%s" relativeFilePath
        zipfile.AddFile(relativeFilePath) |> ignore
        ()

    zipfile.AddFile(filename) |> ignore
    let zipFileName = sprintf "C:\\XML\\Compliance_%s.zip" (System.DateTime.Now.ToString("ddMMyyyy"))
    zipfile.Save(zipFileName)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海里的那抹蓝 2022-09-13 08:56:04

Where does the ZipFile type come from? I don't think this is a standard .NET class... I tried searching and found this library http://dotnetzip.codeplex.com/ which has a class matching to your sample :-)

The mentioned library also has AddFile overload that takes two string - the source file name and a relative file name in the ZIP file. This seems exactly like what you're looking for. I guess the call would be something like zipfile.AddFile(absolutePath, "/")...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文