unpack method

void unpack(
  1. String pathTo
)

Unpacks a resource saving it to the file at pathTo. Throws ResourceException. @Throwing(ArgumentError) @Throwing(CreateDirException) @Throwing(ResourceException) @Throwing(FormatException) @Throwing(RangeError)

Implementation

void unpack(String pathTo) {
  if (exists(pathTo) && !isFile(pathTo)) {
    throw ResourceException('The unpack target $pathTo must be a file');
  }
  final normalized = normalize(pathTo);
  if (!exists(dirname(normalized))) {
    createDir(dirname(normalized), recursive: true);
  }

  final file = File(normalized).openSync(mode: FileMode.write);

  try {
    // Scan the generated Base64 lines without allocating a list containing
    // every line. A fixed buffer batches the small decoded lines into writes.
    final buffer = Uint8List(64 * 1024);
    var buffered = 0;
    for (final decoded in _decodedChunks()) {
      var offset = 0;
      while (offset < decoded.length) {
        final available = buffer.length - buffered;
        final remaining = decoded.length - offset;
        final count = remaining < available ? remaining : available;
        buffer.setRange(buffered, buffered + count, decoded, offset);
        buffered += count;
        offset += count;
        if (buffered == buffer.length) {
          file.writeFromSync(buffer);
          buffered = 0;
        }
      }
    }
    if (buffered != 0) {
      file.writeFromSync(buffer, 0, buffered);
    }
  } finally {
    file
      ..flushSync()
      ..closeSync();
  }
}