从大文件下载 25mb 小块时,这些块实际上比 25mb 大得多。有人可以帮忙吗?
const fs = require('fs')
const files = fs.readdirSync('../in/')
files.map(fileName => {
const readable = fs.createReadStream('../in/'+fileName)
let size = 0
let temp_chunk;
let count = 0
readable.on("data", (chunk) => {
temp_chunk += chunk
const byteSize = new Blob([chunk]).size;
size += byteSize
let amount = size / 1000000
amount = amount.toString()
amount = amount.substring(0, 5) + 'mb'
console.log(amount)
if (amount > 24.5) {
console.log(fileName+' '+count+' downloaded')
fs.writeFileSync('../out/'+fileName+count, temp_chunk)
temp_chunk = ''
size = 0
count++
}
})
})
我尝试从 temp_chunk 读取文件的大小,这有效,但使下载速度明显变慢。
使用 Buffer 来连接块。在字符串上直接使用 += 连接二进制数据(如 temp_chunk += chunk)不适合二进制数据,并且可能会导致意外结果。
const fs = require('fs');
const files = fs.readdirSync('../in/');
files.forEach(fileName => {
const readable = fs.createReadStream('../in/' + fileName);
let size = 0;
let temp_chunk = Buffer.alloc(0); // Initialize as an empty buffer
let count = 0;
readable.on("data", (chunk) => {
temp_chunk = Buffer.concat([temp_chunk, chunk]);
size += chunk.length; // Use chunk.length to get the size in bytes
let amount = size / 1000000; // Convert to megabytes
console.log(amount + 'mb');
if (amount > 24.5) {
console.log(fileName + ' ' + count + ' downloaded');
fs.writeFileSync('../out/' + fileName + count, temp_chunk);
temp_chunk = Buffer.alloc(0); // Reset to an empty buffer
size = 0;
count++;
}
});
readable.on("end", () => {
if (temp_chunk.length > 0) {
// Write the remaining chunk if there is any
console.log(fileName + ' ' + count + ' downloaded (last chunk)');
fs.writeFileSync('../out/' + fileName + count, temp_chunk);
}
});
});