Electron intercept file:/// protocol to read inside zip files


#1

I would like to read inside zip archives with Electron as if they were folders, like so /myfolder/myarchive.zip/picture.jpg. To do this I’m thinking about intercepting the file protocol

protocol.interceptFileProtocol('file', (request, callback) => {
    if (insideZipArchive) {
        //respond with zip file contents
    } else {
        //defaultBehavior
    }

  }, (error) => {
    if (error) console.error('Failed to register protocol')
  })

How do I invoke the default behavior? Also doing an AJAX request inside the interceptor and serving files like this callback({data: new Buffer(xhr.responseText)})does not seem to work.


#2

You’ll want to look at something like Node-7z to extract or read the content.


#3

Extracting zip files is not a problem. The problem is intercepting the protocol.


#4

A year too late.
Recently, I do something to restore default file behaviour as follows
May not be fool-proof , just a gist

protocol.interceptFileProtocol(‘file’, (request, callback) => {
callback(fs.createReadStream(decodeURIComponent(request.url.substr(8))))
}, (error) => {
if (error) {
console.error(‘Failed to intercept file protocol’)
}
})


#6

It looks like this could be solved with service workers, as shown here: