/// <summary> /// 解压文件(By Id) /// </summary> private void DepressFile(List <int> ids) { using (var _dbContext = new FileCenterDbContext()) { var list = _dbContext.SystemFiles.Where(s => ids.Contains(s.Id)).ToList(); foreach (var item in list) { var fullPath = $"{AppConfig.FileUploadRootDirectory.Replace("\\", "/")}/{item.FilePath}"; if (!File.Exists(fullPath)) { ZipFileManager.DepressFile(Path.Combine(AppConfig.FileArchiveRootDirectory, item.ArchiveFileName), item.FileLongName, fullPath); item.FileCompressStatus = EnumFileCompressStatus.Uncompressed; item.ArchiveFileName = string.Empty; item.DecompressDateTime = DateTime.Now; } } _dbContext.SaveChanges(); } }
async Task IJob.Execute(IJobExecutionContext context) { await Task.Run(() => { var st = DateTime.Now; LogHelper.Info("Archive File Starting."); var runningResult = EnumOperateResult.Failure; var archiveFileList = new List <string>(); //产生的归档压缩文件 var allFileInfoList = new List <FileInfo>(); //此次归档所有的文件对象集合 var systemFileList = this.GetUnArchivedFiles(); using (var conn = new SqlConnection(AppConfig.FileDbConnectionString)) { conn.Open(); SqlTransaction transaction = conn.BeginTransaction(); try { //获取到的每个项目文件进行压缩 var projectNameList = systemFileList.Select(s => s.ProjectName).Distinct(); foreach (var projectName in projectNameList) { LogHelper.Info($"Archive File Start In Project - {projectName}."); var projectFileList = systemFileList.Where(s => s.ProjectName == projectName).ToList(); var directoryPath = Path.Combine(AppConfig.FileArchiveRootDirectory, projectName, DateTime.Now.ToString("yyyyMM")); if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); } var archiveFileName = $"{Guid.NewGuid()}.zip"; var archiveFileNameFullPath = Path.Combine(directoryPath, archiveFileName); if (!File.Exists(Path.Combine(archiveFileNameFullPath))) { using (File.Create(archiveFileNameFullPath)) { } } // 转换成FileInfo对象集合,方便操作 var fileInfoList = this.GetFileInfoList(projectFileList); if (!fileInfoList.Any()) { LogHelper.Info($"File Has Been Deleted Or Archived."); continue; } // 压缩所选文件 ZipFileManager.CompressFileList(fileInfoList, archiveFileNameFullPath); allFileInfoList.AddRange(fileInfoList); archiveFileList.Add(archiveFileNameFullPath); var relativeArchiveFileName = Path.Combine(projectName, DateTime.Now.ToString("yyyyMM"), $"{archiveFileName}"); // 在数据库中把正在归档 - 1 的记录设置成归档完成 - 2 this.SetFileToCompressed(fileInfoList, relativeArchiveFileName, conn, transaction); LogHelper.Info($"Archive File Done In Project - {projectName}."); } LogHelper.Info($"Archive File All Done. Time Usage : {(DateTime.Now - st).TotalSeconds} Seconds."); transaction.Commit(); runningResult = EnumOperateResult.Success; } catch (Exception ex) { transaction.Rollback(); // 删除已经归档的压缩文件 foreach (var zipFile in archiveFileList) { if (File.Exists(zipFile)) { File.Delete(zipFile); } } LogHelper.Error($"Archive File Occurs Error : {ex.ToString()}."); } } if (runningResult == EnumOperateResult.Success) { // 删除已压缩的文件 //this.DeleteCompressedFileList(allFileInfoList); } }); }