Unable to extract zip in flutter

3,500

Solution 1

The archive package has the extractFileToDisk method which should do what you want.

Solution 2

Never tried to do that in Dart, however i had similar issues with another languages. Usualy the issue appears if you forget to verify what type of file you are handling. file object should have boolean variables file.isFile and file.isDirectory

you can try to follow this example

import 'dart:io';
import 'package:path/path.dart' as p;
import 'package:path/path.dart';
import 'package:archive/archive.dart';
import 'package:archive/archive_io.dart';

var path = p.Context(style: Style.posix);
final String __filename = Platform.script.path.replaceFirst('/', '');
final String __dirname = Directory(__filename).parent.path;

  // read Zip file from disk
  List<int> bytes = File(path.join(__dirname, 'test-a-master.zip')).readAsBytesSync();

     // decode Zip file
  Archive archive = ZipDecoder().decodeBytes(bytes);

     // Extract the contents of Zip compressed archive to disk
  for (ArchiveFile file in archive) {
    String filename = file.name;
    String decodePath = path.join(__dirname, filename);
    if (file.isFile) {
      List<int> data = file.content;
      File(decodePath)
        ..createSync(recursive: true)
        ..writeAsBytesSync(data);
    } else {
      Directory(decodePath)..create(recursive: true);
    }
  }

hope it will help you

Share:
3,500
Farhana Naaz Ansari
Author by

Farhana Naaz Ansari

Farhana... The only way of writing fewer bugs is writing less code.

Updated on November 21, 2022

Comments

  • Farhana Naaz Ansari
    Farhana Naaz Ansari over 1 year

    Downloading zip folder from server and try to extract the zip but the zip has multiple directory. I can download the zip but not able to extract it.Below code is working when zip has multiple image file but in my case has multiple directories and have multiple files.

    String _localZipFileName = 'archive.zip';
    
      Future<File> _downloadFile(String url, String fileName) async {
        //url==http://115.166.142.178:3000/staff/syncdata
        var req = await http.Client().get(Uri.parse(url));
        Log.e("DOWNLOAD DIRECTORY",_dir);
        var file = File('$_dir/$fileName');
        return file.writeAsBytes(req.bodyBytes);
      }
    
      Future<void> _downloadZip(String _zipPath) async {
    setState(() {
      _downloading = true;
    });
    
    Log.e("Zippath",_zipPath);
    _images.clear();
    _tempImages.clear();
    var zippedFile = await _downloadFile(_zipPath, _localZipFileName);
    Log.e("Zippath",zippedFile.path);
    await unarchiveAndSave(zippedFile);
    setState(() {
      _images.addAll(_tempImages);
      _downloading = false;
    });
      }
    
    
    
     unarchiveAndSave(var zippedFile) async {
        var bytes = zippedFile.readAsBytesSync();
        var archive = ZipDecoder().decodeBytes(bytes);
    
    for (var file in archive) {
      var fileName = '$_dir/${file.name}';
      if (file.isCompressed) {
        var outFile = File(fileName);
        Log.e('File:: ' , outFile.path);
       // _tempImages.add('${outFile.path}/productImages');
        outFile = await outFile.create(recursive: true);
        await outFile.writeAsBytes(file.content);
      }
    }
      }
    
  • Farhana Naaz Ansari
    Farhana Naaz Ansari almost 4 years
    ArchiveFile giving the file name of parent zip.
  • Farhana Naaz Ansari
    Farhana Naaz Ansari about 3 years
    i found the issue . the issue was zip containing zip.