FSharp 中 zip 存档的控制文件结构

发布于 2024-10-15 18:27:34 字数 889 浏览 7 评论 0原文

以下是一个代码示例,它获取文件名列表并将它们压缩到单个存档中。我遇到的问题是,我希望 filname 描述的文件位于 zip 存档的顶层(即打开存档时,“clientName....xml”是您看到的第一个内容,而不是文件夹“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)

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

蓝眼睛不忧郁 2024-10-22 18:27:34

ZipFile 类型从何而来?我不认为这是一个标准的 .NET 类...我尝试搜索并找到了这个库 http://dotnetzip。 codeplex.com/ 具有与您的示例匹配的类:-)

提到的库还具有 AddFile 重载,它需要两个字符串 - 源文件名和 ZIP 中的相对文件名文件。这看起来正是您正在寻找的东西。我猜调用会类似于 zipfile.AddFile(absolutePath, "/") ...

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 和您的相关数据。
原文