예제 #1
0
        public async Task <IActionResult> DownloadSourceCodesAsZip(int exerciseId)
        {
            if (!IsLector())
            {
                return(Forbid());
            }

            var sourceCodes = await _assignmentService.GetAllSourceCodes(exerciseId);

            using (var memoryStream = new MemoryStream())
            {
                using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
                {
                    foreach (var sourceCode in sourceCodes)
                    {
                        var entry = zipArchive.CreateEntry($@"{sourceCode.UserFullName}\source.txt");
                        using (StreamWriter writer = new StreamWriter(entry.Open()))
                        {
                            await writer.WriteAsync(sourceCode.Source);
                        }
                    }
                }

                memoryStream.Position = 0;

                var result = new FileContentResult(memoryStream.ToArray(), "application/zip")
                {
                    FileDownloadName = $"Exercise_{exerciseId}_sources.zip"
                };
                return(result);
            }
        }