最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Native javascript decompress function in Chromewebkit - Stack Overflow

programmeradmin1浏览0评论

If you are sending data that is base64-encoded and compressed (using, say, python's zlibpress()), you can use the native Chrome function window.atob() to convert from base64 to binary data. Is there any similar native javascript function to decompress the zlib-compressed data? Is there some hack to do this?

I know that code to decompress data is already in the browser because it can receive HTML sent with gzip headers.

I am not looking for a javascript library to do decompression.

If you come up a decompression scheme on the browser, I can compress it in that format for transmission. In other words, any decompression routine is acceptable.

If you are sending data that is base64-encoded and compressed (using, say, python's zlib.compress()), you can use the native Chrome function window.atob() to convert from base64 to binary data. Is there any similar native javascript function to decompress the zlib-compressed data? Is there some hack to do this?

I know that code to decompress data is already in the browser because it can receive HTML sent with gzip headers.

I am not looking for a javascript library to do decompression.

If you come up a decompression scheme on the browser, I can compress it in that format for transmission. In other words, any decompression routine is acceptable.

Share Improve this question edited Jun 16, 2011 at 20:32 hughdbrown asked Jun 16, 2011 at 20:13 hughdbrownhughdbrown 49k20 gold badges88 silver badges111 bronze badges 3
  • Maybe you're just looking for "btoa()", but for the record base64 encoding is not compression; in fact it makes the data bigger. – Pointy Commented Jun 16, 2011 at 20:18
  • 1 Okay, I guess I was unclear. I am not looking for btoa(). I know about that. I am talking about decompressing data compressed with LZ77 or LZW or gzip or any similar compression algorithm. – hughdbrown Commented Jun 16, 2011 at 20:27
  • That's what I suspected :-) Unfortunately I don't know of an API that exposes that, but I'm not familiar with non-standard browser internals, especially for Chrome. – Pointy Commented Jun 16, 2011 at 20:30
Add a comment  | 

5 Answers 5

Reset to default 8

2020 Update

Chrome 80+ supports CompressionStream and DecompressionStream APIs

Gzip-compress a stream

const compressedReadableStream = inputReadableStream.pipeThrough(new CompressionStream('gzip'));

Deflate-compress an ArrayBuffer

function compressArrayBuffer(input) {
  const stream = new Response(input).body
    .pipeThrough(new CompressionStream('deflate'));
  return new Response(stream).arrayBuffer();
}

Gzip-decompress a Blob to a Blob

This treats the input as a gzip file regardless of the mime-type. The output Blob has an empty mime-type.

async function DecompressBlob(blob) {
  const ds = new DecompressionStream('gzip');
  const decompressedStream = blob.stream().pipeThrough(ds);
  return await new Response(decompressedStream).blob();
}

https://github.com/WICG/compression/blob/master/explainer.md

Here's a hack to paint a PNG containing compressed data into a canvas and reading the data back, pixel by pixel: Compression using Canvas and PNG-embedded data. If you want anything that uses a browser's native compression library, here's one option. Unfortuantely, you have to convert the ImageData to string within javascript.

There is no such function exposed.

Try window.btoa.

The question doesn't expand on "sending" but if the server sets Content-Encoding: gzip then the browser should decompress it before passing the resulting content to JavaScript.

发布评论

评论列表(0)

  1. 暂无评论