Daivid,
Here is what I have done to create the zipped file from a folder on sharePoint Document Library (on the fly without saving a physical copy of this document library folder and its content on disk)
what happens is that when I download the created the zipped file and try to extract it, it tells me that the following path can't be found...and he refers to the path of the folders and files i tried to add to the zip file
please have a look at the code below and tell me what could be wrong or what needs to be done...
also if you have a sample code of creating a zipped file using the ZipOutputStream and then downloading it, this would be perfect...
public void MainMethod()
{
string tempZippedFilePath;
string zippedFileName = "zipped";
DownloadAssignment(out tempZippedFilePath);
Response.Clear();
Response.AppendHeader(
"content-disposition",
"attachment; filename=" + zippedFileName + ".zip");
Response.WriteFile(tempZippedFilePath);
Response.Flush();
Response.Close();
}
public void DownloadAssignment(out tempZippedFilePath)
{
StringBuilder zippedFilePath = new StringBuilder();
zippedFilePath.AppendFormat("{0}{1}{2}{3}", finalTempFolderPath, @"\",
assignmentFolderName.ToString(), ".zip");
tempZippedFilePath = zippedFilePath.ToString();
CreateZip(tempZippedFilePath, assignmentFolders[0].Folder);
}
public void CreateZip(string tempZippedFilePath, SPFolder assignmentFolder)
{
ZipOutputStream zipOutputStream = new ZipOutputStream(File.Create(tempZippedFilePath));
zipOutputStream.SetLevel(9);
ZipEntry assFolderEntry = new ZipEntry(assignmentFolder.Name);
zipOutputStream.PutNextEntry(assFolderEntry);
foreach (SPFolder subFolder in assignmentFolder.SubFolders)
{
ZipEntry assSubFolderEntry = new ZipEntry(assignmentFolder.Name + @"\" + subFolder.Name);
zipOutputStream.PutNextEntry(assSubFolderEntry);
foreach (SPFile file in subFolder.Files)
{
if ((bool)file.Item["IsLatest"] == true)
{
ZipEntry fileEntry = new ZipEntry(
assignmentFolder.Name +
@"\" +
subFolder.Name +
@"\" +
file.Name);
zipOutputStream.PutNextEntry(fileEntry);
using (Stream fileStream = file.OpenBinaryStream())
{
byte[ buffer = new byte[4096];
int sourceBytes;
do
{
sourceBytes = fileStream.Read(buffer, 0, buffer.Length);
zipOutputStream.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}
}
}
}
zipOutputStream.Finish();
zipOutputStream.Close();
}
/// <summary>
/// Creates a temp folder to hold the assignment files while keeping their correct
/// folder structure as in the document library
/// </summary>
/// <param name="systemTempPath">Path of the current system's temporary folder</param>
/// <param name="finalTempFolderPath">Final path to the temp folder created</param>
private void CreateTempFolder(string systemTempPath, out string finalTempFolderPath)
{
StringBuilder tempFolderPath = new StringBuilder();
tempFolderPath.AppendFormat("{0}{1}", systemTempPath, "SLK_Temp");
if (Directory.Exists(tempFolderPath.ToString()))
{
int folderNameSuffix = 1;
finalTempFolderPath = tempFolderPath.ToString() + folderNameSuffix.ToString();
while (Directory.Exists(finalTempFolderPath))
{
folderNameSuffix++;
finalTempFolderPath = tempFolderPath.ToString() + folderNameSuffix.ToString();
}
Directory.CreateDirectory(finalTempFolderPath);
}
else
{
finalTempFolderPath = tempFolderPath.ToString();
Directory.CreateDirectory(finalTempFolderPath);
}
}
Also I need to know if it is correct to add an entry for a folder....or I can just add entries for files?
what if I need to have a file added under a certain hierarchy of folders that are not yet created in the zip file? does your last example of giving the needed path of the file to the instructor of ZipEntry solve this without adding zipEntry for folders?
Daivid, I know I am asking many questions but I got stuck with that for 2 days and I really need to finish that ASAP...
I really apreciate your efforts...
Thanks a million :)