IOOperatorHelper.cs 5.3 KB

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