Frage

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)
War es hilfreich?

Lösung

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, "/")...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top