在flutter的应用程序文档目录中创建目录的问题

时间:2019-07-20 01:24:19

标签: flutter dart

我正在开发一个应用程序,该应用程序可以从Internet下载zip文件,然后将该文件解压缩到名为maths ..的文件夹中。 我用来创建文件夹的方式:

  _initDir() async {
    if (_dir == null) {
      _dir = (await getApplicationDocumentsDirectory()).path;
    }
    var dr = Directory(_dir + '/maths')
      ..create(recursive: true);
    print(dr.path);
  }

用于解压缩文件的代码

 unzipAndSave(var zippedFile) async {
    var bytes = zippedFile.readAsBytesSync();
    var archive = ZipDecoder().decodeBytes(bytes);
    for (var file in archive) {
      var fileName = '$_dir/maths/${file.name}';
      if (file.isFile) {
        List<int> data = file.content;
        File(fileName)
          ..createSync(recursive: true)
          ..writeAsBytesSync(data);
      }
 }

我得到的错误:

FileSystemException (FileSystemException: Creation failed, path = '/data/user/0/com.example.my_app/app_flutter/maths' (OS Error: Not a directory, errno = 20))

1 个答案:

答案 0 :(得分:0)

这就是我在应用程序中用来解压缩epub文件的

    Future<String> decodeEpubFile() async {
    String filename = "myUnzippedFile.zip";
    final dir = (await getApplicationDocumentsDirectory()).path + '/maths';
    String loc_dir = '$dir';
    String filePath = '$dir/modules/$filename';
    try {
      var targetFile = new File(filePath);
      List<int> bytes = await targetFile.readAsBytes();
      if (bytes != null) {
        print("FILE FOUND: $filePath");
      } else {
        print("ERROR NO FILE FOUND: $filePath");
        return null;
      }

      Archive archive = new ZipDecoder().decodeBytes(bytes);
      for (ArchiveFile file in archive) {
        String filename = file.name;
        print("Decoding: $filename");
        if (file.isFile) {
          List<int> data = file.content;
          new File('$loc_dir/' + filename)
            ..createSync(recursive: true)
            ..writeAsBytesSync(data);
          // print('Created and written $loc_dir/$filename');
        } else {
          new Directory('$loc_dir/' + filename)..create(recursive: true);
          print('Created and written $loc_dir/$filename');
        }
      }
    } catch (e) {
      print('Error $filename Presenter Not able to decode ePub');
      return null;
    }
    return 'file://$loc_dir/ops/xhtml/contents.html';
  }

过去我还使用以下内容创建目录。

final docDirectory = (await getApplicationDocumentsDirectory()).path;
final ebook_dir = new Directory('$docDirectory/ebook/$moduleName').path;