1

I have access to a website for testing purposes. I know the code of the site and am testing it with owner permissions. The code is similar to the below

var something= "{USER INPUT}'
result = eval((something));

So with SSJS I can amend this to effectively read

var something= "fs.readFileSync('/somewhere/file.txt',null)"
result = eval((something));

This outputs the contents to the screen further down the line, which is fine. However, doing the same on a ZIP file gives me all odd characters e.g

PK�coG���^�*file.txt

Now, the file looks very close to a local ZIP file I have. It starts off with PK and the contains the file name. However, in the Node version, there's lots of blank square boxes and black boxes with question marks.

Any advice on how to grab the contents of a ZIP file properly?

pee2pee
  • 3,619
  • 7
  • 52
  • 133
  • 1
    Try [this](https://www.npmjs.com/package/unzip) – Aᴍɪʀ Dec 15 '15 at 17:34
  • 3
    Seems like a duplicate of http://stackoverflow.com/q/34285297/245966 - please do not open multiple questions for the same problem. Instead refine the original question. – jakub.g Dec 15 '15 at 19:19

2 Answers2

0

I suggest you to use ChildProcess exec function to run the gzip command and read it after.

NBeydon
  • 695
  • 6
  • 14
0

If you're looking to unzip an actual .ZIP file, zlib.gunzip() or .unzip() will fail with an "incorrect header check" error since they're not meant to work with .ZIP files. ZIP files tend to be bundled directories, while gzip is generally meant only for single files (someone please comment/correct if I'm off on this). Also see How are zlib, gzip and Zip related? What are is common and How are they different?.

If you are after .ZIP, there are some modules out there designed for that purpose. An example would be node-unzip.

const fs = require('fs');
const unzip = require('node-unzip');

fs.createReadStream('path/to/some.zip').pipe(unzip.Extract({ path: 'output/path' }));

If you did want to do something with zlib, you'll want something like this:

const zlib = require('zlib');

zlib.unzip('./src/slow.jpg.gz', (err, data) => {
  if (err) throw err;
  // do something with the data
});

Also, worth nothing that you should be really careful with #eval() -> Why is using the JavaScript eval function a bad idea?

Community
  • 1
  • 1
markthethomas
  • 4,391
  • 2
  • 25
  • 38