none
Extracting Zip and upload files to sharepoint

    Question

  • i am task to write a console application get a zip file from a FTP source, extract the zip in c# code and upload the individual files into sharepoint.

    i am using ICSharpCode.SharpZipLib

    my code thus far

    using (SPSite oSite = new SPSite(siteUrl))
    {
       using (SPWeb oWeb = oSite.OpenWeb())
       {
    
          //getFileFromFTP() will return Stream
          ZipFile zipFile = new ZipFile(getFileFromFTP(ftpUrl));
    
          if (zipFile != null)
          {
             foreach (ZipEntry entry in zipFile)
             {
                if (entry.IsFile)
                {
                     string fileName = entry.Name;
                     Console.WriteLine("Now processing zip entry: " + fileName);
                      string newFolder = fileName.Substring(0, fileName.IndexOf(']'));
    
                       newFolder = newFolder.Replace("]", "").Replace("[", "");
                       Console.WriteLine("Will create to folder: " + newFolder);
                       Console.WriteLine("Checking for folder existence: " + oWeb.ServerRelativeUrl + "/Main Library/" + sharepointFolder + "/" + newFolder);
    
                       #region create folder 
                       if (!oWeb.GetFolder(oWeb.ServerRelativeUrl + "/Main Library/" + sharepointFolder + "/" + newFolder).Exists)
                       {
                           SPList docLibrary = oWeb.Lists["Main Library"];
                           oWeb.AllowUnsafeUpdates = true;
                           string pathToFolder = docLibrary.RootFolder.ServerRelativeUrl + "/" + sharepointFolder;
    
                           try
                           {
                                var addFolder = docLibrary.Items.Add(pathToFolder, SPFileSystemObjectType.Folder, newFolder.Trim());
    
                                addFolder.Update();
                           }
                           catch (FileNotFoundException fileexe)
                           {
                             Console.WriteLine(fileexe.Message);
    
                           }
                           catch (Exception exe)
                           {
                              Console.WriteLine(exe.Message);
                           }
                           oWeb.AllowUnsafeUpdates = false;
                     }
                     #endregion
    
    
                    SPFolder folder = oWeb.GetFolder(oWeb.ServerRelativeUrl + "/Main Library/" + sharepointFolder + "/" + newFolder);
                    Stream zipStream = new MemoryStream();
                    if (zipFile != null)
                    {
                         if (entry != null)
                         {
                            zipStream = zipFile.GetInputStream(entry);
                         }
                         else
                         {
                             Console.WriteLine("Zip entry is null");
                         }
                     }
                     else
                     {
                         Console.WriteLine("Zip File is null");
                      }
                      if (zipStream != null)
                      {
                         var fileAdd = folder.Files.Add(fileName, zipStream);
                         fileAdd.Update();
                      }
                      else
                      {
                         Console.WriteLine("the stream for zip entry: " + fileName + " is null");
                      }
                  }
                }
             }
             else
             {
                        Console.WriteLine("Stream is empty");
         }
      }
    }

    exception message



    Wednesday, May 3, 2017 9:57 AM

Answers

  • hi i managed to solve my issue. 

    i make changes to the code

    MemoryStream myMemoryStream = new MemoryStream();
    entry.Open().CopyTo(myMemoryStream);
    var fileAdd = folder.Files.Add(fileName, myMemoryStream);


    • Marked as answer by Tweety Tan Monday, May 8, 2017 8:45 AM
    Monday, May 8, 2017 8:45 AM

All replies

  • Hi Tweety Tan,

    Thank you for posting here.

    According to your question is more related to sharepoint, I will move it to  SharePoint 2013 - Development and Programming forum for suitable support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools.

    If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Thanks for your understanding and cooperation.

    Best Regards,

    Wendy 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, May 4, 2017 5:56 AM
  • Hi,

    Could you provide detail error information, it’s better to provide the StackTrace as below.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, May 5, 2017 10:37 AM
  • hi i managed to solve my issue. 

    i make changes to the code

    MemoryStream myMemoryStream = new MemoryStream();
    entry.Open().CopyTo(myMemoryStream);
    var fileAdd = folder.Files.Add(fileName, myMemoryStream);


    • Marked as answer by Tweety Tan Monday, May 8, 2017 8:45 AM
    Monday, May 8, 2017 8:45 AM