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 imageList)
{
string imgRootPath = ConfigurationManager.AppSettings["filepath"] + "\\";
string imgFile = string.Empty;
string imgKey = string.Empty;
string fileName = "DaysImage_";
try
{
var ms = new MemoryStream();
[ValidateAntiForgeryTokenAjax]
public ActionResult Generatezip(List
{
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";
{
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);
}
}
{
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);
}
}
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);
}
}

Amit GuptaRank #190 · 12.8K pointsPosted Feb 20, 2018, 7:17 PM EST
Istudent RanaRank #4,270 · 354 pointsPosted Feb 21, 2018, 2:56 AM EST
Amit GuptaRank #190 · 12.8K pointsPosted Feb 21, 2018, 3:07 AM EST
zipFile.AddEntry(imgKey, imgFile);
Istudent RanaRank #4,270 · 354 pointsPosted Feb 21, 2018, 3:29 AM EST
Amit GuptaRank #190 · 12.8K pointsPosted Feb 21, 2018, 3:42 AM EST
Amit GuptaRank #190 · 12.8K pointsPosted Feb 21, 2018, 4:20 AM EST
Istudent RanaRank #4,270 · 354 pointsPosted Feb 21, 2018, 10:09 AM EST
zipFile.AddEntry(imgKey, imageToByteArray(imgRootPath + imgPath));