IOOperatorHelper.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using static StackExchange.Redis.Role;
  2. namespace OASystem.API.OAMethodLib
  3. {
  4. public class IOOperatorHelper
  5. {
  6. /// <summary>
  7. /// 写文件(覆盖)
  8. /// </summary>
  9. /// <param name="content"></param>
  10. /// <param name="dir"></param>
  11. /// <param name="fileName"></param>
  12. /// <returns></returns>
  13. public string Write_CoverFile(string content, string dir, string fileName)
  14. {
  15. if (!System.IO.Directory.Exists(dir)) System.IO.Directory.CreateDirectory(dir); //该路径不存在时,在当前文件目录下创建文件夹"导出.."
  16. //不存在该文件时先创建
  17. String filePath = dir + fileName;
  18. System.IO.StreamWriter file1 = new System.IO.StreamWriter(filePath, false); //文件已覆盖方式添加内容
  19. file1.Write(content); //保存数据到文件
  20. file1.Close(); //关闭文件
  21. file1.Dispose(); //释放对象
  22. return filePath;
  23. }
  24. public string Read(string filePath)
  25. {
  26. string result = string.Empty;
  27. try
  28. {
  29. // 读取文本文件
  30. using (StreamReader sr = new StreamReader(filePath))
  31. {
  32. string line;
  33. // ReadLine()一行一行的循环读取
  34. //当然可以直接ReadToEnd()读到最后
  35. while ((line = sr.ReadLine()) != null)
  36. {
  37. result += line;
  38. }
  39. }
  40. }
  41. catch (Exception e)
  42. {
  43. }
  44. result = JsonConvert.DeserializeObject<string>(result);
  45. return result;
  46. }
  47. /// <summary>
  48. /// 移动文件
  49. /// </summary>
  50. /// <param name="sourceFilePath"></param>
  51. /// <param name="RecycleBinPath"></param>
  52. /// <returns></returns>
  53. public void MoveFile(string sourceFileFullPath, string RecycleBinPath)
  54. {
  55. //移动文件
  56. if (!string.IsNullOrEmpty(sourceFileFullPath) && System.IO.File.Exists(sourceFileFullPath))
  57. {
  58. string fileName = Path.GetFileName(sourceFileFullPath);
  59. System.IO.File.Move(sourceFileFullPath, RecycleBinPath + fileName);
  60. }
  61. }
  62. /// <summary>
  63. /// 将byte[]数组保存成文件
  64. /// </summary>
  65. /// <param name="byteArray">byte[]数组</param>
  66. /// <param name="fileName">保存至硬盘的文件路径</param>
  67. /// <returns></returns>
  68. public bool ByteToFile(byte[] byteArray, string fileName)
  69. {
  70. bool result = false;
  71. try
  72. {
  73. using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write))
  74. {
  75. fs.Write(byteArray, 0, byteArray.Length);
  76. result = true;
  77. }
  78. }
  79. catch
  80. {
  81. result = false;
  82. }
  83. return result;
  84. }
  85. /// <summary>
  86. /// ZipStream 压缩
  87. /// </summary>
  88. /// <param name="streams">Dictionary(string, Stream) 文件名和Stream</param>
  89. /// <returns></returns>
  90. public byte[] ConvertZipStream(Dictionary<string, Stream> streams)
  91. {
  92. byte[] buffer = new byte[6500];
  93. MemoryStream returnStream = new MemoryStream();
  94. var zipMs = new MemoryStream();
  95. using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(zipMs))
  96. {
  97. zipStream.SetLevel(9);//设置 压缩等级 (9级 500KB 压缩成了96KB)
  98. foreach (var kv in streams)
  99. {
  100. string fileName = kv.Key;
  101. using (var streamInput = kv.Value)
  102. {
  103. ICSharpCode.SharpZipLib.Zip.ZipEntry zipEntry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(fileName);
  104. zipEntry.IsUnicodeText = true;
  105. zipStream.PutNextEntry(zipEntry);
  106. while (true)
  107. {
  108. var readCount = streamInput.Read(buffer, 0, buffer.Length);
  109. if (readCount > 0)
  110. {
  111. zipStream.Write(buffer, 0, readCount);
  112. }
  113. else
  114. {
  115. break;
  116. }
  117. }
  118. zipStream.Flush();
  119. }
  120. }
  121. zipStream.Finish();
  122. zipMs.Position = 0;
  123. zipMs.CopyTo(returnStream, 5600);
  124. }
  125. returnStream.Position = 0;
  126. //Stream转Byte[]
  127. byte[] returnBytes = new byte[returnStream.Length];
  128. returnStream.Read(returnBytes, 0, returnBytes.Length);
  129. returnStream.Seek(0, SeekOrigin.Begin);
  130. return returnBytes;
  131. }
  132. }
  133. }