从SharePoint压缩文件夹并将Zip文件夹复制到SharePoint上的另一个文件夹 [英] Zip a folder from SharePoint and copy the Zip folder to another Folder on SharePoint

查看:95
本文介绍了从SharePoint压缩文件夹并将Zip文件夹复制到SharePoint上的另一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好团队,

我在SharePoint Online上有两个文件夹的要求是

I got a requirement where I have two folders on SharePoint Online:

  1. ZipResult

我正在尝试将C#方法写入Zip(存档) ZipSource 文件夹中的所有文件,并将此Zip文件夹复制到 ZipResult 文件夹,并将Zip文件夹命名为触发此方法的DateTime.Now.ToString().

I am trying to write a C# method to Zip(Archive) all the files in ZipSource folder and copy this Zip folder to the ZipResult folder and to name the Zip folder as the DateTime.Now.ToString() at which this method is triggered.

我已经设置了与SharePoint的连接,并检查了 ZipSource 文件夹中的文件,但是找不到合适的代码来压缩文件并将其复制到 ZipResult 文件夹

I have setup the connection to SharePoint and check the files in the ZipSource folder, but not able to find the appropriate code to Zip the files and Copy to ZipResult folder

以下是代码:

                   

字符串 " xyz.onmicrosoft.com"

string userName = "xyz.onmicrosoft.com";

SecureString密码= ConvertToSecureString(             使用 var " https://xyz.sharepoint.com/sites /Test" ))

            using (var clientContext = new ClientContext("https://xyz.sharepoint.com/sites/Test"))

  ;        {

            {

         //SharePoint Online凭据 

                // SharePoint Online Credentials 

         clientContext.Credentials =

                clientContext.Credentials = new SharePointOnlineCredentials(userName, password);

         //获取SharePoint网站.

                // Get the SharePoint web 

         字符串sourceFolderPath = "ABC/GAB/ZipSource" ;

                String sourceFolderPath = "ABC/GAB/ZipSource";

         字符串destinationFolderPath = " ABC/GAB/ZipResult"" ; ;   

                String destinationFolderPath = " ABC/GAB/ZipResult";          

         Web web = clientContext.Web;

                Web web = clientContext.Web;

          clientContext.Load(web);

                clientContext.Load(web);

         clientContext.Load(web.Lists);

                clientContext.Load(web.Lists);

         clientContext.Load(web,wb => wb.ServerRelativeUrl);

                clientContext.Load(web, wb => wb.ServerRelativeUrl);

         clientContext.ExecuteQuery();

                clientContext.ExecuteQuery();

         列表列表= web.Lists.GetByTitle( "ABC" );

                List list = web.Lists.GetByTitle("ABC");

         clientContext.Load(list);

                clientContext.Load(list);

         clientContext.ExecuteQuery();

                clientContext.ExecuteQuery();

         文件夹sourceFolder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + sourceFolderPath);

                Folder sourceFolder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + sourceFolderPath);

         clientContext.Load(sourceFolder);

                clientContext.Load(sourceFolder);

         clientContext.ExecuteQuery();

                clientContext.ExecuteQuery();

         文件夹destinationFolder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + destinationFolderPath);

                Folder destinationFolder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + destinationFolderPath);

         clientContext.Load(destinationFolder);

                clientContext.Load(destinationFolder);

         clientContext.ExecuteQuery();

                clientContext.ExecuteQuery();

         CamlQuery camlQuery =

                CamlQuery camlQuery = new CamlQuery();

         camlQuery.ViewXml = @"< View Scope ='Recursive'>"

                camlQuery.ViewXml = @"<View Scope='Recursive'>

              nbsp; bsp      < Query>

                                     <Query>

              nbsp; bsp      </Query>

                                     </Query>

              nbsp; bsp   </View>" ;

                                 </View>";

         camlQuery.FolderServerRelativeUrl = sourceFolder.ServerRelativeUrl;

                camlQuery.FolderServerRelativeUrl = sourceFolder.ServerRelativeUrl;

         ListItemCollection listItems = list.GetItems(camlQuery);

                ListItemCollection listItems = list.GetItems(camlQuery);

         clientContext.Load(listItems);

                clientContext.Load(listItems);

       clientContext.ExecuteQuery();

                clientContext.ExecuteQuery();

 

              

  /////将代码压缩并复制到ZipResult文件夹

  ////Code to Zip and Copy to ZipResult folder

}

我们非常感谢您的帮助.

Any help is highly appreciated.

推荐答案

示例代码供您参考.

class Program
    {
        static void Main(string[] args)
        {
            string login = "lee@domain.onmicrosoft.com"; //give your username here  
            string password = "pw"; //give your password  
            var securePassword = new SecureString();
            foreach (char c in password)
            {
                securePassword.AppendChar(c);
            }

            string siteUrl = "https://domain.sharepoint.com/sites/Developer";
            using (ClientContext clientContext = new ClientContext(siteUrl))
            {
                clientContext.Credentials = new SharePointOnlineCredentials(login, securePassword);
                Microsoft.SharePoint.Client.List myList = clientContext.Web.Lists.GetByTitle("MyDoc");

                var zipFile = myList.GetItemById(36).File;
                ClientResult<System.IO.Stream> data = zipFile.OpenBinaryStream();
                clientContext.Load(zipFile);
                clientContext.ExecuteQuery();
                if (data != null)
                {
                    using (var memory = new MemoryStream())
                    {
                        byte[] buffer = new byte[1024 * 64];
                        int nread = 0;

                        while ((nread = data.Value.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            memory.Write(buffer, 0, nread);
                        }
                        memory.Seek(0, SeekOrigin.Begin);
                        //Here you have the file in MemoryStream (memory) and you can use it
                        using (ZipArchive archive = new ZipArchive(memory, ZipArchiveMode.Read))
                        {
                            foreach (var entry in archive.Entries)
                            {
                                Console.WriteLine(entry.FullName);
                                var stream = entry.Open();
                                byte[] fileBytes;
                                using (var ms = new MemoryStream())
                                {
                                    stream.CopyTo(ms);
                                    fileBytes = ms.ToArray();

                                    stream.Read(fileBytes, 0, fileBytes.Length);
                                    stream.Close();
                                    var fileCreationInformation = new FileCreationInformation();
                                    fileCreationInformation.Content = fileBytes;
                                    fileCreationInformation.Overwrite = true;
                                    var fileInfo= entry.FullName.Split('/');
                                    fileCreationInformation.Url = fileInfo[fileInfo.Length-1];

                                    Microsoft.SharePoint.Client.List docs = clientContext.Web.Lists.GetByTitle("MyDoc4");
                                    Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(fileCreationInformation);
                                    clientContext.ExecuteQuery();
                                }
                                
                            }
                        }
                    }
                }


            }
            Console.WriteLine("done");

            Console.ReadLine();
        }
    }

最好的问候,

Lee


这篇关于从SharePoint压缩文件夹并将Zip文件夹复制到SharePoint上的另一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆