0

I am having trouble decompressing a string in JavaScript. There is an XML file that is saved to a database. Before it is saved it is compressed using SharpZipLib in a VB application.

I am currently writing a web application that receives the compressed string through a Web API using JSON. What I am trying to do is to decompress the string into its original XML format so it can be used on the client side. I am trying to do this using JavaScript but I have not come across a way to decompress the string.

Below is an example of the code that I am trying to use with ZLib.js.

var buf = new ArrayBuffer(graphic.length);
var bufView = new Uint8Array(buf);
for (var i = 0; i < graphic.length; i++) {
    bufView[i] = graphic.charCodeAt(i);
}
var inflate = new Zlib.Inflate(compressed);
var plain = inflate.decompress();

I receive an error saying 'Error: unsupported compression method'.

I was able to use an example to compress and then decompress a string using ZLib.js successfully so I'm wondering is there a different compression method being used in SharpZipLib.

I have also tried using js-inflate.min.js to inflate the string but this only returns an empty string.

// using js-inflate.min.js
var uncompressed2 = JSInflate.inflate(graphic);

Is there a way to decompress a compressed string in JavaScript? Does SharpZipLib use Gzip or some other compression method that is causing the problem?

Tom
  • 365
  • 3
  • 19
  • Have you tried looking at the compressed stream? Perhaps first they compress it, then they base64 it. Or perhaps they simply create a DeflateStream (so not a real zip, only the compressed data of the zip without the headers) or perhaps they create a full zip (with headers, filenames, ...)... So many possible options... – xanatos Sep 04 '13 at 10:06
  • What exactly do you mean by looking at the compressed stream? The data is just compressed. Base64 isn't used. – Tom Sep 04 '13 at 11:05
  • Can you check if the first two characters of the stream are `PK`? – xanatos Sep 04 '13 at 11:08
  • The first few characters of the stream are 'UEsDBBQAAA' – Tom Sep 04 '13 at 11:13
  • Then I'll say it's encoded in some way. A compressed stream should contain non-alphabetical characters. – xanatos Sep 04 '13 at 11:15
  • It's base64 encoded, a full ZIP file. Try here http://decode.urih.com/, select Base 64 and paste the text. Click decode. You'll get PK, that is the signature of the ZIP files. – xanatos Sep 04 '13 at 11:16
  • The stream does have some non-alphabetical characters in it. This is a longer segment of the stream 'UEsDBBQAAAAIAIBEejn1IJU7mQkAABu3AAALAAAAR3JhcGhpYy54bWzlnVtPG0kWx99X2u+A/J647hcEvGRGq0iZrLSZkeZtZEwneAcb1heZ/fbrgMHHcdfZ6Yqr/k14icDVVbh/Ofc6XX32sVn/NFqOPjXLi7//7e' – Tom Sep 04 '13 at 11:18
  • it's clearly base64 encoded. First de-base64 and then inflate. – xanatos Sep 04 '13 at 11:19
  • If you paste `UEsDBBQAAAAIAIBEejn1IJU7mQkAABu3AAALAAAAR3JhcGhpYy54bWzlnVt` in the site I linked you, you'll even see the name of the contained file: `Graphic.xml` – xanatos Sep 04 '13 at 11:20
  • Thanks for the help. I was told that the stream wasn't encoded when it was compressed. I'll give that a try and see how it goes. – Tom Sep 04 '13 at 11:27
  • I've tried a couple of different ways to decode the string using window.atob(graphic) and also using the base64_decode function from https://github.com/kvz/phpjs/blob/master/functions/url/base64_decode.js. The resulting string looks very short (the value is 'PK') but the length is over 2000 characters. I have tried decoding the value in an online resource (decodebase64.com) and the value comes out as 'PKDz9õ ; ·Graphic.xmlå[OIÇßWÚïü¸' and so on. When I try to inflate this string I still get the 'Error: unsupported compression method' message. Am I using the wrong decoder? – Tom Sep 04 '13 at 13:58
  • The problem is that it's a full zip, not simply a stream. You could try `Zlib.Unzip` – xanatos Sep 04 '13 at 14:07
  • If it doesn't work try first to save the still-encoded base64, then with a command line base64 decoder decode it to a zip file and then open the zip file, so at least you can check if the zip file is ok. – xanatos Sep 04 '13 at 14:09
  • I tried using Zlib.Unzip and that worked. I had tried using it before but not with the string decoded and it was returning an error. Thanks again for all the help. – Tom Sep 04 '13 at 15:06

0 Answers0