archive
archive copied to clipboard
Fix long path name tar encoding and decoding errors
FormatException: EOF reached without finding string terminator
will be thrown with using the InputFileStream / OutputFileStream for .tar
file's encoding and decoding, if any file's path name is very very long.
Minimum Reproducible Example
import 'package:archive/archive_io.dart';
void main() async {
const kLongFileName = '/long_path_name/long_path_name/long_path_name/long_path_name/to/this_is_an_extremely_long_filename.bin';
{
final archive = Archive()..addFile(ArchiveFile(kLongFileName, 1, [1]));
final bytes = TarEncoder().encode(archive);
TarDecoder().decodeBytes(bytes);
}
{
final archive = Archive()..addFile(ArchiveFile(kLongFileName, 1, [1]));
final outputFile = 'output.tar';
TarEncoder().encode(archive, output: OutputFileStream(outputFile));
TarDecoder().decodeBuffer(InputFileStream(outputFile)); // FormatException: EOF reached without finding string terminator
}
}
How to Fix
In order to control the scope of influence (such as anywhere reading a null-terminated string), pass the size parameter to the InputStream#readString
method only when failed to trying to read the null terminator.
if (tf.filename == '././@LongLink') {
- nextName = tf.rawContent!.readString();
+ try {
+ nextName = tf.rawContent!.readString();
+ } catch (error) {
+ nextName = tf.rawContent!.readString(size: tf.fileSize);
+ }
continue;
}