How to merge two array buffers into one?

时间:2016-02-12 21:35:05

标签: javascript cordova blob fileapi

I have a big blob for a rar file. To uncompress it I had to use this library unrar.js

The blob I am testing with is about 23MB, to readAsArrayBuffer then feed it to unrar.js I had to slice it.

For some reason unrar.js does not play well with sliced blobs--it would throw an unknown archive type error if the sliced blob range does not start with 0 or end with blob.size . -weird?-

The only workaround I can think of is to read the blob as slices then collect it back again in the onloadend function. -is that even possible?-

Collect the array buffers in one array then feed it to unrar.js to uncompress the file as if it was read from one blob.

If this sound too annoying for you I would appreciate any other way to read rar files in javascript/phonegap environment.

1 个答案:

答案 0 :(得分:3)

我不确定我是否完全理解为什么blob是分段的,但是如果它们是,你可以做类似的事情:



var blobs = [new Blob(['Hello ']),new Blob(['World'])];
var buffers = [];
var buffersFilled = 0;

for(var i=0; i < blobs.length; i++) {
  buffers.push(new Uint8Array()); 
}


for(var i=0; i<blobs.length; i++) {
    var fileReader = new FileReader();
  
    fileReader.onload = (function(i,fileReader) {
      return function(){
        buffers[i] = fileReader.result;
        isBuffersDone();
      }
    })(i,fileReader);
    fileReader.readAsArrayBuffer(blobs[i]);
}

function isBuffersDone() {
  buffersFilled++;
  if(buffersFilled >= blobs.length) {
    var result = spliceBuffers();  
    
    //*****************
    // result is the spliced together buffers that could be given to unrar.js
    console.log(result);
  }
}

function spliceBuffers() {
  var tmpResult = new Uint8Array();
  
  for(var i=0; i < buffers.length; i++) {
    tmpResult = appendBuffer(tmpResult, buffers[i]);
  }
  
  return tmpResult;
}




function appendBuffer(buffer1, buffer2) {
  var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);
  tmp.set(new Uint8Array(buffer1), 0);
  tmp.set(new Uint8Array(buffer2), buffer1.byteLength);
  return tmp;
};
&#13;
&#13;
&#13;

虽然上面的工作很有效,但是由于我对你所做的事情的了解有限,我不知道这是最好的解决方案