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

Unzipping file results in *.cpgz file in place of contents #41

Closed
commadelimited opened this issue Mar 25, 2013 · 9 comments
Closed

Unzipping file results in *.cpgz file in place of contents #41

commadelimited opened this issue Mar 25, 2013 · 9 comments

Comments

@commadelimited
Copy link

I'm using Node.js to zip up a directory using the node-zip wrapper for JSZip. The creation of my ZIP file works great, but when I try to unzip the file I get what appears to be another archive instead of the directory I was expecting.

You can check out my entire project here:
https://github.com/commadelimited/jsdownloader

The main work is being done in this file:
https://github.com/commadelimited/JSDownloader/blob/express-version/routes/download.js

But here's the relevant lines of code:

writeZip = function(dir,name) {
    var zip = new JSZip(),
        code = zip.folder(dir),
        output = zip.generate(),
        filename = ['jsd-',name,'.zip'].join('');

    fs.writeFileSync(baseDir + filename, output);
    console.log('creating ' + filename);
};

The values above are as follows:

dir = /tmp/jsd-<randomstring>/
name = <randomstring>

Here's the resulting ZIP file:
http://andymatthews.net/uploads/jsd-papuxetada.zip

Could I get some input as to what's going wrong?

@dduponchel
Copy link
Contributor

The resulting zip file contains your content as a base64 string.

$ base64 -d jsd-papuxetada.zip > decoded.zip
$ zipinfo decoded.zip
Archive:  decoded.zip
Zip file size: 224 bytes, number of entries: 2
drwx---     2.0 fat        0 b- stor 13-Mar-25 13:32 /tmp/
drwx---     2.0 fat        0 b- stor 13-Mar-25 13:32 /tmp/jsd-papuxetada/
2 files, 0 bytes uncompressed, 0 bytes compressed:  0.0%

The issue comes from zip.generate(). Without argument, this is the same as zip.generate({type:"base64"}) (it is an interesting default value for the location.href="data:application/zip;base64,"+content trick).
You should try zip.generate({type:"uint8array"}) or zip.generate({type:"arraybuffer"}) :-)

@commadelimited
Copy link
Author

Tried both of those options and got the same result. Each time I ran it, it still resulted in a *.cpgz file instead of the directory I was expecting.

Here's what I tried:

Encoded with uint8array

base64 jsd-qidecahoyo.zip > decoded.zip

zipinfo decoded.zip
[decoded.zip]
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
zipinfo:  cannot find zipfile directory in one of decoded.zip or
          decoded.zip.zip, and cannot find decoded.zip.ZIP, period.

zipinfo jsd-qidecahoyo.zip
[jsd-qidecahoyo.zip]
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
zipinfo:  cannot find zipfile directory in one of jsd-qidecahoyo.zip or
          jsd-qidecahoyo.zip.zip, and cannot find jsd-qidecahoyo.zip.ZIP, period.
Encoded with arraybuffer

base64 jsd-fowejeveli.zip > decoded2.zip

zipinfo decoded2.zip
[decoded2.zip]
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
zipinfo:  cannot find zipfile directory in one of decoded2.zip or
          decoded2.zip.zip, and cannot find decoded2.zip.ZIP, period.

zipinfo jsd-fowejeveli.zip
[decoded2.zip]
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
zipinfo:  cannot find zipfile directory in one of decoded2.zip or
          decoded2.zip.zip, and cannot find decoded2.zip.ZIP, period.

@dduponchel
Copy link
Contributor

It seems that nodejs method writeFileSync don't take an ArrayBuffer for buffer but transforms it to a string : the generated file contains [object ArrayBuffer].

Try to convert the ArrayBuffer into a Buffer :

var output = zip.generate({type:"arraybuffer"}),
    buffer = new Buffer(output.byteLength);

for (var i = 0; i < buffer.length; ++i) {
    buffer[i] = output[i];
}
fs.writeFileSync(baseDir + filename, buffer);

@commadelimited
Copy link
Author

Thanks. I'll give it a shot. Appreciate your time.

@commadelimited
Copy link
Author

@dduponchel Right on, that's really close. The zip file is extracting properly now, but the ZIP file itself is empty. The documentation itself doesn't explicitly say that it will add the contents of a directory along with the folder itself, but that's what most people would assume. I think I'm just going to go a diff route and dump all the files into one directory. Then I can loop over them and add each by itself.

@dduponchel
Copy link
Contributor

JSZip is designed to work in a browser. It works in nodejs but you will lack some things like the support of Buffer or utilities to recursively read a folder.

Maybe we should add some explanations in the documentation.

@Mithgol
Copy link
Contributor

Mithgol commented Apr 9, 2013

The need to convert some ArrayBuffer into a Buffer here is yet another effect of nodejs/node-v0.x-archive#4742 wontfixed.

@p1nox
Copy link

p1nox commented Jul 6, 2015

I had the same problem. Instead of using jszip I'm using this and is working fine.

@dannydulai
Copy link

the problem is that jszip produces a pkzip file (4.5) and many archival programs only support 2.1

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

6 participants