Bytepack is a simple Dart utility class for encoding/decoding Map<String, dynamic> objects to bytes, optionally with compression. It also provides methods for converting integers to bytes and back.
BytepackCompresser is how to compress and decompress files using in Dart.
-
Encode/Decode a Map to
Uint8List. -
Encode/Decode a Map with ZLib compression.
-
Convert 4-byte and 8-byte integers to/from
Uint8List. -
Compress a file to a
.compressedformat. -
Decompress a
.compressedfile back to its original form. -
Show progress during compression and decompression.
import 'dart:convert';
import 'dart:typed_data';
import 'package:archive/archive.dart'; // for ZLibEncoder / ZLibDecoder
import 'bytepack.dart'; // your Bytepack classvoid main() {
Map<String, dynamic> data = {
'name': 'Than Coder Win',
'age': 18,
'active': true
};
// Encode to bytes
Uint8List encoded = Bytepack.encodeMap(data);
// Decode back to Map
Map<String, dynamic> decoded = Bytepack.decodeMap(encoded);
print(decoded);
// Output: {name: Than Coder Win, age: 18, active: true}
}
void main() {
Map<String, dynamic> data = {
'name': 'Than Coder Win',
'age': 18,
'active': true
};
// Encode with zlib compression
Uint8List compressed = Bytepack.encodeMapCompress4(data);
// Decode from compressed bytes
Map<String, dynamic> decompressed = Bytepack.decodeMapCompress4(compressed);
print(decompressed);
// Output: {name: Than Coder Win, age: 18, active: true}
}
void main() {
int value4 = 123456;
int value8 = 123456789012345;
// 4-byte integer
Uint8List bytes4 = Bytepack.intToBytes4(value4);
int int4 = Bytepack.bytesToInt4(bytes4);
print(int4); // 123456
// 8-byte integer
Uint8List bytes8 = Bytepack.intToBytes8(value8);
int int8 = Bytepack.bytesToInt8(bytes8);
print(int8); // 123456789012345
}
- Dart SDK >= 3.0
BytepackCompresserclass (custom or package you implemented)- Permissions to read/write files on your system
import 'dart:io';
import 'bytepack_compresser.dart'; // Import your class
void main() async {
await compress();
await deCompress();
}
Future<void> compress() async {
final file = File('/home/than/Music/test.mp4');
final outFile = File('/home/than/Music/test.mp4.compressed');
final outRaf = await outFile.open(mode: FileMode.write);
await BytepackCompresser.compressRandomAccessFile(
file: file,
outputRaf: outRaf,
onProgress: (progress, name) => print('progres: $progress % - name: $name'),
);
await outRaf.close();
}
Future<void> deCompress() async {
final inpFile = File('/home/than/Music/test.mp4.compressed');
final outFile = File('/home/than/Music/test-decompress.mp4');
final inpRaf = await inpFile.open(mode: FileMode.read);
await BytepackCompresser.decompressFromRandomAccessFile(
inputRaf: inpRaf,
outputFile: outFile,
onProgress: (progress, name) => print('progres: $progress % - name: $name'),
);
await inpRaf.close();
}