Istudent Rana

Istudent Rana

  • 1.4k
  • 340
  • 83.7k

file did not open after extracting zip created by DotNetZip

Feb 20 2018 2:21 PM
here I am trying create zip folders including list of files passed through ajax call and downloading the zip on success return.
I have used DotNetZip Lib to create zip. Currently my code creates zip folder with files. Files are jpeg files. When i extract the zip
and try to open the file. File does not open. It looks like file is corrupted or formatting hasnot been done right.
What is wrong in my below code?
[HttpPost]
[ValidateAntiForgeryTokenAjax]
public ActionResult Generatezip(List<string> imageList)
{
string imgRootPath = ConfigurationManager.AppSettings["filepath"] + "\\";
string imgFile = string.Empty;
string imgKey = string.Empty;
string fileName = "DaysImage_";
try
{
var ms = new MemoryStream();
if (imageList.Count > 0)
{
string[] fileNames = imageList.FirstOrDefault().ToString().ToLower().Split('\\');
fileName += fileNames[0] + fileNames[1] + fileNames[2] + ".zip";
using (ZipFile zipFile = new ZipFile())
{
foreach (var imgPath in imageList)
{
imgKey = imgPath.Substring(imgPath.LastIndexOf("\\") + 1);
imgFile = imgRootPath + imgPath;
zipFile.AddEntry(imgKey, imgFile);
//zipFile.AddFile(imgFile);
}
zipFile.Save(ms);
TempData[fileName] = ms;
// zipFile.Save(ms);
}
}
ms.Position = 0;
return Json(new { success = true, fileName }, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
Logger.LogError(ex);
return Json(new { success = false, fileName }, JsonRequestBehavior.AllowGet);

}
}

public ActionResult DownloadImageZip(string fileName)
{
var ms = TempData[fileName] as MemoryStream;
if (ms == null)
return new EmptyResult();
TempData[fileName] = null;
return File(ms, "application/zip", fileName);
}
}

Answers (7)