我有一个解密的epub文件的内存字节,但是我想用来打开这个epub文件的包只接受DART:IO文件对象.

I don't want to create real local file => just for security

有没有办法创建一个"假的"dart:ioFile,简单地包装我在内存中的字节,这样我就可以把这个"假的"File传递给包?

我正在使用vocsy_epub_viewer包打开epub文件

filePath should be a local file filePath should be a local file

import 'package:archive/archive.dart';
import 'package:flutter/material.dart';
import 'package:vocsy_epub_viewer/epub_viewer.dart';
import 'package:file_picker/file_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:encrypt/encrypt.dart' as en;
import 'dart:io';
import 'package:cross_file/cross_file.dart';
import 'package:file/memory.dart';
import 'package:file/file.dart' as F;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Ebook(),
    );
  }
}

class Ebook extends StatefulWidget {
  @override
  State<Ebook> createState() => _EbookState();
}

class _EbookState extends State<Ebook> {
  Directory? _temp;
  String? path;
  @override
  void initState() {
    address();
    super.initState();
  }

  Future decodeEpub(String path) async {
    final bytes = await File(path).readAsBytes();
    Archive archive =
        await ZipDecoder().decodeBytes(bytes, password: '7web', verify: true);
    final data = await archive.first.content;
    final key = en.Key.fromUtf8('qwertyuiopasdfghjklmnbvcxzasdfgh');
    final iv = en.IV.fromLength(16);
    final decrypted = await en.AES(key).decrypt(en.Encrypted(data), iv: iv);
    // XFile file = await XFile.fromData(decrypted,name:'2.epub',path:"${_appDocumentsDirectory!.path}/2.epub" );
    // File file1= await File(file.path).create(recursive: true);
    // File file = MemoryFileSystem().file('${temp!.path}/2.epub')..create(recursive: true)
    //   ..writeAsBytesSync(decrypted);
    File file = await File('${_temp!.path}/2.epub').writeAsBytes(decrypted);  // it must be change ******

    print(file.path);

    return file.path;
  }

  void address() async {
    final temp = await getTemporaryDirectory();
    setState(() {
      _temp = temp;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ElevatedButton(
                onPressed: () async {
                  FilePickerResult? result =
                      await FilePicker.platform.pickFiles();
                  if (result != null) {
                    File file = File(result.files.single.path!);
                    path = await decodeEpub(file.path);
                    print('?= $path , ${file.path}');
                  } else {
                    print('cancel');
                  }
                },
                child: Text('add File')),
            ElevatedButton(
              onPressed: () {
                EpubViewer.setConfig(
                    themeColor: Theme.of(context).primaryColor,
                    identifier: "iosBook",
                    scrollDirection: EpubScrollDirection.ALLDIRECTIONS,
                    allowSharing: true,
                    enableTts: true,
                    nightMode: false);
                EpubViewer.open(path!, lastLocation: EpubLocator());
              },
              child: Text("open"),
            ),
          ],
        ),
      ),
    );
  }
}

推荐答案

为了安全起见,我不想创建真正的本地文件

这实际上不是真的,因为vocsy_epub_viewer背后使用的框架EPUBs are structured as a zip filefolioreader将是"unzip" the file to a temp folder (or one predefined).因此,到最后,它无论如何都不会是安全的.

只需将EPUB从path_provider解压缩到临时文件夹,然后将其删除即可.否则,你将需要一个新的框架来阅读没有这种行为的ePub,或者甚至自己定制vocsy_epub_viewer/folioreader源代码以确保它的安全.

Flutter相关问答推荐

壁炉有序在Flutter

Flutter 翼future 建造者不断开火和重建.它在每次重建时都会生成一个新的随机数--我如何防止这种情况?

如何使页面浏览量达到活动页面所要求的高度?

为什么PUT方法没有发出任何响应?

脚本具有不受支持的MIME类型(';Text/html';).(messaging/failed-service-worker-registration)

为Android生成时出现Flutter 错误:给定资源值的<;COLOR&>无效

为什么FloatingActionButton未显示

Flutter为什么不;没有ListView.iterable()构造函数?

从图标启动器打开时,VSCode未检测到Web设备

在 Flutter 中从 FireStore 获取数据并编译它们需要太多时间

如何从Firebase登录获取用户信息,如电话号码、出生日期和性别

Flutter url_launcher 插件抛出java.lang.IllegalArgumentException:接收器未注册:io.flutter.plugins.urllauncher.WebViewActivity

启动画面中的淡入淡出图像

Elevated 和 Outlined 按钮之间的动画

Flutter Dart 功能弹出错误的小部件?

Flutter/Dart - 从外部类更新状态

底部工作表顶部的一块 Flutter

单元测试中如何比较NaN和NaN是否相等

如何在Flutter中制作带有波浪边框的圆圈来显示图像?

在Flutter 中将数字转换为印度文字样式