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="sourceFilePath"></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;
        }

    }
}