使用 C# 的 ICSharpCode.SharpZipLib,我如何验证所传递的文件实际上是一个有效的 zip 文件(不是被右键单击并重命名为 .zip 的文件)?
[HttpPost]
public ActionResult Upload(HttpPostedFileBase fileData)
{
if (fileData != null && fileData.ContentLength > 0)
{
if (Path.GetExtension(fileData.FileName) == ".zip")
{
var zipFile = Server.MapPath("~/Content/uploads/" + Path.GetFileName(fileData.FileName));
fileData.SaveAs(zipFile);
FileStream fs = System.IO.File.OpenRead(zipFile);
ZipFile zf = new ZipFile(fs);
foreach (ZipEntry zipEntry in zf)
{
if (zipEntry.Name.EndsWith(".htm") || zipEntry.Name.EndsWith(".html"))
{
return Json(new { success = true });
}
}
fs.Close();
fs.Dispose();
System.IO.File.Delete(zipFile);
}
else
{
var fileName = Server.MapPath("~/Content/uploads/" + Path.GetFileName(fileData.FileName));
fileData.SaveAs(fileName);
return Json(new { success = true });
}
}
return Json(new { success = false });
}