Flutter Archive Plugin returns data in Uint8List format of my XML file

792

package:archive is a pure Dart package to handle zip and gzip compression. If you know that the zip file contains UTF-8 encoded text files you can decompress each file in memory and then convert the decompressed contents to characters using a character codec - presumably the UTF-8 one, especially as this is the most frequent character encoding of XML.

As the package is pure Dart you can use it from a pure Dart program, which may be easier to test than on your phone. (Incidentally, also included in the package is the dart:io based version, which may be more efficient than the pure Dart version as long as you aren't targetting the web.)

Here's a working snippet that reads a zip file containing text files, prints the file names and the first few characters of each file.

import 'dart:convert';
import 'dart:io';

import 'package:archive/archive.dart';

void main() async {
  var zipBytes = await File('zip1.zip').readAsBytes();
  Archive archive = ZipDecoder().decodeBytes(zipBytes);

  for (var archiveFile in archive) {
    print(archiveFile.name);
    var content = archiveFile.content;
    print(utf8.decode(content).substring(0, 10));
  }
}
Share:
792
Admin
Author by

Admin

Updated on December 16, 2022

Comments

  • Admin
    Admin over 1 year

    Flutter Archive Plugin returns data in Uint8List format of my XML file. How can i convert the List data into String so that i can parse the XML in the app

    Here is the piece of code i wrote

       unarchiveAndSave(var zippedFile,String filepath) async {
    //    String _dir = (await 
     getApplicationSupportDirectory()).path+"/SampleFolder";
    List<int> bytes = await zippedFile.readAsBytesSync();
    if (bytes != null) {
      print("FILE FOUND: $_path");
    } 
    else {
      print("ERROR NO FILE FOUND: $_path");
      return null;
    }
    Archive archive = new ZipDecoder().decodeBytes(bytes);
    for (ArchiveFile file in archive) {
      String filename = file.name;
    
      Uint8List fileContent = file.content;
    
    
      print("File Content ::: $fileContent");
    
      if (file.isFile) {
        List<int> data = file.content;
    
        print(data);
    
        /*File contentFile = new File('$filepath/'+filename)
          ..createSync(recursive: true)
          ..writeAsBytesSync(bytes);*/
    
      }
    
      else {
        new Directory('$filepath/'+filename)
          ..create(recursive: true);
        print('Created and written $filepath/$filename');
      }
    
      }
    }
    

    here is the data i'm getting [45, 246, 8, 26, 241, 251, 150, 86, 156, 194, 16, 162, 231, 138, 40, 95, 178, 209, 62, 79, 136, 196, 26, 234, 97, 105, 44, 50, 93, 141, 26, 111, 253, 199, 19, 255, 250, 57, 105, 27, 114, 86, 164, 144, 98, 101, 84, 114, 192, 236, 19, 187, 225, 186, 188, 91, 152, 187, 20, 228, 69, 23, 219, 34, 51, 85, 241, 124, 230, 224, 36, 7, 56, 5, 235, 172, 34, 199, 63, 108, 69, 239, 161, 52, 11, 56, 138, 246, 57, 218, 93, 147, 89, 85, 252, 26, 45, 117, 76, 50, 73, 76, 170, 38, 91, 122, 97, 151, 175, 57, 135, 30, 30, 115, 224, 106, 4, 86, 164, 118, 190, 48, 118, 211, 104, 194, 96, 229, 57, 173, 22, 230, 143, 217, 204, 82, 177, 201, 224, 121, 136, 47, 147, 217, 217, 111, 60, 143, 133, 79, 162, 255, 71, 127, 162, 155, 20, 43, 144, 5, 116, 215, 179, 97, 7, 193, 24, 234, 193, 235, 181, 115, 193, 150, 29, 83, 232, 48, 8, 4, 84, 214, 255, 177, 230, 99, 64, 19, 39, 78, 127, 59, 1, 138, 158, 152, 104, 235, 216, 69, 68, 133, 23, 15, 99, 162, 29, 127, 253, 56, 180, 231, 32, 77, 93, 188, 2, 152, 248, 157, 46, 47, 72, 161, 153, 168, 8

  • Admin
    Admin over 4 years
    Code is working fine for .txt files but it is not working for xml files. it's returning the following exception [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: FormatException: Bad UTF-8 encoding 0xf6 (at offset 1) #0 _Utf8Decoder.convert (dart:convert/utf.dart:530:13) #1 Utf8Decoder.convert (dart:convert/utf.dart:327:13) #2 Utf8Codec.decode (dart:convert/utf.dart:59:56)
  • Admin
    Admin over 4 years
    the problem is because i was trying to decompress the password protected file, So it returns some illegal characters. So how can i check with the zip file password is valid or not.