Is there a built-in function for copying a directory in Dart?

Is there a built-in function to copy a directory and recursively copy all files (and other directories) to Dart?

+6
source share
3 answers

No, I don’t know, no. But Dart supports basic reading and writing files from directories, so it is quite reasonable that this can be solved programmatically.

Mark this meaning. I found a tool that would complete this process.

Basically, you should look in the directory for the files you want to copy and perform a copy operation:

newFile.writeAsBytesSync(element.readAsBytesSync()); 

to all file paths, new Path(element.path); , in new Directory(newLocation); .

Edit:

But this is super inefficient because all files must be read by the system and written back to the file. You could simply use the shell process generated by Dart to take care of the process for you:

 Process.run("cmd", ["/c", "copy", ...]) 
+4
source

Thanks, James, wrote a quick function for it, but did it in an alternative way. I'm not sure if this method will be more effective or not?

 /** * Retrieve all files within a directory */ Future<List<File>> allDirectoryFiles(String directory) { List<File> frameworkFilePaths = []; // Grab all paths in directory return new Directory(directory).list(recursive: true, followLinks: false) .listen((FileSystemEntity entity) { // For each path, if the path leads to a file, then add to array list File file = new File(entity.path); file.exists().then((exists) { if (exists) { frameworkFilePaths.add(file); } }); }).asFuture().then((_) { return frameworkFilePaths; }); } 

Edit: OR! An even better approach (in some situations) is to return the file stream to the directory:

 /** * Directory file stream * * Retrieve all files within a directory as a file stream. */ Stream<File> _directoryFileStream(Directory directory) { StreamController<File> controller; StreamSubscription source; controller = new StreamController<File>( onListen: () { // Grab all paths in directory source = directory.list(recursive: true, followLinks: false).listen((FileSystemEntity entity) { // For each path, if the path leads to a file, then add the file to the stream File file = new File(entity.path); file.exists().then((bool exists) { if (exists) controller.add(file); }); }, onError: () => controller.addError, onDone: () => controller.close ); }, onPause: () { if (source != null) source.pause(); }, onResume: () { if (source != null) source.resume(); }, onCancel: () { if (source != null) source.cancel(); } ); return controller.stream; } 
+2
source

Found https://pub.dev/documentation/io/latest/io/copyPath.html (or a synchronized version of the same) that seems to work for me. This is part of the io https://pub.dev/documentation/io/latest/io/io-library.html package available at https://pub.dev/packages/io .

This is equivalent to cp -R <from> <to> .

+1
source

Source: https://habr.com/ru/post/978871/


All Articles