2013-01-07 21 views
0

我有一個node.js緩衝區實例,其中緩衝區是連接的部分,每個部分都有一個20字節的頭部,然後是放縮的數據。如何確定從傳遞給node.js zlib.inflate的緩衝區中讀取的字節數?

我需要的是使用node.js讀取泄漏數據,並知道泄漏序列有多少個字節,因此我可以正常前進到下一個緩衝區。事情是這樣的:

var zlib = require('zlib'); 
var sections = []; 
// A variable named 'buffer' is declared pointing to the Buffer instance 
// so I need to read the first section, then the second section etc. 
buffer = buffer.slice(20); // skip the 20-byte header 
zlib.inflate(buffer, function(err, inflatedData) { 
    sections.push(inflatedData); 
}); 
// How many bytes zlib readed from the buffer to 
// create the 'inflatedData' instance? 
// suppose the number of bytes read is pointed by the variable 'offset', 
// then I could do this to read the next section: 
buffer = buffer.slice(offset + 20); 
zlib.inflate(buffer, function(err, inflatedData) { 
    sections.push(inflatedData); 
}); 
+0

我不認爲數據暴露在任何地方。 Zip期望'buffer'包含所有的數據,所以它沒有用戶知道停止的地方的概念。爲什麼你需要爲這一切使用一個緩衝區? – loganfsmyth

+0

緩衝區並不重要,我需要的是解析與未壓縮數據混合的壓縮數據。爲了正確地調整位置,我需要知道壓縮序列的長度,只能通過解析它來完成。既然zlib已經做了解析,我寧願用它來獲取這些信息,而不是自己重新實現它。 –

回答

0
// How many bytes zlib readed from the buffer to 
// create the 'inflatedData' instance? 

答:所有這些。

buffer.slice(20)將從位置20直到緩衝區結束。這就是zlib.inflate()的方法,所以這就是它處理的。

也許這將是有益的:https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem

你看起來像你可能會感到困惑。你究竟在做什麼? (也就是說,你想要解決什麼問題?)

+0

是的,我理解buffer.slice是如何工作的,但zlib只會讀取字節,直到縮小的序列結束,因爲它具有固定的大小。我試圖解決的問題是(也許這也會是一個更好的標題):'如何解析包含與其他數據混合的泄漏數據的node.js緩衝區?'。在閱讀zlib.js和zlib文檔的源代碼後,我認爲答案是它目前不可能,因爲node.js綁定不公開所需的信息。如果你想給我答案,我會接受它 –

相關問題