entry.state = Entry.FILE_DATA_DONE;
pumpEntries(self);
});
}
从上面代码可以看出来:
uncompressedSizeCounter.byteCount 是从
pumpFileDataReadStream() 函数 readStream 参数中获取的属性值,而
entry.uncompressedSize 也是函数的 entry 参数中获取的属性。接着找
pumpFileDataReadStream() 函数是从哪里调用的。通过输出日志得出
pumpFileDataReadStream() 函数是在以下面的代码中被调用的:
ZipFile.prototype.addFile = function(realPath, metadataPath, options) {
var self = this;
metadataPath = validateMetadataPath(metadataPath, false);
if (options == null) options = {};
var entry = new Entry(metadataPath, false, options);
self.entries.push(entry);
fs.stat(realPath, function(err, stats) {
if (err) return self.emit("error", err);
if (!stats.isFile()) return self.emit("error", new Error("not a file: " + realPath));
// 这里是文件的大小
entry.uncompressedSize = stats.size;
if (options.mtime == null) entry.setLastModDate(stats.mtime);
if (options.mode == null) entry.setFileAttributesMode(stats.mode);
entry.setFileDataPumpFunction(function() {
// readStream在这里创建的
var readStream = fs.createReadStream(realPath);
entry.state = Entry.FILE_DATA_IN_PROGRESS;
readStream.on("error", function(err) {
self.emit("error", err);
});
// 在这里被调用
pumpFileDataReadStream(self, entry, readStream);
});
pumpEntries(self);
});
};从上面代码可以看出来
entry.uncompressedSize 是stats.size,即文件的大小, readStream 是创建的文件流。但是在什么情况下两者会不一样呢?感觉只可能在文件还没有读取完,但是是什么原因导致这种情况发生?由于对JS接触的时间不长,没有进行深入分析。最后在抛出异常的上面一行用 console.log 将两个属性的大小值都输出,代码如下所示:
if (entry.uncompressedSize == null) {
entry.uncompressedSize = uncompressedSizeCounter.byteCount;
} else {
// 增加日志输出
console.log("entry size: " + entry.uncompressedSize + ", uncompressedSize: " + uncompressedSizeCounter.byteCount);
if (entry.uncompressedSize !== uncompressedSizeCounter.byteCount) return self.emit("error", new Error("file data stream has unexpected number of bytes"));
}在
archive.finalize() 时和 output 写入流的 close 事件时(详细参照官方的示例代码),分别加上日志输出,代码如下所示:









