using ICSharpCode.SharpZipLib.Zip; using Learun.Application.Base.SystemModule; using Learun.Application.TwoDevelopment.ZZDT_EC; using Learun.Application.Web.Common; using Learun.Util; using Learun.Util.Operat; using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Mvc; using SystemIO = System.IO; namespace Learun.Application.Web.Areas.ZZDT_EC.Controllers { /// /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架 /// Copyright (c) 2013-2018 Hexagon PPM /// 创 建:超级管理员 /// 日 期:2022-04-11 09:45 /// 描 述:项目备份表 /// public class ec_project_bakupController : MvcControllerBase { private ec_project_bakupIBLL ec_project_bakupIBLL = new ec_project_bakupBLL(); #region 视图功能 /// /// 主页面 /// /// [HttpGet] public ActionResult Index() { return View(); } /// /// 表单页 /// /// [HttpGet] public ActionResult Form() { return View(); } #endregion #region 获取数据 /// /// 获取页面显示列表数据 /// /// 查询参数 /// [HttpGet] [AjaxOnly] public ActionResult GetPageList(string pagination, string queryJson) { Pagination paginationobj = pagination.ToObject(); var data = ec_project_bakupIBLL.GetList(queryJson, paginationobj); var jsonData = new { rows = data, total = paginationobj.total, page = paginationobj.page, records = paginationobj.records }; return Success(jsonData); } /// /// 获取表单数据 /// /// [HttpGet] [AjaxOnly] public ActionResult GetFormData(string keyValue) { var ec_project_bakupData = ec_project_bakupIBLL.GetEntity(keyValue); var jsonData = new { ec_project_bakup = ec_project_bakupData, }; return Success(jsonData); } #endregion #region 提交数据 /// /// 删除实体数据 /// 主键 /// /// [HttpPost] [AjaxOnly] public ActionResult DeleteForm(string keyValue) { ec_project_bakupIBLL.DeleteEntity(keyValue); return Success("删除成功!", "项目备份", OperationType.Delete, null, null); } /// /// 保存实体数据(新增、修改) /// 主键 /// /// [HttpPost] [ValidateAntiForgeryToken] [AjaxOnly] public ActionResult SaveForm(string keyValue, string strEntity) { ec_project_bakupEntity entity = strEntity.ToObject(); ec_project_bakupIBLL.OnlySaveEntity(keyValue, entity); return Success("保存成功!", "项目备份", string.IsNullOrEmpty(keyValue) ? OperationType.Create : OperationType.Update, null, null); } #endregion #region 项目备份还原功能 /// /// 项目备份 /// /// public ActionResult ProjectBackup(string ProjectId, string ProjectIndex, string ProjectName) { UserInfo userInfo = LoginUserInfo.Get(); //获取要备份的数据库表名 List list_tblName = BackupTable.GetBackupTableList(); list_tblName.Add("lr_base_annexesfile"); string filename = ProjectName + "_备份_" + DateTime.Now.ToString("yyyyMMddHHmmss");//文件名 string filePath = string.Format("{0}/{1}/{2}/{3}/{4}/{5}", Config.GetValue("AnnexesFile"), "Backup", LoginUserInfo.Get().userId, DateTime.Now.ToString("yyyyMMdd"), Guid.NewGuid().ToString("D"), filename); string ZipFilesPath = filePath + ".zip";//ZIP包存放的路径 long fileSize = 0; new TaskFactory().StartNew(() => { try { string sqlitePath = Path.Combine(filePath, "dbbackup"); if (!Directory.Exists(sqlitePath)) { Directory.CreateDirectory(sqlitePath); } sqlitePath = Path.Combine(sqlitePath, "sqlitebackup.db"); //创建sqlite文件 SqliteHelper.NewSqliteDatabase(sqlitePath); string fileBackupPath = filePath + "/filebackup"; if (!Directory.Exists(fileBackupPath)) { Directory.CreateDirectory(fileBackupPath); } //往sqlite里面添加和插入表数据 ec_project_bakupIBLL.CreateAndAddBySqlit(ProjectId, list_tblName, sqlitePath, fileBackupPath, ProjectIndex); //提交 SqliteHelper.SaveChange(); //将集合生成为压缩文件 ZipOutputStream stream = new ZipOutputStream(SystemIO.File.Create(ZipFilesPath)); stream.SetLevel(0); // 压缩级别 0-9 byte[] buffer = new byte[4096]; //缓冲区大小 string[] filenames = Directory.GetFiles(filePath, "*.*", SearchOption.AllDirectories); foreach (string file in filenames) { ZipEntry entry = new ZipEntry(file.Replace(filePath, "")); stream.PutNextEntry(entry); using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { int sourceBytes; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); stream.Write(buffer, 0, sourceBytes); } while (sourceBytes > 0); } } fileSize = stream.Length; stream.Finish(); stream.Close(); //Directory.Delete(filePath.Replace('/', '\\'), true); } catch (Exception ex) { SqliteHelper.Close(); Logger.Error($"项目备份异常:{ex.Message}"); } }).ContinueWith(t => { //往数据库备份表中和附件表中插入数据 ec_project_bakupIBLL.SaveEntity(ProjectId, ProjectName, ZipFilesPath, userInfo.userId, userInfo.realName, fileSize); }); return Success("项目备份中..."); } /// /// 项目还原 /// /// public ActionResult ProjectRestore(string ProjectIndex, string fileId) { try { //获取附件信息 var annexesFile = new AnnexesFileBLL().GetByfolderIdEntity(fileId); if (annexesFile == null) { return Fail("文件未上传完成,项目还原异常!!!"); } var size = double.Parse(annexesFile.F_FileSize) / 1024 / 1024; if (size > 500) { return Fail("ZIP文件大于限制值500M,项目还原异常!!!"); } if (!string.IsNullOrWhiteSpace(annexesFile?.F_FilePath)) { string virtualPath = string.Format("{0}/{1}/{2}/{3}/{4}", Config.GetValue("AnnexesFile"), "BackupDecompress", LoginUserInfo.Get().userId, DateTime.Now.ToString("yyyyMMdd"), Guid.NewGuid().ToString("D")); //如果解压目录不存在则创建 if (!Directory.Exists(virtualPath)) { Directory.CreateDirectory(virtualPath); } Decompress(virtualPath, annexesFile.F_FilePath, ProjectIndex); } } catch (Exception ex) { return Fail("项目还原异常!"); } return Success("项目还原成功!"); } /// /// 解压zip格式的文件 /// /// 解压文件存放路径 /// 压缩文件路径 /// 项目序号 /// public void Decompress(string unZipDir, string zipFilePath, string ProjectIndex) { byte[] data = new byte[2048]; int size = 2048; //一个ZIP文件中的条目,可以理解为压缩包里面的一个文件夹/文件 ZipEntry theEntry = null; //解压缩输出流,从压缩包中一个接一个的读出文档。 using (ZipInputStream inputStream = new ZipInputStream(SystemIO.File.OpenRead(zipFilePath))) { while ((theEntry = inputStream.GetNextEntry()) != null) { if (theEntry.IsDirectory) {// 该结点是目录 if (!Directory.Exists(unZipDir + "/" + theEntry.Name)) { Directory.CreateDirectory(unZipDir + "/" + theEntry.Name); } } else { if (!string.IsNullOrEmpty(theEntry.Name)) { string fileName = unZipDir + theEntry.Name; fileName = fileName.Replace('/', '\\'); if (!Directory.Exists(Path.GetDirectoryName(fileName)))//判断是否存在 { Directory.CreateDirectory(Path.GetDirectoryName(fileName));//创建新路径 } using (FileStream streamWriter = SystemIO.File.Create(fileName)) { while (true) { size = inputStream.Read(data, 0, data.Length); if (size <= 0) break; streamWriter.Write(data, 0, size); } streamWriter.Close(); } } } } inputStream.Close(); } //从sqlite里面获取数据还原 List list_tblName = BackupTable.GetBackupTableList(); string sqlitePath = unZipDir + "\\dbbackup\\sqlitebackup.db";//sqlite路径 string fileBackupPath = unZipDir + "\\filebackup";//附件路径 ec_project_bakupIBLL.RestoreBySqlite(list_tblName, sqlitePath, fileBackupPath, ProjectIndex); } /// /// 下载备份文件 /// public void BackupDown(string projectBakupID) { //根据项目ID获取备份数据 var data = ec_project_bakupIBLL.GetEntity(projectBakupID); if (data != null) { var annexesFile = new AnnexesFileBLL().GetList(data.FolderId).First(); if (!string.IsNullOrEmpty(annexesFile?.F_FilePath)) { FileInfo fileinfo = new SystemIO.FileInfo(annexesFile?.F_FilePath); if (fileinfo.Exists) { string filename = data.BakupName + ".zip"; FileStream fs = new FileStream(annexesFile?.F_FilePath, SystemIO.FileMode.Open); byte[] bytes = new byte[(int)fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); Response.Charset = "UTF-8"; Response.ContentEncoding = Encoding.GetEncoding("UTF-8"); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(filename)); Response.BinaryWrite(bytes); Response.Flush(); Response.End(); } } } } #endregion } }