write method
Implementation
void write(OutputStream output, {Encoding? filenameEncoder}) {
fileSize = size;
// The name, linkname, magic, uname, and gname are null-terminated
// character strings. All other fields are zero-filled octal numbers in
// ASCII. Each numeric field of width w contains w minus 1 digits, and a null.
final header = OutputMemoryStream();
_writeString(header, filename, 100, filenameEncoder);
_writeInt(header, mode, 8);
_writeInt(header, ownerId, 8);
_writeInt(header, groupId, 8);
_writeInt(header, fileSize, 12);
_writeInt(header, lastModTime, 12);
_writeString(header, ' ', 8); // checksum placeholder
_writeString(header, typeFlag, 1);
if (nameOfLinkedFile != null) {
_writeString(header, nameOfLinkedFile!, 100, filenameEncoder);
} else {
_writeString(header, '', 100);
}
final remainder = 512 - header.length;
var nulls = Uint8List(remainder); // typed arrays default to 0.
header.writeBytes(nulls);
final headerBytes = header.getBytes();
// The checksum is calculated by taking the sum of the unsigned byte values
// of the header record with the eight checksum bytes taken to be ascii
// spaces (decimal value 32). It is stored as a six digit octal number
// with leading zeroes followed by a NUL and then a space.
var sum = 0;
for (var b in headerBytes) {
sum += b;
}
var sumStr = sum.toRadixString(8); // octal basis
while (sumStr.length < 6) {
sumStr = '0$sumStr';
}
var checksumIndex = 148; // checksum is at 148th byte
for (var i = 0; i < 6; ++i) {
headerBytes[checksumIndex++] = sumStr.codeUnits[i];
}
headerBytes[154] = 0;
headerBytes[155] = 32;
output.writeBytes(header.getBytes());
// Through a subset, so that writing never moves the position of the
// stream the entry was given: a file output reads it in chunks
if (_content != null) {
output.writeStream(_content!.getStream().subset());
} else if (_rawContent != null) {
output.writeStream(_rawContent!.subset());
}
if (isFile && fileSize > 0) {
// Pad to 512-byte boundary
final remainder = fileSize % 512;
if (remainder != 0) {
final skiplen = 512 - remainder;
nulls = Uint8List(skiplen); // typed arrays default to 0.
output.writeBytes(nulls);
}
}
}