archive icon indicating copy to clipboard operation
archive copied to clipboard

Invalid zip file created with ZipFileEncoder

Open shariful2011 opened this issue 5 years ago • 4 comments

I tried to create zip file with following code:

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); }); encoder.close();//here exception raised.

void _generateItemArchive(ZipFileEncoder encoder, int id) async { List<Item> _itemList = await DBProvider.db.getAllItem(id); _itemList.forEach((Item _item) { encoder.addFile(File(_item.photo)); }); What I am missing? The zip file created with above code is invalid. Can you please guid?

shariful2011 avatar Jun 02 '19 18:06 shariful2011

I'm not entirely sure what the problem might be. The one bit that I would investigate is _generateItemArchive is marked as async, and the full function doesn't seem to be shown here, so is there something actually asynchronous about it? Maybe use an await when calling it?

brendan-duncan avatar Jun 03 '19 03:06 brendan-duncan

I tried _generateItemArchive to call as await but got an error as following: The await expression can only be used in an asynchronous function. Try marking the function body with either 'async' or 'async*'.dart(await_in_wrong_context) - the method already as aync. _generateItemArchive is async as it uses Sqflite package for SQLite.

Problem is not _generateItemArchive, I tried with only encoder.addFile(File('${documentsDirectory.path}/TestDB.db')); with same result as invalid zip file for test purpose.

shariful2011 avatar Jun 03 '19 05:06 shariful2011

Ok, throws out that theory :-). You said there's an exception thrown from encoder.close(). What exception is that?

brendan-duncan avatar Jun 03 '19 06:06 brendan-duncan

I think I found a clue, let me dig more and provide you details. You are right about the problem is in the async method. As my app data flow has changed a little bit I have to create one more level for an item to get its photo.

shariful2011 avatar Jun 03 '19 06:06 shariful2011