以编程方式从SharePoint文档库中压缩文件(无第三方工具)

时间:2015-02-24 13:54:40

标签: c# sharepoint zip

我想要实现的目标是在SharePoint表单库中创建一个InfoPath表单(.Net 4.5 c#代码),然后按下按钮,将文档库中的某些文件包装到一个zip文件中。 困难在于,我无法使用第三方工具。

我的方法看着这样的时刻:

SPSecurity.RunWithElevatedPrivileges(delegate()
           {
               using (SPSite siteCollection = new SPSite(site))
               {
                   using (SPWeb oWeb = siteCollection.OpenWeb())
                   {
                       //Identifies the mode we will be using - the default is Create
                       ZipArchiveMode mode = ZipArchiveMode.Create;
                        using (ZipArchive zipFile = ZipFile.Open(archiveFullName, mode))
                        {
                            foreach (string file in files)
                            {
                                //Adds the file to the archive
                                zipFile.CreateEntryFromFile(file, Path.GetFileName(file), compression);
                            }
}
}
}
});

问题在于,ZipFile Open() - Method(也可能是' CreateEntryFromFile' -Method)也不会将URL作为参数和抛出NotSupportedException。 有没有人知道如何解决这个问题?

提前谢谢你。 最好的祝福 Greven的

1 个答案:

答案 0 :(得分:0)

看起来您正在创建一个全新的zip文件,而不是更新现有的zip文件(如果我的假设错误,请告诉我)。如果要在内存中创建zip存档而不是在磁盘上创建zip文件,则可以使用ZipArchive类。

using (MemoryStream stream = new MemoryStream())
{
    using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Create))
    {
        archive.CreateEntryFromFile(fileToPutInZip, fileNameInZip);
    }

    // To get the zip bytes (i.e. to return in HTTP response maybe):
    byte[] bytes = stream.ToArray();

    // TODO: Do something with zip bytes
}
相关问题