private static void TestDownloadNzbStreaming(NntpClient client, string nzbFileName) { string fullPath = Path.Combine(nzbFileName); string nzbData = File.ReadAllText(fullPath, UsenetEncoding.Default); NzbDocument nzbDocument = NzbParser.Parse(nzbData); string downloadDir = Path.Combine("downloads", nzbFileName); Directory.CreateDirectory(downloadDir); var sw = new Stopwatch(); sw.Restart(); log.LogInformation("Downloading nzb {nzbFileName}", nzbFileName); foreach (NzbFile file in nzbDocument.Files) { log.LogInformation("Downloading file {subject}", file.Subject); foreach (NzbSegment segment in file.Segments) { log.LogInformation("Downloading article {messageId}", segment.MessageId); NntpArticleResponse response = client.Article(segment.MessageId); using (YencStream yencStream = YencStreamDecoder.Decode(response.Article.Body)) { YencHeader header = yencStream.Header; string fileName = Path.Combine(downloadDir, header.FileName); if (!File.Exists(fileName)) { log.LogInformation("Creating file {fileName}", fileName); // create file and pre-allocate disk space for it using (FileStream stream = File.Create(fileName)) { stream.SetLength(header.FileSize); } } log.LogInformation("Writing {size} bytes to file {fileName} at offset {offset}", header.PartSize, fileName, header.PartOffset); using (FileStream stream = File.Open( fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite)) { stream.Seek(header.PartOffset, SeekOrigin.Begin); yencStream.CopyTo(stream); } } } } log.LogInformation("Nzb downloaded in {elapsed}", sw.Elapsed); }
public void ShouldBeEncodedAsPartOfMultiPartFile() { List <string> expectedText = testData .GetEmbeddedFile(@"yenc.multipart.test (1.2).ntx") .ReadAllLines(UsenetEncoding.Default) .Skip(3) .Take(10) .ToList(); byte[] data = testData.GetEmbeddedFile(@"yenc.multipart.test (1.2).dat").ReadAllBytes(); using var stream = new MemoryStream(data); var header = new YencHeader("test (1.2).txt", 120, 10, 1, 2, data.Length, 0); List <string> actualText = YencEncoder.Encode(header, stream).ToList(); Assert.Equal(expectedText, actualText); }