如何学习nsIZipWriter和nsIZi $ P $帕德尔? [英] How to learn nsIZipWriter and nsIZipReader?

查看:134
本文介绍了如何学习nsIZipWriter和nsIZi $ P $帕德尔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用JavaScript来进行异步压缩写作和阅读。

I was wondering how to use JavaScript to do async zip writing and reading.

推荐答案

下面是一个使用的 nsIZi $ p $帕德尔,邮编写进去了类似的方式。该示例读取 /tmp/example.zip 一个zip文件并打印第一个文件到控制台的内容。

Here is an example for zip reading using nsIZipReader, zip writing goes in a similar way. The example reads a zip file from /tmp/example.zip and prints the content of the first file to the console.

注:


  • 我使用 nsIInputStreamPump 为异步I / O 我发现在<一个参考,这个API href=\"https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Guide/Streams#Forcing_an_input_stream_to_be_read\"相对=nofollow>流指导MDN 。该接口提供了一个 .asyncRead 方法,需要一个对象实现的 nsIStreamListener (和的 nsIRequestObserver )。

  • 我比较本实施例与同步阅读拉链在例如MDN 并发现读取由阻塞读用的时间是显著较小(从读取RAM磁盘的压缩文件:从1-2MS到0.1-0.3ms)< SUP>(测量使用 console.time('富'); reusableStreamInstance.init 调用和<$ C之前$ C> console.timeEnd('富'); 在 reusableStreamInstance.readBytes

  • 我使用 .readBytes 而不是 .read 来避免数据截断如果文件中包含一个空字节

  • 我还没有实现任何错误处理(ZIP不存在的,拉链不包含文件,ZIP /文件无法打开),但我关闭子$ P $帕德尔无论是否发生错误。

  • I'm using nsIInputStreamPump for async I/O I found a reference to this API at the Streams guide on MDN. This interface offers a .asyncRead method which takes an object that implements nsIStreamListener (and nsIRequestObserver).
  • I have compared the running time of this example with the synchronous zip reading example at MDN and found that reading the time used by the blocking read is significantly smaller (reading a zip file from RAM disk: from 1-2ms to 0.1-0.3ms) (measured using console.time('foo'); before the reusableStreamInstance.init invocation and console.timeEnd('foo'); after reusableStreamInstance.readBytes).
  • I am using .readBytes instead of .read to avoid truncation of data if the file contains a null byte.
  • I haven't implemented any error handling (zip not existent, zip doesn't contain a file, zip/file cannot be opened), but I do close the zipreader regardless of whether an error occurs.
let { Cc: classes, Cu: utils, Ci: interfaces } = Components;

Cu.import('resource://gre/modules/FileUtils.jsm');

let pathToZip = '/tmp/example.zip';
let nsiFileZip = new FileUtils.File(pathToZip);

let streamListener = {
  buffer: null,
  // nsIRequestObserver
  onStartRequest: function(aRequest, aContext) {
    this.buffer = [];
  },
  // nsIStreamListener
  onDataAvailable:
      function(aRequest, aContext, aInputStream, aOffset, aCount) {
    let reusableStreamInstance = Cc['@mozilla.org/scriptableinputstream;1']
        .createInstance(Ci.nsIScriptableInputStream);
    reusableStreamInstance.init(aInputStream);
    let chunk = reusableStreamInstance.readBytes(aCount);
    this.buffer.push(chunk);
  },
  // nsIRequestObserver
  onStopRequest: function(aRequest, aContext, aStatusCode) {
    console.log('end', aStatusCode);
    var data = this.buffer.join('');
    console.log(data);
  }
};

let pump = Cc['@mozilla.org/network/input-stream-pump;1']
    .createInstance(Ci.nsIInputStreamPump);
let zr = Cc['@mozilla.org/libjar/zip-reader;1']
    .createInstance(Ci.nsIZipReader);
try {
  zr.open(nsiFileZip);
  // null = no entry name filter.
  var entries = zr.findEntries(null);
  while (entries.hasMore()) {
    var entryPointer = entries.getNext();
    var entry = zr.getEntry(entryPointer);
    if (!entry.isDirectory) {
      var stream = zr.getInputStream(entryPointer);
      pump.init(stream, 0, -1, 0, 0, true);
      pump.asyncRead(streamListener, null);
      // For the demo, we only take one file, so break now.
      break;
    }
  }
} finally {
  zr.close();
}

这篇关于如何学习nsIZipWriter和nsIZi $ P $帕德尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆