Implementation
void addFile(ArchiveFile file) {
final fileData = _ZipFileData();
_data.files.add(fileData);
fileData.name = file.name;
fileData.time = _data.time;
fileData.date = _data.date;
fileData.mode = file.mode;
fileData.isFile = file.isFile;
InputStreamBase? compressedData;
int crc32;
// If the user want's to store the file without compressing it,
// make sure it's decompressed.
if (!file.compress) {
if (file.isCompressed) {
file.decompress();
}
compressedData = (file.content is InputStreamBase)
? file.content as InputStreamBase
: InputStream(file.content);
if (file.crc32 != null) {
crc32 = file.crc32!;
} else {
crc32 = getFileCrc32(file);
}
} else if (file.isCompressed &&
file.compressionType == ArchiveFile.DEFLATE) {
// If the file is already compressed, no sense in uncompressing it and
// compressing it again, just pass along the already compressed data.
compressedData = file.rawContent;
if (file.crc32 != null) {
crc32 = file.crc32!;
} else {
crc32 = getFileCrc32(file);
}
} else {
// Otherwise we need to compress it now.
crc32 = getFileCrc32(file);
dynamic bytes = file.content;
if (bytes is InputStreamBase) {
bytes = bytes.toUint8List();
}
bytes = Deflate(bytes as List<int>, level: _data.level).getBytes();
compressedData = InputStream(bytes);
}
var filename = Utf8Encoder().convert(file.name);
var comment =
file.comment != null ? Utf8Encoder().convert(file.comment!) : null;
_data.localFileSize += 30 + filename.length + compressedData!.length;
_data.centralDirectorySize +=
46 + filename.length + (comment != null ? comment.length : 0);
fileData.crc32 = crc32;
fileData.compressedSize = compressedData.length;
fileData.compressedData = compressedData;
fileData.uncompressedSize = file.size;
fileData.compress = file.compress;
fileData.comment = file.comment;
fileData.position = _output!.length;
_writeFile(fileData, _output!);
fileData.compressedData = null;
}