readFile static method

Future<List<String>> readFile(
  1. String fileName
)

Read the txt file inside the extracted zip file

Implementation

static Future<List<String>> readFile(String fileName) async {
  // If the whatsapp chat is shared with media, the unzipped folder will contain the media files,
  // so we need to read the txt file from the unzipped folder and list all the images and do something
  // with them
  String path = '${(await getTemporaryDirectory()).path}/unzipped/$fileName';
  File file = File(path);
  if (!file.existsSync()) {
    // The expected name depends on the WhatsApp version and phone language,
    // so log what the export actually contains.
    final files = Directory(file.parent.path)
        .listSync()
        .map((f) => f.path.split('/').last)
        .toList();
    Logger.error('Chat file "${Logger.shape(fileName)}" not found in the '
        'export. Files in the export: ${files.map(Logger.shape).toList()}');
  }
  List<String> lines = await file.readAsLines();
  if (lines.isEmpty) Logger.warning('Chat file "$fileName" is empty');
  await _deleteDir(
      Directory("${(await getTemporaryDirectory()).path}/unzipped"));
  return lines;
}