protected void Btn_DownLoad_ServerClick(object sender, EventArgs e) { string[] FileProperties = new string[2]; string htmlReportTemplatePath = "D:\\github\\Tools\\Tool\\PDFReportGenerator\\TemplateFiles";//待压缩文件目录 //string zipPath = "D:\\TestFiles\\zip\\a.zip"; //压缩后的目标文件 string sFileName = "a.zip"; string temp = Directory.GetCurrentDirectory(); string modelValue = ""; // 读取文件夹、文件,同时替换 List <ComplexFile> fileList = GetHtmlReportFiles(htmlReportTemplatePath, modelValue); byte[] fileBytes = new FileCompress().Compress(fileList); //File.WriteAllBytes(zipPath, fileBytes); Context.Response.ContentType = "application/octet-stream"; Context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(sFileName, Encoding.UTF8)); Context.Response.AddHeader("Content-Length", fileBytes.Length.ToString()); Context.Response.BinaryWrite(fileBytes); Context.Response.End(); Context.Response.Close(); }
static void Main(string[] args) { string[] FileProperties = new string[2]; string htmlReportTemplatePath = "D:\\github\\Tools\\Tool\\PDFReportGenerator\\TemplateFiles"; //待压缩文件目录 string zipPath = "D:\\TestFiles\\zip\\a.zip"; //压缩后的目标文件 string temp = Directory.GetCurrentDirectory(); string modelValue = ""; // 读取文件夹、文件,同时替换 List <ComplexFile> fileList = GetHtmlReportFiles(htmlReportTemplatePath, modelValue); byte[] fileBytes = new FileCompress().Compress(fileList); File.WriteAllBytes(zipPath, fileBytes); }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Magenta; Console.Write("Введите имя файла, который хотите найти: "); var myFileName = Console.ReadLine(); SearchFile.ApplyAllFiles(@"C:\", SearchFile.AddList, myFileName); Console.Write("Выберите файл из списка, который хотите открыть (укажите номер в списке): "); if (!int.TryParse(Console.ReadLine(), out int numberInList)) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Вы ввели не число"); System.Environment.Exit(1); } if (numberInList - 1 > SearchFile.getList.Count || numberInList < 1) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Такого номера в списке не существует"); System.Environment.Exit(2); } try { OpenInNotepad.openFile(SearchFile.getList, numberInList); } catch (ArgumentException ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ex.Message); } Console.ForegroundColor = ConsoleColor.Magenta; try { FileCompress.Compress(SearchFile.getList[numberInList - 1], SearchFile.getList[numberInList - 1].Split('.')[1] + "Compress.txt"); } catch (ArgumentException ex) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ex.Message); } Console.ReadKey(); }
private void backgroundWorkerAddContract_DoWork(object sender, DoWorkEventArgs e) { conn = new Conn(); FileInfo _contract = null; try { foreach (var item in uploadFileContracts) { try { // ################### INSERE NA BASE DE DADOS CONTRATOS DO TIPO TELA 16 ################### if (item.Tela.Equals(16)) { _tela = $"TELA {item.Tela}"; FileCompress fileCompress = null; table = CriaTabelaPdf(); item.Contracts.ToList().ForEach(w => { fileCompress = new FileCompress(); try { isDateContract = false; _contract = new FileInfo(w); countPercent++; using (PdfReader reader = new PdfReader(w)) { ITextExtractionStrategy its; pagina = _personDocument = string.Empty; for (int i = 1; i <= 1; i++) { its = new LocationTextExtractionStrategy(); pagina = PdfTextExtractor.GetTextFromPage(reader, i, its).Trim(); using (StringReader strReader = new StringReader(pagina)) { string line = string.Empty; while ((line = strReader.ReadLine()) != null) { arrayLine = line.Split(' ').ToList(); if (arrayLine.Any(ctn => ctn.Trim().Contains("CTFIN"))) { if (!arrayLine.Any(ctn => ctn.Trim().Equals("CTFIN/O016A"))) { isNotTela = true; break; } } if (!isDateContract) { if (!arrayLine.Any(c => Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\-\d{2}$)") || Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\/\d{4}\-\d{2}$)"))) { continue; } sequencia = arrayLine.ToList().FindIndex(c => Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\-\d{2}$)") || Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\/\d{4}\-\d{2}$)")); fileCompress.PersonName = string.Join(" ", arrayLine.Take((sequencia - 1)).ToArray()); fileCompress.NumberContract = _contract.Name.Split('_')[0]; _personDocument = arrayLine.FirstOrDefault(c => Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\-\d{2}$)") || Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\/\d{4}\-\d{2}$)")); fileCompress.PersonDocument = Regex.Replace(_personDocument, "[^0-9$]", string.Empty); isDateContract = true; continue; } else { if (!arrayLine.Any(c => Regex.IsMatch(c, @"(^\d{2}/\d{2}/\d{4}$)"))) { continue; } fileCompress.DateContract = Convert.ToDateTime(arrayLine.Find(c => Regex.IsMatch(c, @"(^\d{2}/\d{2}/\d{4}$)"))); fileCompress.TypeContract = item.TypeContract.ToString(); fileCompress.EncryptedFile = File.ReadAllBytes(_contract.FullName); } break; } } if (isNotTela) { break; } if (!string.IsNullOrWhiteSpace(_personDocument)) { break; } } if (!isNotTela) { dataRow = table.NewRow(); dataRow["TypeContract"] = fileCompress.TypeContract.Trim(); dataRow["PersonName"] = fileCompress.PersonName.Length > 100 ? fileCompress.PersonName.Substring(0, 100) : fileCompress.PersonName; dataRow["PersonDocument"] = fileCompress.PersonDocument; dataRow["NumberContract"] = fileCompress.NumberContract; dataRow["DateContract"] = fileCompress.DateContract; dataRow["DateInclude"] = DateTime.Now; dataRow["EncryptedFile"] = fileCompress.EncryptedFile; if (!table.AsEnumerable().Any(r => ((string)r["NumberContract"]) == fileCompress.NumberContract)) { listContractsTable.Add(Convert.ToInt64(fileCompress.NumberContract)); table.Rows.Add(dataRow); } } } contador++; backgroundWorkerAddContract.ReportProgress(countPercent, _contract); _contract = null; if (contador == 5000) { RemoveDuplicate(); contador = 0; var tab = new { item1 = table, item2 = item.TableName, }; if (table.Rows.Count > 0) { _thread = new Thread(new ParameterizedThreadStart(conn.FileStores)); _thread.Start(tab); if (_thread.ThreadState == System.Threading.ThreadState.Running) { _thread.Join(); } } table.Rows.Clear(); listContractsTable.Clear(); } } catch (iTextSharp.text.exceptions.InvalidPdfException exload) { Console.Clear(); Console.WriteLine(exload.Message + w); } catch (PdfException exload) { Console.Clear(); Console.WriteLine(exload.Message + w); } }); if (contador > 0) { RemoveDuplicate(); var tab = new { item1 = table, item2 = item.TableName, }; if (table.Rows.Count > 0) { conn.FileStores(tab); } contador = 0; table = null; listContractsTable.Clear(); } } else { // ################### INSERE NA BASE DE DADOS CONTRATOS DO TIPO TELA (18,20,25 e 34) ################### table = CriaTabela(); Thread.Sleep(500); listContractsTable.Clear(); totalArquivos = item.Contracts.Count(); backgroundWorkerAddContract.ReportProgress(0); _tela = $"TELA {item.Tela}"; int _typeTela = Convert.ToInt32(Regex.Replace(_tela, "[^0-9$]", string.Empty)); countPercent = 0; listContracts.Clear(); listContracts = fsf.GetNumberContract(_typeTela); item.Contracts.ToList().ForEach(w => { try { _contract = new FileInfo(w); dataRow = table.NewRow(); countPercent++; using (PdfReader reader = new PdfReader(w)) { ITextExtractionStrategy its; pagina = _personDocument = string.Empty; for (int i = 1; i <= 1; i++) { its = new LocationTextExtractionStrategy(); pagina = PdfTextExtractor.GetTextFromPage(reader, i, its).Trim(); using (StringReader strReader = new StringReader(pagina)) { string line = string.Empty; while ((line = strReader.ReadLine()) != null) { arrayLine = line.Split(' ').ToList(); if (arrayLine.Any(ctn => ctn.Trim().Contains("CTFIN"))) { if (!arrayLine.Any(ctn => ctn.Trim().Equals($"CTFIN/O0{_typeTela}A"))) { isNotTela = true; break; } else { break; } } } } if (isNotTela) { break; } } } if (isNotTela) { isNotTela = false; return; } _numberContract = _contract.Name.Split('_')[0]; dataRow["TypeContract"] = item.TypeContract; dataRow["NumberContract"] = _contract.Name.Split('_')[0]; dataRow["EncryptedFile"] = File.ReadAllBytes(_contract.FullName); if (!table.AsEnumerable().Any(r => ((string)r["NumberContract"]) == _numberContract)) { table.Rows.Add(dataRow); listContractsTable.Add(Convert.ToInt64(_contract.Name.Split('_')[0])); } contador++; backgroundWorkerAddContract.ReportProgress(countPercent, _contract); _contract = null; if (contador == 5000) { RemoveDuplicate(); contador = 0; var tab = new { item1 = table, item2 = item.TableName, }; if (table.Rows.Count > 0) { _thread = new Thread(new ParameterizedThreadStart(conn.FileStores)); _thread.Start(tab); if (_thread.ThreadState == System.Threading.ThreadState.Running) { _thread.Join(); } } table.Rows.Clear(); listContractsTable.Clear(); arrayLine.Clear(); } } catch (Exception exload) { string err = exload.Message; Console.WriteLine(err + "-----> " + _contract.FullName); } }); if (contador > 0) { RemoveDuplicate(); var tab = new { item1 = table, item2 = item.TableName, }; if (table.Rows.Count > 0) { conn.FileStores(tab); } contador = 0; table = null; listContractsTable.Clear(); arrayLine.Clear(); } } } catch (Exception exLoop) { string err = exLoop.Message; Console.WriteLine(err + "-----> " + _contract.FullName); } } } catch (Exception exErroWork) { string err = exErroWork.Message; Console.WriteLine(err + "-----> " + _contract.FullName); } }
//method add new soda since interfaz public void AddNewFile(string nfo, string pnf, float rc, float fc, float rp, string alg) { FileCompress n_compress = new FileCompress(nfo, pnf, rc, fc, rp, alg); lifo.Push(n_compress); }
/// <summary> /// 对fileInfo 的文件对象进行解压缩 /// </summary> /// <param name="fileInfo">fileInfo对象</param> /// <param name="outputPath">压缩对象解压输出的路径</param> public static void DeZip(this FileInfo fileInfo, string outputPath) { FileCompress.DeCompress(fileInfo.FullName, outputPath, 1024); }
/// <summary> /// 对 fileInfo 的文件对象进行压缩 /// </summary> /// <param name="fileInfo">fileInfo对象</param> /// <param name="outputPath">压缩对象解压输出的路径</param> /// <param name="passWord">压缩文件所用的密码</param> public static void Zip(this FileInfo fileInfo, string outputPath, string passWord) { FileCompress.Compress(fileInfo.FullName, outputPath, passWord); }
static void Main(string[] args) { List <string> diretorios = new List <string>(); DataTable table = null; DataRow dataRow = null; Console.WriteLine("Lendo contratos..."); string PathFileCompany = @"C:\@TombamentoV1_01\TOMBAMENTOS\TOMBAMENTO2020-02-14\2020-02-12\T004Z20"; try { table = CriaTabelaPdf(); string newNameContract = string.Empty; int contador = 0, totalContratos = 0, sequencia = 0; string pagina, _personDocument; bool isDateContract; List <string> arrayLine = new List <string>(); IEnumerable <string> fileContract = Directory.EnumerateFiles(PathFileCompany, "*_16.pdf", SearchOption.AllDirectories); FileCompress fileCompress = null; fileContract.ToList().ForEach(w => { fileCompress = new FileCompress(); try { isDateContract = false; FileInfo _contract = new FileInfo(w); using (PdfReader reader = new PdfReader(w)) { ITextExtractionStrategy its; pagina = _personDocument = string.Empty; for (int i = 1; i <= reader.NumberOfPages; i++) { its = new LocationTextExtractionStrategy(); pagina = PdfTextExtractor.GetTextFromPage(reader, i, its).Trim(); using (StringReader strReader = new StringReader(pagina)) { string line = string.Empty; while ((line = strReader.ReadLine()) != null) { arrayLine = line.Split(' ').ToList(); if (!isDateContract) { if (!arrayLine.Any(c => Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\-\d{2}$)") || Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\/\d{4}\-\d{2}$)"))) { continue; } sequencia = arrayLine.ToList().FindIndex(c => Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\-\d{2}$)") || Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\/\d{4}\-\d{2}$)")); fileCompress.PersonName = string.Join(" ", arrayLine.Take((sequencia - 1)).ToArray()); fileCompress.NumberContract = _contract.Name.Split('_')[0]; _personDocument = arrayLine.FirstOrDefault(c => Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\-\d{2}$)") || Regex.IsMatch(c, @"(^\d{3}.\d{3}.\d{3}\/\d{4}\-\d{2}$)")); fileCompress.PersonDocument = Regex.Replace(_personDocument, "[^0-9$]", string.Empty); isDateContract = true; continue; } else { if (!arrayLine.Any(c => Regex.IsMatch(c, @"(^\d{2}/\d{2}/\d{4}$)"))) { continue; } fileCompress.DateContract = Convert.ToDateTime(arrayLine.Find(c => Regex.IsMatch(c, @"(^\d{2}/\d{2}/\d{4}$)"))); fileCompress.TypeContract = "L"; fileCompress.EncryptedFile = File.ReadAllBytes(_contract.FullName); } break; } } if (!string.IsNullOrWhiteSpace(_personDocument)) { break; } } dataRow = table.NewRow(); dataRow["TypeContract"] = fileCompress.TypeContract.Trim(); dataRow["PersonName"] = fileCompress.PersonName.Length > 100 ? fileCompress.PersonName.Substring(0, 100) : fileCompress.PersonName; dataRow["PersonDocument"] = fileCompress.PersonDocument; dataRow["NumberContract"] = fileCompress.NumberContract; dataRow["DateContract"] = fileCompress.DateContract; dataRow["DateInclude"] = DateTime.Now; dataRow["EncryptedFile"] = fileCompress.EncryptedFile; table.Rows.Add(dataRow); } contador++; _contract = null; if (contador == 5000) { totalContratos += contador; Cnn.FileStores(table, "FileSafe16"); Console.WriteLine($"Aguarde...\n\nTotal Arnazenado {totalContratos} \n"); contador = 0; table.Rows.Clear(); } } catch (iTextSharp.text.exceptions.InvalidPdfException exload) { Console.WriteLine(exload.Message); } catch (PdfException exload) { Console.WriteLine(exload.Message); } }); if (contador > 0) { totalContratos += contador; Cnn.FileStores(table, "FileSafe16"); contador = 0; table = null; } } catch (Exception ex) { throw ex; } Console.WriteLine("\n\n\n CONCLUIDO"); Console.ReadKey(); }
public CommandResult <KCT_ACCOUNTSET> ImportDataToTempTable(string fileName) { CommandResult <KCT_ACCOUNTSET> result = new CommandResult <KCT_ACCOUNTSET>(); result.Result = false; string filePath = HttpContext.Current.Server.MapPath("~/data/" + fileName); string directoryPath = filePath.Substring(0, filePath.LastIndexOf('.')); try { FileCompress.Decompress(directoryPath, filePath); } catch (Exception e) { result.Message = e.Message; return(result); } #region 校验文件,获得帐套ID //key是TableName value是保存到的文件名 Dictionary <string, string> tableList = LoadBCPConfigTables(); int accountSetID = 0; string accountSetName = string.Empty; ValidateUploadData(directoryPath, tableList, ref accountSetID, ref accountSetName); if (accountSetID == 0) { result.Message = "错误的数据文件!"; try { File.Delete(filePath); } catch { } return(result); } #endregion Dictionary <string, string> dicData = new Dictionary <string, string>(); string[] strs = null; foreach (var tableInfo in tableList) { strs = tableInfo.Key.Split('.'); dicData.Add(strs[0] + "..Temp_" + strs[strs.Length - 1], directoryPath + "\\" + tableInfo.Value + ".txt"); } #region 除临时表中数据,不成功直接返回错误 if (!DelTempTableData(accountSetID)) { return(result); } #endregion //执行BCP导入数据到临时表中 if (DataProvider.DirecImportData(dicData)) { int r = ValidateAccountsetDataIsDel(accountSetID); if (r != 0)//等于0标识存储过程执行发生错误 { result.Result = true; result.IntResult = r; result.Entity.ID = accountSetID; result.Entity.ACCOUNTYEAR = accountSetName; } } return(result); }