예제 #1
0
 /// <summary>
 /// 以指定编码将字符串写入到文件
 /// </summary>
 /// <param name="fileName"></param>
 /// <param name="source"></param>
 /// <param name="encoding"></param>
 public static void SaveStringToFile(string fileName, string source, Encoding encoding)
 {
     I3DirectoryUtil.CreateDirectoryByFileName(fileName);
     using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
     {
         byte[] data = encoding.GetBytes(source);
         fs.Write(data, 0, data.Length);
     }
 }
예제 #2
0
        /// <summary>
        /// 将文件经过rar压缩后,写入数据库字段中
        /// 写入数据库中的实际是一个rar压缩文件,里面只有一个文件,文件名为 IEFS_Rar.tmp
        /// </summary>
        /// <param name="row"></param>
        /// <param name="fieldName"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static I3MsgInfo WriteFileToDataRowAfterRar(DataRow row, string fieldName, string sourceFileName, bool deleteSource)
        {
            if (!File.Exists(sourceFileName))
            {
                return(new I3MsgInfo(false, "文件不存在!文件名:" + sourceFileName));
            }

            string tmpDir            = I3DirectoryUtil.GetAppTmpDir();
            string tmpRarFileName    = Path.Combine(tmpDir, I3DateTimeUtil.ConvertDateTimeToLongString(DateTime.Now) + ".rar");
            string tmpSourceFileName = Path.Combine(tmpDir, I3DateTimeUtil.ConvertDateTimeToLongString(DateTime.Now));

            tmpSourceFileName = Path.Combine(tmpSourceFileName, "IEFS_Rar.tmp");

            #region 生成临时目录并删除临时文件
            if (!I3FileUtil.CheckFileNotExists(tmpRarFileName))
            {
                return(new I3MsgInfo(false, "临时文件删除失败"));
            }
            I3MsgInfo msg = I3DirectoryUtil.CreateDirctory(tmpDir);
            if (!msg.State)
            {
                return(msg);
            }
            if (!I3FileUtil.CheckFileNotExists(tmpSourceFileName))
            {
                return(new I3MsgInfo(false, "临时源文件删除失败"));
            }
            msg = I3DirectoryUtil.CreateDirectoryByFileName(tmpSourceFileName);
            if (!msg.State)
            {
                return(msg);
            }
            msg = I3FileUtil.MoveFile(sourceFileName, tmpSourceFileName, deleteSource);
            if (!msg.State)
            {
                return(msg);
            }
            #endregion
            try
            {
                #region 压缩
                using (I3Rar rar = new I3Rar())
                {
                    msg = rar.CompressASingleFile(tmpSourceFileName, tmpRarFileName);
                    if (!msg.State)
                    {
                        return(msg);
                    }
                }
                if (!File.Exists(tmpRarFileName))
                {
                    return(new I3MsgInfo(false, "未知原因,压缩失败!文件名:" + tmpRarFileName));
                }
                #endregion

                #region 写入
                using (FileStream stream = File.OpenRead(tmpRarFileName))
                {
                    byte[] buffer = new byte[stream.Length];
                    stream.Position = 0;
                    int readSize = stream.Read(buffer, 0, buffer.Length);
                    if (readSize != buffer.Length)
                    {
                        return(new I3MsgInfo(false, "数据缓冲读取失败!目标长度:" + buffer.Length.ToString() + ",读取长度:" + readSize.ToString()));
                    }

                    try
                    {
                        row.BeginEdit();
                        row[fieldName] = buffer;
                        row.EndEdit();

                        stream.Close();
                        return(I3MsgInfo.Default);
                    }
                    catch (Exception ex)
                    {
                        return(new I3MsgInfo(false, ex.Message, ex));
                    }
                }
                #endregion
            }
            finally
            {
                #region  除临时文件
                I3FileUtil.CheckFileNotExists(tmpRarFileName);
                I3DirectoryUtil.DeleteDirctory(Path.GetDirectoryName(tmpSourceFileName));
                #endregion
            }
        }