//功能是自动检测给定路径是否存在dish.zip压缩文件,如果存在,则判断是否最新,
//如果不是最新,则删除原来的压缩文件,重新压缩,
//如果给定路径不存在dish.zip压缩文件,则进行压缩
//最后用隐藏下载压缩文件
//出现的问题:在未发布网站前,测试没有任何问题。网站生成发布后,错误提示见下面截图。
//程序代码:
public void downLoadZip(string strZipPath, string strZipName)
{
FileStream fs = new FileStream(strZipPath, FileMode.Open, FileAccess.Read, FileShare.Read);
long byteslength = fs.Length;
byte[] bytes = new byte[(int)byteslength];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Length", byteslength.ToString());
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(strZipName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
}
#endregion
#region "Page_Load"
protected void Page_Load(object sender, EventArgs e)
{
#region "获取action"
RequestAction = Request.QueryString["action"];
#endregion
#region "action=download"
if (RequestAction == "download")
{
#region "给压缩文件方法参数 变量赋值"
zipName = "dish.zip";//客户端保存的文件名
zipPath = MapPath(@"~/dish.zip");//压缩文件保存路径
#endregion
#region "压缩文件存在"
if(File.Exists(zipPath))
{
#region "分别获取压缩文件和HTML最近一次修改的时间"
zipDateTime = Convert.ToDateTime(new FileInfo(zipPath).CreationTime.ToString());
try
{
htmlDateTime = Convert.ToDateTime(File.ReadAllText(MapPath(@"/UpdateTime.txt"), Encoding.UTF8).ToString());
}
catch
{
File.WriteAllText(MapPath(@"~\UpdateTime.txt"), DateTime.Now.ToString(), Encoding.UTF8);
htmlDateTime = Convert.ToDateTime(File.ReadAllText(MapPath(@"/UpdateTime.txt"), Encoding.UTF8).ToString());
}
#endregion
#region "比较时间,如果压缩文件不是最新的"
if (zipDateTime.CompareTo(htmlDateTime) < 0)
{
File.Delete(zipPath);
myCompression.ZipClass.ZipFileDictory(MapPath(@"~/HtmlAndImage/"), zipPath, "");
File.SetCreationTime(zipPath, DateTime.Now);
}
#endregion
}
#endregion
#region "压缩文件不存在,压缩HTML文件,设置文件创建时间为now"
else
{
myCompression.ZipClass.ZipFileDictory(MapPath(@"~/HtmlAndImage/"), zipPath, "");
File.SetCreationTime(zipPath, DateTime.Now);
}
#endregion
#region "下载zip文件"
downLoadZip(zipPath, zipName);
#endregion
}
#endregion
myCompression.ZipClass.ZipFileDictory(MapPath(@"~/HtmlAndImage/"), zipPath, "");
压缩使用的是同步还是异步的呀?
如果是同步是没问题的,要是异步就有问题了:
File.Delete(zipPath);
myCompression.ZipClass.ZipFileDictory(MapPath(@"~/HtmlAndImage/"), zipPath, "");
File.SetCreationTime(zipPath, DateTime.Now); //如果文件还没有压完这行是找不到文件的。
那一句出的错,是否有相关操作权限。
在 File.Delete(zipPath);这句后面先创建该文件,