/// <summary> /// 解压 /// </summary> /// <param name="zipFilePath"></param> private static string UnZipFile(string zipFilePath, out bool hasError) { hasError = false; if (File.Exists(zipFilePath)) { try { using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath))) { ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { Console.WriteLine(theEntry.Name); string directoryName = Path.GetDirectoryName(theEntry.Name); string fileName = Path.GetFileName(theEntry.Name); // create directory if (directoryName.Length > 0) { Directory.CreateDirectory(directoryName); } if (fileName != String.Empty) { using (FileStream streamWriter = File.Create(theEntry.Name)) { int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } } } } return(""); } } catch (Exception ex) { hasError = true; LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), "UnZipFile 发生异常!"); return(ex.Message); } } hasError = true; return(string.Format("Cannot find file '{0}'", zipFilePath)); }
/// <summary> /// 将一组文件打包成一个ZIP文件,并返回相对地址 /// </summary> /// <param name="lstFileZipInfo"></param> /// <returns></returns> public static string CreateZipFile(List <FileZipInfo> lstFileZipInfo, out bool hasError) { hasError = false; if (lstFileZipInfo != null && lstFileZipInfo.Count > 0) { string zipFilePath = ConfigHelper.LocalZipSavePath + Guid.NewGuid().ToString() + ".zip"; //ZIP文件保存路径 string zipFileDownloadUrl = "http://" + ConfigHelper.MainDomain + zipFilePath; //ZIP文件下载地址 zipFileDownloadUrl = zipFileDownloadUrl.Replace("\\", "/"); zipFilePath = HttpContext.Current.Server.MapPath(zipFilePath);//将保存地址转换为绝对路径 try { using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath))) { s.SetLevel(0); // 压缩级别 0-9 // 0 - store only to 9 - means best compression //s.Password = "******"; //Zip压缩文件密码 byte[] buffer = new byte[4096]; //缓冲区大小 foreach (FileZipInfo fileZipInfo in lstFileZipInfo) { ZipEntry entry = new ZipEntry(Path.GetFileName(fileZipInfo.FileName)); entry.DateTime = DateTime.Now; s.PutNextEntry(entry); using (FileStream fs = File.OpenRead(fileZipInfo.FilePath)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); s.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } s.Finish(); s.Close(); } return(zipFileDownloadUrl); } catch (Exception ex) { hasError = true; LogManager.DefaultLogger.Error(LogConvert.ParseWebEx(ex), "CreateZipFile 发生异常!"); return(ex.Message); } } hasError = true; return(""); }
/// <summary> /// 发送Email邮件 /// </summary> /// <param name="subject">主题</param> /// <param name="bodyContent">内容</param> /// <param name="mailto">收件人邮箱地址</param> /// <param name="fromDisplayName">显示名</param> /// <param name="filename">附件文件地址</param> /// <param name="mailccto">抄送人邮箱地址</param> /// <returns></returns> public static int SendEmail(string subject, string bodyContent, string mailto, string fromDisplayName = "", byte[] buffer = null, string filename = "", string mailccto = "") { if (string.IsNullOrEmpty(mailto)) { return(-9999); } using (MailMessage mailObj = new MailMessage()) { try { SmtpClient smtp = new SmtpClient(); //实例化一个 SmtpClient smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //将smtp的出站方式设为 Network smtp.EnableSsl = false; //smtp服务器是否启用SSL加密 smtp.Host = ConfigHelper.Email_SMTP; //指定 smtp 服务器地址 smtp.Port = 25; //指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去 smtp.UseDefaultCredentials = true; //如果需要认证,则用下面的方式 smtp.Credentials = new NetworkCredential(ConfigHelper.Email_UserName, ConfigHelper.Email_PWD); MailMessage mm = new MailMessage(); //实例化一个邮件类 if (buffer != null) //添加附件 { MemoryStream memoryStream = new MemoryStream(buffer); mm.Attachments.Add(new Attachment(memoryStream, filename)); } //mm.Priority = MailPriority.High; //邮件的优先级,分为 Low, Normal, High,通常用 Normal即可 mm.From = new MailAddress(ConfigHelper.Email_SendFrom, fromDisplayName, Encoding.GetEncoding(936));//收件方看到的邮件来源; mm.Sender = new MailAddress(ConfigHelper.Email_SendFrom, fromDisplayName, Encoding.GetEncoding(936)); mailto = GetMailToWithoutExcludeMailTo(mailto); //获取排除的邮箱以外的发送邮箱 if (string.IsNullOrEmpty(mailto)) { return(0); } mm.To.Add(mailto); //邮件的接收者,支持群发,多个地址之间用 半角逗号 分开//当然也可以用全地址添加 if (!string.IsNullOrEmpty(mailccto)) //抄送人 { mm.CC.Add(mailccto); } mm.Subject = subject; //邮件标题 mm.SubjectEncoding = Encoding.GetEncoding(936); // 这里非常重要,如果你的邮件标题包含中文,这里一定要指定,否则对方收到的极有可能是乱码。 //936是简体中文的pagecode,如果是英文标题,这句可以忽略不用 mm.IsBodyHtml = true; //邮件正文是否是HTML格式 mm.BodyEncoding = Encoding.GetEncoding(936); //邮件正文的编码, 设置不正确, 接收者会收到乱码 mm.Body = bodyContent; //邮件正文 smtp.Send(mm); return(0); } catch (Exception ex) { LogManager.DefaultLogger.Error(LogConvert.ParseComplexEx(ex), string.Concat("SendEmail 发生异常。", subject, "。", mailto)); return(-9999); } } }