Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter how to create a zip file

Tags:

flutter

How to create a zip file with Flutter? I tried with archive package but it does not have any sample to create a zip file. Please help. I am totally new in Flutter. After creating the zip file with archive package error getting while extracting the zip file.

With archive package this is what I did:

  void _generateItemArchive(ZipFileEncoder encoder, int id) async {
List<Item> _itemList = await DBProvider.db.getAllItem(id);
_itemList.forEach((Item _item) {
  encoder.addFile(File(_item.photo));
});

}

ZipFileEncoder encoder = ZipFileEncoder();
encoder.create(join(documentsDirectory.path, 'product.zip'));

encoder.addFile(File('${documentsDirectory.path}/TestDB.db'));

List<Supplier> _supplierList = await DBProvider.db.getAllSupplier();
_supplierList.forEach((Supplier _supplier) {
  encoder.addFile(File(_supplier.photo));
  _generateItemArchive(encoder, _supplier.id);
});

I am getting an error when I tried to call encoder.close(); I need to add the files from database record.

like image 819
shariful Avatar asked Apr 22 '26 09:04

shariful


2 Answers

Add this in your 'pubspec.yaml' file (from https://pub.dev/packages/archive)

archive: ^3.1.6

Import this in your dart file:

import 'package:archive/archive.dart';

Finally:

Directory appDocDirectory = await getExternalStorageDirectory();
var encoder = ZipFileEncoder();
encoder.create(appDocDirectory.path+"/"+'jay.zip');
encoder.addFile(File(selectedAdharFile));
encoder.addFile(File(selectedIncomeFile));
encoder.close();
like image 53
Jay Gadariya Avatar answered Apr 25 '26 13:04

Jay Gadariya


For example, you need to make zip file with all assets of your project.

pubspec.yaml

dependencies:
  archive: 3.4.10

dart file

import 'dart:io';
import 'package:archive/archive_io.dart';

Future<void> zipFolder() async {
  final Directory inputDir = Directory('./assets');
  final File outputFile = File('./temp/output.zip');

  final ZipFileEncoder zipEncoder = ZipFileEncoder();
  zipEncoder.create(outputFile.path);
  await zipEncoder.addDirectory(inputDir);
  zipEncoder.close();
}
like image 38
Yauheni Prakapenka Avatar answered Apr 25 '26 12:04

Yauheni Prakapenka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!