[whatwg] Archive API - proposal

Jonas Sicking jonas at sicking.cc
Tue Aug 14 11:22:11 PDT 2012


On Tue, Jul 17, 2012 at 7:23 PM, Andrea Marchesini <baku at mozilla.com> wrote:
> Hi All,
>
> I would like to propose a new javascript/web API that provides the ability to read the content of an archive file through DOMFile objects.
> I have started to work on this API because it has been requested during some Mozilla Game Meeting by game developers who often use ZIP files as storage system.
>
> What I'm describing is a read-only and asynchronous API built on top of FileAPI ( http://dev.w3.org/2006/webapi/FileAPI/ ).
>
> Here a draft written in webIDL:
>
> interface ArchiveRequest : DOMRequest
> {
>   // this is the ArchiveReader:
>   readonly attribute nsIDOMArchiveReader reader;
> }
>
> [Constructor(Blob blob)]
> interface ArchiveReader
> {
>   // any method is supposed to be asynchronous
>
>   // The ArchiveRequest.result is an array of strings (the filenames)
>   ArchiveRequest getFilenames();
>
>   // The ArchiveRequest.result is a DOMFile (http://dev.w3.org/2006/webapi/FileAPI/#dfn-file)
>   ArchiveRequest getFile(DOMString filename);
> };
>
> Here an example about how to use it:
>
> function startRead() {
>   // Starting from a <input type="file" id="file" />:
>   var file = document.getElementById('file').files[0];
>
>   if (file.type != 'application/zip') {
>     alert("This archive format is not supported");
>     return;
>   }
>
>   // The ArchiveReader object works with Blob objects:
>   var archiveReader = new ArchiveReader(file);
>
>   // Any request is asynchronous:
>   var handler = archiveReader.getFilenames();
>   handler.onsuccess = getFilenamesSuccess;
>   handler.onerror = errorHandler;
>
>   // Multiple requests can run at the same time:
>   var handler2 = archiveReader.getFile("levels/1.txt");
>   handler2.onsuccess = getFileSuccess;
>   handler2.onerror = errorHandler;
> }
>
> // The getFilenames handler receives a list of DOMString:
> function getFilenamesSuccess() {
>   for (var i = 0; i < this.result.length; ++i) {
>     /* this.reader is the ArchiveReader:
>     var handle = this.reader.getFile(this.result[i]);
>     handle.onsuccess = ...
>     */
>   }
> }
>
> // The GetFile handler receives a File/Blob object (and it can be used with FileReader):
> function getFileSuccess() {
>   var reader = FileReader();
>   reader.readAsText(this.result);
>   reader.onload = function(event) {
>     // alert(event.target.result);
>   }
> }
>
> function errorHandler() {
>   // ...
> }
>
> I would like to receive feedback about this.. In particular:
> . Do you think it can be useful?
> . Do you see any limitation, any feature missing?

FWIW, this API is now available in Firefox nightly builds. It's
currently on track to ship in Firefox 17. Feedback would still be
greatly appreciated!

/ Jonas


More information about the whatwg mailing list