brendan-duncan / archive

Dart library to encode and decode various archive and compression formats, such as Zip, Tar, GZip, ZLib, and BZip2.
MIT License
403 stars 140 forks source link

Invalid zip file created with ZipFileEncoder #74

Open shariful2011 opened 5 years ago

shariful2011 commented 5 years ago

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 _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 _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?

brendan-duncan commented 5 years ago

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?

shariful2011 commented 5 years ago

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.

brendan-duncan commented 5 years ago

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

shariful2011 commented 5 years ago

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.