/// <summary> /// 执行批量操作 /// </summary> /// <param name="connection">数据连接串</param> /// <returns></returns> public I3MsgInfo BatchExecute(string connection) { this.insertIDList.Clear(); Type conType = assembly.GetType("Oracle.DataAccess.OracleConnection"); IDbConnection sqlCon = Activator.CreateInstance(conType, connection) as IDbConnection; sqlCon.Open(); using (IDbTransaction sqlTrans = sqlCon.BeginTransaction()) { try { I3MsgInfo msgInfo = BatchExecute(sqlTrans); sqlTrans.Commit(); return(msgInfo); } catch (Exception ex) { //回滚数据库事务 sqlTrans.Rollback(); //if (LogHelper.IsInfoEnable) // LogHelper.Info(string.Concat("BatchExecute执行失败:", ex.Message)); return(new I3MsgInfo(false, ex.Message)); } finally { sqlCon.Close(); } } }
/// <summary> /// 压缩一个数据集到文件 /// </summary> /// <param name="dataSet"></param> /// <param name="destFileName"></param> /// <param name="cry"></param> /// <param name="cryKey"></param> /// <returns></returns> public I3MsgInfo CompressADataSet(DataSet dataSet, string destFileName, bool cry, string cryKey) { #region 初始化临时文件变量 string tmpDir = I3DirectoryUtil.GetAppTmpTmpDir(); string tmpDataSetFile = Path.Combine(tmpDir, "DataSet.tmp"); string tmpRarFile = Path.Combine(tmpDir, I3DateTimeUtil.ConvertDateTimeToLongString(DateTime.Now) + ".tmp"); if ((!I3DirectoryUtil.CreateDirctory(tmpDir).State) || (!I3FileUtil.CheckFileNotExists(tmpDataSetFile)) || (!I3FileUtil.CheckFileNotExists(tmpRarFile))) { return(new I3MsgInfo(false, "")); } #endregion try { #region 数据集保存到临时文件 try { //保存时要写入架构信息,否则字段类型有可能会更改,如Int32更改为String dataSet.WriteXml(tmpDataSetFile, XmlWriteMode.WriteSchema); } catch (Exception ex) { return(new I3MsgInfo(false, "数据集保存到临时文件失败!文件名:" + tmpDataSetFile + "错误信息:" + ex.Message)); } if (!File.Exists(tmpDataSetFile)) { return(new I3MsgInfo(false, "数据集保存到临时文件失败!文件名:" + tmpDataSetFile)); } #endregion #region 数据集临时文件压缩到临时压缩包 I3MsgInfo msg = this.CompressASingleFile(tmpDataSetFile, tmpRarFile); if (!msg.State) { return(msg); } if (!File.Exists(tmpRarFile)) { return(new I3MsgInfo(false, "数据集临时文件压缩失败!文件名:" + tmpDataSetFile)); } #endregion #region 临时压缩包加密到目标文件 if (cry) { return(I3RijnDaelCry.CryFile(tmpRarFile, destFileName, cryKey)); } else { return(I3FileUtil.MoveFile(tmpRarFile, destFileName, true)); } #endregion } finally { #region 除临时文件 I3DirectoryUtil.DeleteDirctory(tmpDir); #endregion } }
/// <summary> /// 解压单个文件 到 指定文件 /// 注意:单个文件的文件名,要指定在rar文件中的相对路径 /// </summary> /// <param name="sourceRarFileName"></param> /// <param name="sourceFileName"></param> /// <param name="destFileName"></param> /// <returns></returns> public I3MsgInfo UnCompressASingleFile(string sourceRarFileName, string sourceFileName, string destFileName) { #region 先删除目的文件 if (!I3FileUtil.CheckFileNotExists(destFileName)) { return(new I3MsgInfo(false, "")); } #endregion #region 获取临时目录与临时文件名 string tmpDir = I3DirectoryUtil.GetAppTmpTmpDir(); string tmpFileName = Path.Combine(tmpDir, Path.GetFileName(sourceFileName)); #endregion I3MsgInfo msg = I3DirectoryUtil.CreateDirectoryByFileName(tmpFileName); #region 创建临时目录 if (!msg.State) { return(msg); } #endregion try { #region 生成命令行参数 string cmdLine; cmdLine = " E -Y "; if (!string.IsNullOrEmpty(passWord)) { cmdLine = cmdLine + " -p" + I3StringUtil.AppendDoubleQuotes(passWord) + " "; } cmdLine = cmdLine + I3StringUtil.AppendDoubleQuotes(sourceRarFileName) + " " + I3StringUtil.AppendDoubleQuotes(sourceFileName) + " " + I3StringUtil.AppendDoubleQuotes(tmpDir); #endregion //执行 msg = RunCmdLine(cmdLine); if (!msg.State) { return(msg); } //检查临时文件 if (!File.Exists(tmpFileName)) { return(new I3MsgInfo(false, "未知错误,临时文件未生成!文件名:" + tmpFileName)); } //移动 return(I3FileUtil.MoveFile(tmpFileName, destFileName, true)); } finally { I3DirectoryUtil.DeleteDirctory(tmpDir); } }
/// <summary> /// 初始化 /// </summary> /// <param name="fileName"></param> /// <param name="type"></param> public void Init(string fileName, Type type) { this.fileName = fileName; I3MsgInfo msg = I3DirectoryUtil.CreateDirectoryByFileName(fileName); if (!msg.State) { throw msg.ExpMsg == null ? new Exception(msg.Message) : msg.ExpMsg; } this.type = type; }
public I3MsgInfo BatchExecute(string connection) { using (SqlConnection sqlCon = I3DBUtil.CreateAndOpenDbConnection(connection) as SqlConnection) { using (SqlTransaction sqlTrans = sqlCon.BeginTransaction()) { I3MsgInfo msgInfo = BatchExecute(sqlTrans); if (msgInfo.State) { sqlTrans.Commit(); } else { sqlTrans.Rollback(); } return(msgInfo); } } }
public I3MsgInfo UnAllCompressToADir(string sourceRarFileName, string destDir) { I3MsgInfo msg = I3DirectoryUtil.CreateDirctory(destDir); if (!msg.State) { return(msg); } string cmdLine = " X -Y -C- "; if (!string.IsNullOrEmpty(passWord)) { cmdLine = cmdLine + " -p" + I3StringUtil.AppendDoubleQuotes(passWord) + " "; } cmdLine = cmdLine + I3StringUtil.AppendDoubleQuotes(sourceRarFileName) + " " + I3StringUtil.AppendDoubleQuotes(destDir); return(RunCmdLine(cmdLine)); }
/// <summary> /// 压缩一个目录下的所有文件,包含子目录中的文件 /// aFilter为空时,压缩所有文件 指定文件时,其值类似于 .txt /// </summary> /// <param name="aDir"></param> /// <param name="aDestFileName"></param> /// <returns></returns> public I3MsgInfo CompressADir(string aDir, string aDestFileName) { if (!I3FileUtil.CheckFileNotExists(aDestFileName)) { return(new I3MsgInfo(false, "")); } FileName = aDestFileName; AddDirFileToFileList(aDir, aDir); I3MsgInfo msg = Flush(); if (!msg.State) { return(msg); } FileName = aDestFileName; return(new I3MsgInfo(fileExists && fileOK, "")); }
/// <summary> /// 压缩单个文件 /// </summary> /// <param name="aFileName"></param> /// <param name="aDestFileName"></param> /// <returns></returns> public I3MsgInfo CompressASingleFile(string aFileName, string aDestFileName) { if (!I3FileUtil.CheckFileNotExists(aDestFileName)) { return(new I3MsgInfo(false, "")); } FileName = aDestFileName; I3SharpZipFileInfo fileInfo = new I3SharpZipFileInfo(Path.GetFileName(aFileName), aFileName, I3FileUtil.GetFileSize(aFileName), I3SZFileInfoMode.szimNew); fileInfoList.Add(fileInfo); I3MsgInfo msg = Flush(); if (!msg.State) { return(msg); } FileName = aDestFileName; return(new I3MsgInfo(fileExists && fileOK, "")); }
/// <summary> /// 根据fileInfoList的设置,生成新的压缩包文件 /// /// 错误处理:IEFS_Error.LastErrorMessage /// /// </summary> /// <returns></returns> public I3MsgInfo Flush() { #region 定义变量 string message; ZipOutputStream outputStream = null; int addFileCount = GetAddFileCount() + GetNormalFileCount(); long totalAddFileSize = GetTotalAddFileSize() + GetTotalNormalFileSize(); //所有需要压缩的文件的字节数 long passFileSize = 0; //已经压缩的文件的字节数 long totalPosition = 0; //总共压缩到的字节数 #endregion #region 检查临时文件与临时目录 string tmpFile = fileName + ".tmpsharpzip"; if (!I3FileUtil.CheckFileNotExists(tmpFile)) { return(new I3MsgInfo(false, "")); } string tmpDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, I3DateTimeUtil.ConvertDateTimeToLongString(DateTime.Now)); I3MsgInfo msg = I3DirectoryUtil.CheckAndClearDirctory(tmpDir); if (!msg.State) { return(msg); } #endregion try { #region 解压所有不需要替换或者删除的文件到临时目录 msg = UnCompressAllFile(tmpDir, true); if (!msg.State) { return(msg); } #endregion #region 开始压缩状态为normal、New、Change三种状态的文件到临时文件中 try { #region 压缩 outputStream = new ZipOutputStream(File.Create(tmpFile)); outputStream.SetLevel(zipLevel); if (!string.IsNullOrEmpty(passWord)) { outputStream.Password = passWord; } int fileindex = 0; foreach (I3SharpZipFileInfo info in fileInfoList) { #region 判断文件是否要参与压缩 if (info.Mode == I3SZFileInfoMode.szimDelete) { continue; } #endregion #region 发送文件个数进度信息 message = "正在压缩文件\"" + info.FileName + "\""; fileindex++; OnMutiFileProcessReport(addFileCount, fileindex, message); #endregion #region 写入ZipEntry //outputStream.PutNextEntry(GetEntry(info.FileName, info.FullName)); //自己在ZipEntry中写入CRC信息,读取时会报Eof of Header的错误 //在网上查找资料后发现,新版SharpZip.dll已经更改,不再需要自己写入CRC等信息 outputStream.PutNextEntry(new ZipEntry(info.FileName)); info.Mode = I3SZFileInfoMode.szimNormal; if (info.FileSize == 0) { continue; } #endregion FileStream sourceFileStream = null; try { #region 将文件写入压缩流 sourceFileStream = File.OpenRead(info.FullName); sourceFileStream.Position = 0; //IEFS_Stream.WriteStreamToStream(null, sourceFileStream, outputStream, blockSize, null); byte[] data = new byte[blockSize]; long longsize = 0; while (true) { int size = sourceFileStream.Read(data, 0, data.Length); if (size > 0) { outputStream.Write(data, 0, size); longsize += size; OnSingleFileProcessReport(info.FileSize, longsize, message); totalPosition = passFileSize + longsize; OnTotalBytesProcessReport(totalAddFileSize, totalPosition, message); } else { break; } } passFileSize += longsize; #endregion } finally { #region 释放单个文件的文件流 if (sourceFileStream != null) { sourceFileStream.Close(); } #endregion } } #endregion } #region 错误处理 catch (Exception ex) { return(new I3MsgInfo(false, ex.Message, ex)); } #endregion #region 释放变量 删除临时目录 finally { if (outputStream != null) { outputStream.Finish(); outputStream.Close(); } I3DirectoryUtil.DeleteDirctory(tmpDir); } #endregion #endregion #region 替换原始文件 if (!I3FileUtil.CheckFileNotExists(fileName)) { return(new I3MsgInfo(false, "")); } return(I3FileUtil.MoveFile(tmpFile, fileName, true)); #endregion } finally { I3DirectoryUtil.DeleteDirctory(tmpDir); I3FileUtil.CheckFileNotExists(tmpFile); } }
/// <summary> /// 根据文件信息从压缩包中获取单个文件,需要指定FullName /// </summary> /// <param name="aFileInfo"></param> /// <returns></returns> public I3MsgInfo UnCompressSingleFile(I3SharpZipFileInfo aFileInfo) { #region 检查文件名与上级目录 if (string.IsNullOrEmpty(aFileInfo.FullName)) { return(new I3MsgInfo(false, "未设置目的路径!")); } I3MsgInfo msg = I3DirectoryUtil.CreateDirectoryByFileName(aFileInfo.FullName); if (!msg.State) { return(msg); } string message = "正在解压文件" + aFileInfo.FileName + "到" + aFileInfo.FullName; #endregion #region 检查是否是目录 文件大小为0且文件名最后一个字符是/表示是目录 if ((aFileInfo.FileSize.Equals(0) && (I3StringUtil.SubString(aFileInfo.FileName, aFileInfo.FileName.Length - 1).Equals("/")))) { return(I3MsgInfo.Default); } #endregion bool result = false; ZipInputStream inputStream = null; try { inputStream = new ZipInputStream(File.OpenRead(fileName)); if (!string.IsNullOrEmpty(passWord)) { inputStream.Password = passWord; } ZipEntry theEntry; while ((theEntry = inputStream.GetNextEntry()) != null) { #region 判断文件名是否为要解压的文件 if (!theEntry.Name.Equals(aFileInfo.FileName)) { continue; } #endregion #region 定义单个文件的变量 FileStream streamWriter = null; #endregion try { #region 解压 streamWriter = File.Create(aFileInfo.FullName); byte[] data = new byte[blockSize]; long longsize = 0; while (true) { int size = inputStream.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); longsize += size; OnSingleFileProcessReport(aFileInfo.FileSize, longsize, message); } else { break; } } #endregion } finally { #region 释放单个文件的变量 if (streamWriter != null) { streamWriter.Close(); } #endregion } result = true; } } #region 错误处理 catch (Exception ex) { return(new I3MsgInfo(false, ex.Message, ex)); } #endregion #region 释放inputStream finally { if (inputStream != null) { inputStream.Close(); } } #endregion return(new I3MsgInfo(result, "")); }