【问题标题】:Is there a built-in function to copy a directory in Dart?Dart 中是否有用于复制目录的内置函数?
【发布时间】:2015-01-28 01:28:41
【问题描述】:

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

【问题讨论】:

    标签: directory copy dart


    【解决方案1】:

    不,据我所知没有。但是 Dart 支持从目录中读取和写入文件的基本功能,因此可以通过编程方式解决这个问题。

    查看this gist,我发现了一个可以完成此过程的工具。

    基本上,您会在目录中搜索要复制的文件并执行复制操作:

    newFile.writeAsBytesSync(element.readAsBytesSync());
    

    new Directory(newLocation);中的所有文件路径new Path(element.path);

    编辑:

    但是这样效率非常低,因为整个文件必须由系统读入并写回一个文件。你可以由 Dart 生成的 use a shell process 来为你处理这个过程:

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

    【讨论】:

      【解决方案2】:

      谢谢詹姆斯,为它写了一个快速函数,但是用另一种方式做了。我不确定这种方式是否会更有效?

      /**
       * 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; });
      
      }
      

      编辑:或者!更好的方法(在某些情况下)是返回目录中的文件流:

      /**
       * 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;
      }
      

      【讨论】:

        【解决方案3】:

        找到似乎对我有用的https://pub.dev/documentation/io/latest/io/copyPath.html(或相同的同步版本)。它是 io 包 https://pub.dev/documentation/io/latest/io/io-library.html 的一部分,可在 https://pub.dev/packages/io 获得。

        它相当于cp -R &lt;from&gt; &lt;to&gt;

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-06
          • 1970-01-01
          • 1970-01-01
          • 2018-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多