| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 | namespace OASystem.API.OAMethodLib{    public class IOOperatorHelper    {        /// <summary>        /// 写文件(覆盖)        /// </summary>        /// <param name="content"></param>        /// <param name="dir"></param>        /// <param name="fileName"></param>        /// <returns></returns>        public string Write_CoverFile(string content, string dir, string fileName)        {            if (!System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir);   //该路径不存在时,在当前文件目录下创建文件夹"导出.."            //不存在该文件时先创建            String filePath = dir + fileName;            System.IO.StreamWriter file1 = new System.IO.StreamWriter(filePath, false);     //文件已覆盖方式添加内容            file1.Write(content);                                                              //保存数据到文件            file1.Close();                                                                  //关闭文件            file1.Dispose();                                                                //释放对象            return filePath;        }        public string Read(string filePath)        {            string result = string.Empty;            try            {                // 读取文本文件                using (StreamReader sr = new StreamReader(filePath))                {                    string line;                    // ReadLine()一行一行的循环读取                    //当然可以直接ReadToEnd()读到最后                    while ((line = sr.ReadLine()) != null)                    {                        result += line;                    }                }            }            catch (Exception e)            {            }            result = JsonConvert.DeserializeObject<string>(result);            return result;        }        /// <summary>        /// 移动文件        /// </summary>        /// <param name="sourceFileFullPath"></param>        /// <param name="recycleBinPath"></param>        /// <returns></returns>        public void MoveFile(string sourceFileFullPath, string recycleBinPath)        {            //移动文件            if (!string.IsNullOrEmpty(sourceFileFullPath) && System.IO.File.Exists(sourceFileFullPath))            {                string fileName = Path.GetFileName(sourceFileFullPath);                System.IO.File.Move(sourceFileFullPath, recycleBinPath + fileName);            }        }        /// <summary>        /// 将byte[]数组保存成文件        /// </summary>        /// <param name="byteArray">byte[]数组</param>        /// <param name="fileName">保存至硬盘的文件路径</param>        /// <returns></returns>        public bool ByteToFile(byte[] byteArray, string fileName)        {            bool result = false;            try            {                using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))                {                    fs.Write(byteArray, 0, byteArray.Length);                    result = true;                }            }            catch            {                result = false;            }            return result;        }        /// <summary>        /// ZipStream 压缩        /// </summary>        /// <param name="streams">Dictionary(string, Stream) 文件名和Stream</param>        /// <returns></returns>        public byte[] ConvertZipStream(Dictionary<string, Stream> streams)        {            byte[] buffer = new byte[6500];            MemoryStream returnStream = new MemoryStream();            var zipMs = new MemoryStream();            using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(zipMs))            {                zipStream.SetLevel(9);//设置 压缩等级 (9级 500KB 压缩成了96KB)                foreach (var kv in streams)                {                    string fileName = kv.Key;                    using (var streamInput = kv.Value)                    {                        ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fileName);                        zipEntry.IsUnicodeText = true;                        zipStream.PutNextEntry(zipEntry);                        while (true)                        {                            var readCount = streamInput.Read(buffer, 0, buffer.Length);                            if (readCount > 0)                            {                                zipStream.Write(buffer, 0, readCount);                            }                            else                            {                                break;                            }                        }                        zipStream.Flush();                    }                }                zipStream.Finish();                zipMs.Position = 0;                zipMs.CopyTo(returnStream, 5600);            }            returnStream.Position = 0;            //Stream转Byte[]            byte[] returnBytes = new byte[returnStream.Length];            returnStream.Read(returnBytes, 0, returnBytes.Length);            returnStream.Seek(0, SeekOrigin.Begin);            return returnBytes;        }    }}
 |