Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content-Encoding for Javascript Blob to use browser's built-in decompression? #48

Open
optimalisatie opened this issue Jan 22, 2017 · 0 comments

Comments

@optimalisatie
Copy link

Hi!

I am investigating an idea to make use of the browsers built in gzip decompression facility by using the Blob API. It appears that the only obstacle is the ability to set Content-Encoding for the blob.

I hereby want to inform if anyone knows of a solution to use a javascript Blob to uncompress gzip data.

The code that I use to test via the browser console is the following. It will change the background of a page to green on success.

/* create blob */
var createBlobUrl = function(fileData,mimeType) {
    var blob;

    // Create blob
    try {
        blob = new Blob([fileData], {type: mimeType});
    } catch (e) { // Backwards-compatibility
        window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder;
        blob = new BlobBuilder();
        blob.append(fileData);
        blob = blob.getBlob(mimeType);
    }
    return URL.createObjectURL(blob);
};

/* CSS: body, * { background:green; } */
var zip = window.atob('H4sIAAAAAAAA/0vKT6nUUdBSqFZISkzOTi/KL81LsUovSk3Ns1aoBQCLj6wTHQAAAA==');

function str2bytes (str) {
    var bytes = new Uint8Array(str.length);
    for (var i=0; i<str.length; i++) {
        bytes[i] = str.charCodeAt(i);
    }
    return bytes;
}
var bloburl = createBlobUrl(str2bytes(zip),'text/css');
var link = document.createElement('link');
link.setAttribute('rel','stylesheet');
link.setAttribute('type','text/css');
link.setAttribute('href',bloburl);
document.head.appendChild(link);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant