【问题标题】:Programmatically zip files from SharePoint document library (no 3rd party tools)以编程方式从 SharePoint 文档库压缩文件(无第三方工具)
【发布时间】:2015-02-24 13:54:40
【问题描述】:

我想要实现的是,在 SharePoint 表单库中拥有一个 InfoPath 表单(.Net 4.5 c# 代码),然后按下按钮,文档库中的某些文件被打包成一个 zip 文件。 困难在于,我不能使用 3rd 方工具。

目前我的方法是这样的:

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()-方法(也可能是“CreateEntryFromFile”-方法)没有将 URL 作为参数并引发 NotSupportedException。 有没有人知道如何解决这个问题?

提前谢谢你。 最好的祝福 绿色

【问题讨论】:

    标签: c# sharepoint zip


    【解决方案1】:

    看起来您正在创建一个全新的 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2012-02-15
      • 2010-10-24
      相关资源
      最近更新 更多