public override void Execute( Job job, Action<string> updateStatus, IRepository repositoryService, IJobManager jobManager) { foreach(var file in job.Files.OfType<CreateFile>()) { updateStatus(string.Format("Downloading file: \"{0}\"...", file.Name)); using(var stream = repositoryService.Download(file.Link.Id)) { using(var fstream = new FileStream( Path.Combine(Directory.GetCurrentDirectory(), string.Format("{0}.data", file.Name)), FileMode.Create)) { var buff = new byte[256]; while(true) { var isCompleted = stream.Read(buff, 0, buff.Length) < buff.Length; fstream.Write(buff, 0, buff.Length); if(isCompleted) break; } } } } }
private static string ReadFile(IRepository repository, Guid id, int bufferSize) { using (Perfomance.Trace("Download with buffer size {0} Kbytes", bufferSize / 1024).BindToConsole()) { using (var reader = new StreamReader(repository.Download(id), Encoding.Default, false, bufferSize)) { var text = reader.ReadToEnd(); return text; } } }
private string Download(IRepository repository, string path, string targetFile) { var response = repository.Download(path); if (!response.IsSuccessful) { Logger.Error("Failed to download " + path + " due to " + response.ErrorMessage + ". Going on without this download."); return(null); } FileSystem.CreateDirectoryRecursively(Path.GetDirectoryName(targetFile)); using (var stream = new FileStream(targetFile, FileMode.Create)) { stream.Write(response.RawBytes, 0, response.RawBytes.Length); } using (var reader = new FileStream(targetFile, FileMode.Open)) using (var archive = ZipReader.Open(reader)) using (var memory = new MemoryStream()) using (StreamReader stringReader = new StreamReader(memory, Encoding.UTF8)) { try { while (archive.MoveToNextEntry()) { var isPom = archive.Entry.Key.EndsWith("pom.xml"); if (isPom) { archive.WriteEntryTo(memory); memory.Position = 0; return(stringReader.ReadToEnd()); } } } catch (EndOfStreamException) { // Java JAR files are their own ZIP variant and we always get an exception if we try to parse it } } return(null); }