Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. public static string CreateZipFile(int Guia)
  2. {
  3. // a bool variable that says whether or not the file was created
  4. string isCreated = string.Empty;
  5.  
  6.  
  7. try
  8. {
  9. UniCaribe.Model.Reps.GuiaDeEstudioRepository _Rep = new Model.Reps.GuiaDeEstudioRepository();
  10. UniCaribe.Model.Entities.GuiaDeEstudio guia = new Model.Entities.GuiaDeEstudio();
  11. guia = _Rep.GetById(Guia);
  12.  
  13. string zipFileName = guia.Asignatura.Descripcion;
  14. string ruta = System.Configuration.ConfigurationManager.AppSettings["CarpetaDatos"].ToString();
  15. string rutaFinal = ruta + zipFileName + "\\Guia";
  16.  
  17. //Creando el directorio de la guia
  18. if (!Directory.Exists(ruta + zipFileName))
  19. {
  20. Directory.CreateDirectory(ruta + zipFileName);
  21. }
  22. else
  23. {
  24. //Borrando la guia anterior
  25. Directory.Delete(ruta + zipFileName, true);
  26.  
  27. //Escribiendo la guia nueva
  28. Directory.CreateDirectory(ruta + zipFileName);
  29.  
  30. //Creando la carpeta Guia
  31. Directory.CreateDirectory(rutaFinal);
  32.  
  33. }
  34.  
  35.  
  36. foreach (var uni in guia.Unidades)
  37. {
  38. foreach (var ele in uni.Elementos)
  39. {
  40. ////Buscando imagenes en el contenido
  41. if (ele.Contenido.ToLower().Contains("/guia/elementos/datos/images/"))
  42. {
  43. string contenido = ele.Contenido;
  44.  
  45. string match = "src=(?:\"|\')?(?<imgSrc>[^>]*[^/].(?:jpg|png|gif|bmp|jpge|jpeg))(?:\"|\')?";
  46.  
  47. var abc = System.Text.RegularExpressions.Regex.Split(contenido.ToLower(), match);
  48.  
  49. foreach (var i in abc)
  50. {
  51. if (i.StartsWith("/guia/elementos/datos/images/"))
  52. {
  53. if (File.Exists(HttpContext.Current.Server.MapPath(i)))
  54. {
  55. File.Copy(HttpContext.Current.Server.MapPath(i), rutaFinal + "/" + i.Replace("/guia/elementos/datos/images/", ""), true);
  56. }
  57. }
  58. }
  59.  
  60. }
  61.  
  62.  
  63. if (!string.IsNullOrEmpty(ele.Archivo))
  64. {
  65. //Moviendo Directorios
  66. if (ele.Tipo == 14)
  67. {
  68. string directorio = ele.Archivo.Split('.')[0].ToString();
  69.  
  70. if (System.IO.Directory.Exists(ruta + directorio))
  71. {
  72.  
  73. DirectoryInfo directoryInicio = new DirectoryInfo(ruta + directorio);
  74. DirectoryInfo directoryFinal = new DirectoryInfo(rutaFinal + "\\" + directorio);
  75.  
  76. CopyAll(directoryInicio, directoryFinal);
  77.  
  78. }
  79.  
  80. }
  81. else
  82. {
  83. //Moviendo Archivos
  84. if (File.Exists(ruta + ele.Archivo))
  85. {
  86. if (File.Exists(ruta + ele.Archivo))
  87. {
  88. File.Copy(ruta + ele.Archivo, rutaFinal + "/" + ele.Archivo, true);
  89. }
  90. }
  91.  
  92. }
  93. }
  94.  
  95. }
  96.  
  97.  
  98. ////Buscando imagenes en el contenido de las unidades
  99. if (uni.Contenido.ToLower().Contains("/guia/elementos/datos/images/"))
  100. {
  101. string contenido = uni.Contenido;
  102.  
  103. string match = "src=(?:\"|\')?(?<imgSrc>[^>]*[^/].(?:jpg|png|gif|bmp|jpge|jpeg))(?:\"|\')?";
  104.  
  105. var abc = System.Text.RegularExpressions.Regex.Split(contenido.ToLower(), match);
  106.  
  107. foreach (var i in abc)
  108. {
  109. if (i.StartsWith("/guia/elementos/datos/images/"))
  110. {
  111. if (File.Exists(HttpContext.Current.Server.MapPath(i)))
  112. {
  113. File.Copy(HttpContext.Current.Server.MapPath(i), rutaFinal + "/" + i.Replace("/guia/elementos/datos/images/", ""), true);
  114. }
  115. }
  116. }
  117.  
  118. }
  119. }
  120.  
  121. //Escribiendo la guia
  122. EscribirGuia(rutaFinal + "/", guia.Id);
  123.  
  124.  
  125. //Moviendo el ejecutable
  126. if (System.IO.Directory.Exists(ruta + "GuiaSistema"))
  127. {
  128.  
  129. string[] dir = System.IO.Directory.GetDirectories(ruta + "GuiaSistema");
  130.  
  131. foreach (string dirin in dir)
  132. {
  133. string[] filesin = System.IO.Directory.GetFiles(dirin);
  134.  
  135. if (!Directory.Exists(rutaFinal + "\\" + Path.GetFileName(dirin))) Directory.CreateDirectory(rutaFinal + "\\" + Path.GetFileName(dirin));
  136.  
  137. // Copy the files and overwrite destination files if they already exist.
  138. foreach (string s in filesin)
  139. {
  140. // Use static Path methods to extract only the file name from the path.
  141. string fileName = System.IO.Path.GetFileName(s);
  142. string destFile = System.IO.Path.Combine(rutaFinal + "\\" + Path.GetFileName(dirin), fileName);
  143.  
  144. if (File.Exists(s))
  145. {
  146. System.IO.File.Copy(s, destFile, true);
  147. }
  148. }
  149. }
  150.  
  151. string[] files = System.IO.Directory.GetFiles(ruta + "GuiaSistema");
  152.  
  153. // Copy the files and overwrite destination files if they already exist.
  154. foreach (string s in files)
  155. {
  156. // Use static Path methods to extract only the file name from the path.
  157. string fileName = System.IO.Path.GetFileName(s);
  158. string destFile = string.Empty;
  159.  
  160. if (fileName == "Guia.vbs")
  161. {
  162. destFile = System.IO.Path.Combine(ruta + zipFileName, fileName);
  163. }
  164. else
  165. {
  166. destFile = System.IO.Path.Combine(rutaFinal, fileName);
  167. }
  168.  
  169.  
  170. if (File.Exists(s))
  171. {
  172. System.IO.File.Copy(s, destFile, true);
  173. }
  174. }
  175. }
  176.  
  177. //success!
  178. isCreated = ruta + zipFileName;
  179. }
  180. catch (Exception ex)
  181. {
  182. //failed
  183. isCreated = string.Empty;
  184. //lets throw our error
  185. throw ex;
  186. }
  187.  
  188. //return the creation status
  189. return isCreated;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement