Click here to Skip to main content
14,325,629 members
Rate this:
Please Sign up or sign in to vote.
See more:
I am trying to download multiple files from document library present in SharePoint 2013 document library. To achieve this I have created a cloud service in windows azure and fetching all the files info using CSOM and zipping it using sharpzip library as there are large no of files and file size are large(100 MB etc.) I am not zipping files in to memory stream because its taking too much time to zip files in memory stream. I am streaming zip file on the fly. And starts download, its working fine when I run the code on my local machine but when I host the service on Azure. It will corrupts the file and showing a message showing below. Its working fine for small files but not for large one. Following is the error showing when opening file.

The Compressed Zip folder is invalid.

Following is the code. Please let me know where do I mistake in the code

String ZipFileName = "Document.zip";
try
{
String[] DocumentIDArray = null;
if (DocumentID != null)
{
DocumentIDArray = DocumentID.Split(',');
}
string url = SiteUrl;
string username = ConfigurationSettings.AppSettings["username"].ToString();
SecureString securecreds = new SecureString();
var password = ConfigurationSettings.AppSettings["Password"].ToString();
foreach (char c in password)
{
securecreds.AppendChar(c);
}
/*Download Code*/
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BufferOutput = false;
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", ZipFileName));
HttpContext.Current.Response.ContentType = "application/Zip";
MemoryStream strm=new MemoryStream();
ICSharpCode.SharpZipLib.Zip.ZipOutputStream ZipOutputStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(HttpContext.Current.Response.OutputStream);
ZipOutputStream.SetLevel(0); //0-9, 9 being the highest level of compression
ZipOutputStream.UseZip64 = ICSharpCode.SharpZipLib.Zip.UseZip64.Off;
byte[] buffer = new byte[65536];
/*End Download code*/
SharePointOnlineCredentials creds = new SharePointOnlineCredentials(username, securecreds);
using (var clientContext = new Microsoft.SharePoint.Client.ClientContext(url))
{
clientContext.Credentials = creds;
var list = clientContext.Web.Lists.GetByTitle(DocumentLibraryName);
for (int i = 0; i < DocumentIDArray.Length; i++)
{
Microsoft.SharePoint.Client.ListItem listitem = list.GetItemById(DocumentIDArray[i]);
clientContext.Load(listitem.File);
}
clientContext.ExecuteQuery();
for (int i = 0; i < DocumentIDArray.Length; i++)
{
Microsoft.SharePoint.Client.ListItem listitem = list.GetItemById(DocumentIDArray[i]);
var fileRef = listitem.File.ServerRelativeUrl;
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
var fileName = (string)listitem.File.Name;
ICSharpCode.SharpZipLib.Zip.ZipEntry entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(ICSharpCode.SharpZipLib.Zip.ZipEntry.CleanName(fileName));
ZipOutputStream.PutNextEntry(entry);

int count = fileInfo.Stream.Read(buffer, 0, buffer.Length);
while (count > 0)
{
ZipOutputStream.Write(buffer, 0, count);
count = fileInfo.Stream.Read(buffer, 0, buffer.Length);
if (!HttpContext.Current.Response.IsClientConnected)
{
break;
}
HttpContext.Current.Response.Flush();
}

}
ZipOutputStream.Close();
HttpContext.Current.Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();

}


}
catch (Exception ex)
{
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
Posted
Updated 5-Sep-19 6:42am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

hi swapnil,
Did you get solution for the issue?
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100