Skip to content

ThanCoder/bytepack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bytepack

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.

Features

  • 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 .compressed format.

  • Decompress a .compressed file back to its original form.

  • Show progress during compression and decompression.

Usage

Import

import 'dart:convert';
import 'dart:typed_data';
import 'package:archive/archive.dart'; // for ZLibEncoder / ZLibDecoder
import 'bytepack.dart'; // your Bytepack class

Encode and Decode a Map

void 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}
}

Encode and Decode a Map with Compression

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}
}

Convert Integer to Bytes and Back

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
}

Bytepack Compressor Example

Requirements

  • Dart SDK >= 3.0
  • BytepackCompresser class (custom or package you implemented)
  • Permissions to read/write files on your system

Compress a File

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();
}

About

BytepackCompresser is a Dart utility for efficiently encoding Map<String, dynamic> objects to bytes and compressing files with ZLib, supporting both data serialization and file compression in one package.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages