SwiftUI fileExporter - save entire folder

Hello everybody,

I am trying to export a folder to the Files App using SwiftUI. I am using the fileExporter view modifier.

This is my code so far.

Code Block
.fileExporter(isPresented: self.$presentExportCSV, document: FolderExport(url: createFolderURL()), contentType: .folder) { (res) in
 }
struct FolderExport: FileDocument {
    
    let url: String
    
    static var readableContentTypes: [UTType] {[.folder]}
    
    init(url: String) {
        self.url = url
    }
    
    init(configuration: ReadConfiguration) throws {
        url = ""
    }
    
    func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {
        
        let file = try! FileWrapper(url: URL(fileURLWithPath: url), options: .immediate)
        return file
    }
}
func createFolderURL() -> String {
        
        var folder: String = ""
        
        DispatchQueue.global(qos: .userInitiated).async {
            
            let fileManager = FileManager.default
            //Get apps document directory
            let path = fileManager.urls(for: .documentDirectory, in: .userDomainMask)
            
            //Create folder with obseravtion data
            let folderName = "Observations Data"
            let dataDirectory = path[0].appendingPathComponent("\(folderName)", isDirectory: true)
            try? fileManager.createDirectory(at: dataDirectory.absoluteURL, withIntermediateDirectories: true, attributes: nil)
            
            //Create folder with all images
            let imagesFolder = "Observation Images"
            let imagesDirectory = dataDirectory.appendingPathComponent("\(imagesFolder)", isDirectory: true)
            try? fileManager.createDirectory(at: imagesDirectory.absoluteURL, withIntermediateDirectories: true, attributes: nil)
            
            for observation in observationList {
                
                let image = UIImage(data: observation.image!)
                
                do {
                    let imageName = observation.id!.description
                    let imageURL = imagesDirectory.appendingPathComponent("\(imageName)" + ".jpeg")
                    try image?.jpegData(compressionQuality: 1.0)?.write(to: imageURL)
                } catch {
                    print(error.localizedDescription)
                }
            }
            
            let csvFileURL = dataDirectory.appendingPathComponent("Observation data.csv")
            let csvFile = createCSVFile()
            
            do {
                try csvFile?.write(to: csvFileURL, atomically: true, encoding: .utf16)
            } catch {
                print(error.localizedDescription)
            }
            
            folder = dataDirectory.description
        }
        
        return folder
    }


When the fileWrapper function is called in the FolderExport struct the app crashes.

I get this error: Error Domain=NSCocoaErrorDomain Code=263 "The item “System” couldn’t be opened because it is too large." UserInfo={NSFilePath=/, NSUnderlyingError=0x2834e2850 {Error Domain=NSPOSIXErrorDomain Code=27 "File too large"}}

I can't figure out what I am doing wrong.

I would really appreciate your help.
SwiftUI fileExporter - save entire folder
 
 
Q