Skip to main content

t_binary 2.0.0 copy "t_binary: ^2.0.0" to clipboard
t_binary: ^2.0.0 copied to clipboard

TBinary is a Dart-based library designed to create a high-performance custom binary file format, capable of handling header metadata, an optional cover, multiple file entries, and large file data effi [...]

example/t_binary_example.dart

import 'dart:convert';
import 'dart:io';
import 'package:t_binary/src/core/binary_file_entry.dart';
import 'package:t_binary/src/core/func.dart';
import 'package:t_binary/t_binary.dart';

void main() async {
  try {
    final binary = await TBinary.readBinary('compress.tzip');
    for (var entry in binary.entries) {
      print(
        'name: ${entry.name} - Size: ${entry.getSizeLabel} CSize: ${entry.getCompressedSizeLabel}',
      );
    }

    // binary.extractAll(
    //   Directory('dist'),
    //   onProgress: (loaded, total, message) {
    //     print(
    //       'Progress: ${((loaded / total) * 100).toStringAsFixed(2)}% - message: $message',
    //     );
    //   },
    // );

    await binary.add(
      BFileEntry(file: File('/home/than/Documents/apyar_source.zip'),isCompress: true),
    );
    final one = binary.entries.last;
    print('last entry: $one');
    // await one.deleteEntry();
    // await one.extractDirectory(Directory('dist'));

    // await TBinary.deleteEntry(
    //   'compress.tzip',
    //   'CHANGELOG.md',
    //   onProgress: (loaded, total, message) {
    //     print(
    //       'Progress: ${((loaded / total) * 100).toStringAsFixed(2)}% - message: $message',
    //     );
    //   },
    // );

    // await compress(isCompress: true);
    // await deCompress();
    // await readFileList(File('compress.tzip'));

    // final res = await TBinary.readBinary('compress.tzip');
    // // print(res.fileDataStartOffset);
    // // print(res.fileListOffset);
    // for (var entry in res.fileList) {
    //   print(entry.size);
    //   entry.size = 0;
    //   print(entry.size);
    // }

    // await TBinary.addEntry(
    //   'compress.tzip',
    //   BFileEntry(
    //     file: File('/home/than/Videos/Sai_Sai_Kham_Leng_Feat._Frenzo_Artists.mp4'),
    //     isCompress: true,
    //   ),
    //   onProgress: (loaded, total, message) {
    //     print(
    //       'Progress: ${((loaded / total) * 100).toStringAsFixed(2)}% - message: $message',
    //     );
    //   },
    // );

    print('done');
  } catch (e) {
    print(e.toString());
  }
}

Future<void> readFileList(File file) async {
  final raf = await file.open();
  final fileSize = await file.length();

  // Seek to footer start
  await raf.setPosition(fileSize - 8);

  // --- Read footer ---
  final offsetBytes = await raf.read(4);
  final fileListOffset = bytesToInt(offsetBytes);
  // read magic
  final magicBytes = await raf.read(4);
  final magic = bytesToInt(magicBytes);

  const expectedMagic = 0x5442494E; // 'TBIN'
  if (magic != expectedMagic) {
    throw Exception('Invalid file: magic mismatch');
  }

  // --- Read file list JSON ---
  await raf.setPosition(fileListOffset);
  final listLengthBytes = await raf.read(4);
  final listLength = bytesToInt(listLengthBytes);

  final listBytes = await raf.read(listLength);
  final jsonStr = utf8.decode(listBytes);
  final entries = jsonDecode(jsonStr);

  await raf.close();

  print("File list: ${entries.toString()}");
  File('res.json').writeAsStringSync(jsonStr);
}

Future<void> deCompress() async {
  final dist = Directory('dist');
  if (!dist.existsSync()) {
    await dist.create();
  }

  final res = await TBinary.readBinary('compress.tzip');
  print(res.header.toMap());
  // print('cover: ${res.cover}');
  // await res.saveCover('cover.png');

  // for (var file in res.fileList) {
  //   print(
  //     'name: ${file.name} - isCompressed: ${file.isCompressed} - size: ${file.size} - compressedSize: ${file.compressedSize} offset: ${file.offset}',
  //   );
  // }
  // final one = res.fileList.first;
  // await one.extract(
  //   '${dist.path}/${one.name}',
  //   onProgress: (loaded, total, message) {
  //     print(
  //       'Progress: ${((loaded / total) * 100).toStringAsFixed(2)}% - message: $message',
  //     );
  //   },
  // );

  await res.extractAll(
    dist,
    onProgress: (loaded, total, message) {
      print(
        'Progress: ${((loaded / total) * 100).toStringAsFixed(2)}% - message: $message',
      );
    },
  );
}

Future<void> compress({bool isCompress = false}) async {
  List<String> filePaths = [
    '/home/than/Documents/manga.site.txt',
    '/home/than/projects/plugins/t_db/apyar/apyar.db',
    '/home/than/projects/plugins/t_db/apyar/apyar.db.lock',
    '/home/than/Documents/test2.pdf',
    '/home/than/Documents/Eric_Freeman_-_Head_First_Design_Patterns_-_2004.pdf',
  ];
  await TBinary.writeBinary(
    'compress.tzip',
    cover: File(
      '/home/than/Pictures/Untitled_logo_4_free-file.jpg',
    ).readAsBytesSync(),
    isCompress: isCompress,
    filePaths: filePaths,
    onProgress: (loaded, total, message) {
      print(
        'Progress: ${((loaded / total) * 100).toStringAsFixed(2)}% - message: $message',
      );
    },
  );
}
0
likes
145
points
9
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

TBinary is a Dart-based library designed to create a high-performance custom binary file format, capable of handling header metadata, an optional cover, multiple file entries, and large file data efficiently using streaming techniques.

Repository (GitHub)
View/report issues

License

MIT (license)

More

Packages that depend on t_binary