I have a Bzip2 file I want to decompress in Flutter But I did not find a way Of course, this is a package https://pub.flutter-io.cn/packages/archive/versions/2.0.11 But there is no example for Bzip2 Thank you for helping me
Asked
Active
Viewed 285 times
1 Answers
1
You are right. The archive package does not document how one can decode bzip2 archive. But fortunately its quite simple.
As of wikipedia, Bzip2 can only compress single files. If you know which file is encoded it is easy to extract the file.
For me, I had a compressed file that was called test.dem.bz2
and i expected a file called test.dem
Unzipping
import 'package:archive/archive_io.dart';
import 'package:archive/archive.dart';
final bytes = File('test.dem.bz2').readAsBytesSync();
final List<int> archive = BZip2Decoder().decodeBytes(bytes);
The result of the decodeBytes
operation is a List of unsigned 8 bit values. These represent the extracted files.
Creating the file extracted file
if the function in which you are executing the code is async use
await var file = File('test.dem')..create();
await file.writeAsBytes(archive);
otherwise use
var file = File('test.dem')..createSync();
file.writeAsBytesSync(archive);

Jens
- 2,592
- 2
- 21
- 41