AtsUtils.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.IO.Compression;
  4. using System.Net;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using ICSharpCode.SharpZipLib.Zip;
  9. namespace Top.Api.Util
  10. {
  11. /// <summary>
  12. /// 异步API下载工具类。
  13. /// </summary>
  14. public abstract class AtsUtils
  15. {
  16. private const string CTYPE_OCTET = "application/octet-stream";
  17. private static Regex regex = new Regex("attachment;filename=\"([\\w\\-]+)\"", RegexOptions.Compiled);
  18. /// <summary>
  19. /// 通过HTTP GET方式下载文件到指定的目录。
  20. /// </summary>
  21. /// <param name="url">需要下载的URL</param>
  22. /// <param name="destDir">需要下载到的目录</param>
  23. /// <returns>下载后的文件</returns>
  24. public static string Download(string url, string destDir)
  25. {
  26. string file = null;
  27. try
  28. {
  29. WebUtils wu = new WebUtils();
  30. HttpWebRequest req = wu.GetWebRequest(url, "GET", null);
  31. HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
  32. if (CTYPE_OCTET.Equals(rsp.ContentType))
  33. {
  34. file = Path.Combine(destDir, GetFileName(rsp.Headers["Content-Disposition"]));
  35. using (System.IO.Stream rspStream = rsp.GetResponseStream())
  36. {
  37. int len = 0;
  38. byte[] buf = new byte[8192];
  39. using (FileStream fileStream = new FileStream(file, FileMode.OpenOrCreate))
  40. {
  41. while ((len = rspStream.Read(buf, 0, buf.Length)) > 0)
  42. {
  43. fileStream.Write(buf, 0, len);
  44. }
  45. }
  46. }
  47. }
  48. else
  49. {
  50. throw new TopException(wu.GetResponseAsString(rsp, Encoding.UTF8));
  51. }
  52. }
  53. catch (WebException e)
  54. {
  55. throw new TopException("isv.file-already-download", e.Message);
  56. }
  57. return file;
  58. }
  59. /// <summary>
  60. /// 解压gzip文件到指定的目录,目前只能解压gzip包里面只包含一个文件的压缩包。
  61. /// </summary>
  62. /// <param name="gzipFile">需要解压的gzip文件</param>
  63. /// <param name="destDir">需要解压到的目录(不能和压缩文件在同一个目录)</param>
  64. /// <returns>解压后的文件</returns>
  65. public static string Ungzip(string gzipFile, string destDir)
  66. {
  67. string destFile = Path.Combine(destDir, Path.GetFileName(gzipFile));
  68. using (System.IO.Stream output = File.Create(destFile))
  69. {
  70. using (System.IO.Stream input = new GZipStream(File.Open(gzipFile, FileMode.Open), CompressionMode.Decompress))
  71. {
  72. int size = 0;
  73. byte[] buf = new byte[8192];
  74. while ((size = input.Read(buf, 0, buf.Length)) > 0)
  75. {
  76. output.Write(buf, 0, size);
  77. }
  78. }
  79. }
  80. return destFile;
  81. }
  82. /// <summary>
  83. /// 解压zip文件到指定的目录。
  84. /// </summary>
  85. /// <param name="zipFile">需要解压的zip文件</param>
  86. /// <param name="destDir">需要解压到的目录</param>
  87. /// <returns>解压后的文件列表(不包含文件夹)</returns>
  88. public static List<string> Unzip(string zipFile, string destDir)
  89. {
  90. List<string> files = new List<string>();
  91. using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFile)))
  92. {
  93. ZipEntry theEntry;
  94. while ((theEntry = s.GetNextEntry()) != null)
  95. {
  96. if (theEntry.IsDirectory)
  97. {
  98. Directory.CreateDirectory(Path.Combine(destDir, theEntry.Name));
  99. continue;
  100. }
  101. string fileName = Path.Combine(destDir, theEntry.Name);
  102. using (FileStream streamWriter = File.Create(fileName))
  103. {
  104. int size = 0;
  105. byte[] buf = new byte[8192];
  106. while ((size = s.Read(buf, 0, buf.Length)) > 0)
  107. {
  108. streamWriter.Write(buf, 0, size);
  109. }
  110. }
  111. files.Add(fileName);
  112. }
  113. }
  114. return files;
  115. }
  116. /// <summary>
  117. /// 检查指定文件的md5sum和指定的检验码是否一致。
  118. /// </summary>
  119. /// <param name="fileName">需要检验的文件</param>
  120. /// <param name="checkCode">已知的md5sum检验码</param>
  121. /// <returns>true/false</returns>
  122. public static bool CheckMd5sum(string fileName, string checkCode)
  123. {
  124. using (FileStream stream = new FileStream(fileName, FileMode.Open))
  125. {
  126. MD5 md5 = new MD5CryptoServiceProvider();
  127. byte[] retVal = md5.ComputeHash(stream);
  128. StringBuilder sb = new StringBuilder();
  129. for (int i = 0; i < retVal.Length; i++)
  130. {
  131. sb.Append(retVal[i].ToString("x2"));
  132. }
  133. return sb.ToString().Equals(checkCode);
  134. }
  135. }
  136. private static string GetFileName(string contentDisposition)
  137. {
  138. Match match = regex.Match(contentDisposition);
  139. if (match.Success)
  140. {
  141. return match.Groups[1].ToString();
  142. }
  143. else
  144. {
  145. throw new TopException("Invalid response header format!");
  146. }
  147. }
  148. }
  149. }