简体   繁体   English

Dart 中是否有用于复制目录的内置函数?

[英]Is there a built-in function to copy a directory in Dart?

是否有内置函数可以复制目录并递归复制 Dart 中的所有文件(和其他目录)?

No, not to my knowledge there isn't.不,据我所知没有。 But Dart supports basic reading and writing of files from directories, so it stands to reason that this could be solved programmatically.但是 Dart 支持从目录中读取和写入文件的基本功能,因此可以通过编程方式解决这个问题。

Check out this gist I found of a tool that would accomplish this process.查看这个要点,我发现了一个可以完成这个过程的工具。

Basically, you would search the directory for files you wanted to copy and perform the copy operation:基本上,您将在目录中搜索要复制的文件并执行复制操作:

newFile.writeAsBytesSync(element.readAsBytesSync());

to all file paths, new Path(element.path);到所有文件路径, new Path(element.path); , in the new Directory(newLocation); ,在new Directory(newLocation); . .

Edit:编辑:

But this is super inefficient, because the whole files has to be read in by the system and wrote back out to a file.但这是非常低效的,因为整个文件必须由系统读入并写回文件。 You could just use a shell process spawned by Dart to take care of the process for you:您可以只使用 Dart 生成的 shell 进程来为您处理该进程:

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

Found https://pub.dev/documentation/io/latest/io/copyPath.html (or the sync version of same) which seems to be working for me.找到似乎对我有用的https://pub.dev/documentation/io/latest/io/copyPath.html (或相同的同步版本)。 It's part of the io package https://pub.dev/documentation/io/latest/io/io-library.html available at https://pub.dev/packages/io .它是 io 包https://pub.dev/documentation/io/latest/io/io-library.html的一部分,可在https://pub.dev/packages/io获得。

It does the equivalent of cp -R <from> <to> .它相当于cp -R <from> <to>

Thank you James, wrote a quick function for it, but did it an alternative way.谢谢詹姆斯,为它写了一个快速函数,但是用另一种方式做了。 I'm unsure if this way would be anymore efficient 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) would be to return a stream of files in 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;
}

I came across the same problem today.我今天遇到了同样的问题。 Turns out the io package from pub.dev solve this in a clean api: copyPath or copyPathSync结果来自 pub.dev 的 io 包在干净的 api 中解决了这个问题:copyPath 或 copyPathSync

https://pub.dev/documentation/io/latest/io/copyPath.html https://pub.dev/documentation/io/latest/io/copyPath.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM