@@ -54,27 +54,37 @@ public class LlmMarkdownExporter(bool branded = false) : IMarkdownExporter
5454
5555 public async ValueTask < bool > FinishExportAsync ( IDirectoryInfo outputFolder , Cancel ctx )
5656 {
57+ var fs = outputFolder . FileSystem ;
58+ if ( ! outputFolder . Exists )
59+ outputFolder . Create ( ) ;
60+
5761 var outputDirectory = outputFolder . FullName ;
58- var zipPath = Path . Join ( outputDirectory , "llm.zip" ) ;
62+ var zipPath = fs . Path . Join ( outputDirectory , "llm.zip" ) ;
5963
6064 // Create the llms.txt file; omit Elastic boilerplate for branded builds
61- var llmsTxt = Path . Join ( outputDirectory , "llms.txt" ) ;
62- await outputFolder . FileSystem . File . WriteAllTextAsync ( llmsTxt , branded ? string . Empty : LlmsTxtTemplate , ctx ) ;
63-
64- using var zip = ZipFile . Open ( zipPath , ZipArchiveMode . Create ) ;
65- var llmsTxtRelativePath = Path . GetRelativePath ( outputDirectory , llmsTxt ) ;
66- _ = zip . CreateEntryFromFile ( llmsTxt , llmsTxtRelativePath ) ;
65+ var llmsTxt = fs . FileInfo . New ( fs . Path . Join ( outputDirectory , "llms.txt" ) ) ;
66+ await fs . File . WriteAllTextAsync ( llmsTxt . FullName , branded ? string . Empty : LlmsTxtTemplate , ctx ) ;
6767
68- var markdownFiles = Directory . GetFiles ( outputDirectory , "*.md" , SearchOption . AllDirectories ) ;
68+ await using var zipStream = fs . File . Create ( zipPath ) ;
69+ using var zip = new ZipArchive ( zipStream , ZipArchiveMode . Create ) ;
70+ await AddFileAsync ( zip , llmsTxt , llmsTxt . Name , ctx ) ;
6971
70- foreach ( var file in markdownFiles )
72+ foreach ( var file in outputFolder . EnumerateFiles ( "*.md" , SearchOption . AllDirectories ) )
7173 {
72- var relativePath = Path . GetRelativePath ( outputDirectory , file ) ;
73- _ = zip . CreateEntryFromFile ( file , relativePath ) ;
74+ var relativePath = fs . Path . GetRelativePath ( outputDirectory , file . FullName ) . Replace ( ' \\ ' , '/' ) ;
75+ await AddFileAsync ( zip , file , relativePath , ctx ) ;
7476 }
7577 return true ;
7678 }
7779
80+ private static async Task AddFileAsync ( ZipArchive archive , IFileInfo file , string entryName , Cancel ctx )
81+ {
82+ var entry = archive . CreateEntry ( entryName , CompressionLevel . Optimal ) ;
83+ await using var entryStream = entry . Open ( ) ;
84+ await using var sourceStream = file . FileSystem . File . OpenRead ( file . FullName ) ;
85+ await sourceStream . CopyToAsync ( entryStream , ctx ) ;
86+ }
87+
7888 public async ValueTask < bool > ExportAsync ( MarkdownExportFileContext fileContext , Cancel ctx )
7989 {
8090 var fs = fileContext . BuildContext . WriteFileSystem ;
0 commit comments