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

How to extract ZIP archive using zlib module? #2385

Closed
ghost opened this issue Jan 3, 2020 · 7 comments
Closed

How to extract ZIP archive using zlib module? #2385

ghost opened this issue Jan 3, 2020 · 7 comments

Comments

@ghost
Copy link

ghost commented Jan 3, 2020

  • Node.js Version: v12.11.0
  • OS: windows
  • Scope (install, code, runtime, meta, other?): code
  • Module (and version) (if relevant): zlib

How to use zlib module to extract files from a ZIP archive? Is it possible using the builting zlib module, or do I have to parse ZIP file manually? Using an external NPM module is not an option.

This is my attempt:

const fs = require('fs');
const zlib = require('zlib');

const input = fs.readFileSync('file.zip');
const output = zlib.inflateSync(input);

And I get the following error

Error: incorrect header check
    at Zlib.zlibOnError [as onerror] (zlib.js:170:17)
    at processChunkSync (zlib.js:416:12)
    at zlibBufferSync (zlib.js:156:12)
    at Object.syncBufferWrapper [as inflateSync] (zlib.js:755:14)
    at Object.<anonymous> (C:\test.js:29:21)
    at Module._compile (internal/modules/cjs/loader.js:945:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:962:10)
    at Module.load (internal/modules/cjs/loader.js:798:32)
    at Function.Module._load (internal/modules/cjs/loader.js:711:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1014:10) {
  errno: -3,
  code: 'Z_DATA_ERROR'
}

Sample ZIP file: https://codeload.github.com/nodejs/help/zip/master

But also it throws error with any ZIP file.


What I would like is to list the files that are in the archive and to extract a specific file to some given location. How to do that?

Thanks.

@addaleax
Copy link
Member

addaleax commented Jan 4, 2020

Using an external NPM module is not an option.

Node.js core doesn’t support the full .zip format, so you’ll either have to use an external npm module or write code to read .zip files manually at this point.

@ghost
Copy link
Author

ghost commented Jan 4, 2020

@addaleax Thanks for the reply...

So this answer is definitely wrong? The OP says "zlib does not support zip files from what i read? Can you please confirm?" and then the response comes: "They support, use deflate api or create a unzip class object and use the api. Read and try with deflate option for start and then dig deeper" and the OP accepted it as an answer, meaning probably they somehow did it?

And also am I understanding correctly this issue: node/#26332? Is it talking about ZIP inflate support?

I'm not looking for full support (like some fancy file structure explorer etc). I can write low-level things like ZIP header check, I can locate file start and file end, but the main question is can I somehow use ZLIB module to decode/decompress/inflate/unzip the buffer that represents the file content once I find its boundaries?

For example suppose I have a ZIP file and it contains some files, then I manually parse ZIP header and chunks or whatever there is, and I locate the start position and length of a buffer representing the file I want to extract. Then I slice it from the whole archive and how can I use zlib to convert it to original format (quivalent to uncompression), so that I can writeFileSync it to somewhere and use it?

Thanks in advance.

@addaleax
Copy link
Member

addaleax commented Jan 4, 2020

So this answer is definitely wrong? The OP says "zlib does not support zip files from what i read? Can you please confirm?" and then the response comes: "They support, use deflate api or create a unzip class object and use the api. Read and try with deflate option for start and then dig deeper" and the OP accepted it as an answer, meaning probably they somehow did it?

I mean, it sounds like you have a pretty good understanding of what the zlib API provides – support for deflate/inflate, but not for the .zip file format.

I'm not looking for full support (like some fancy file structure explorer etc). I can write low-level things like ZIP header check, I can locate file start and file end, but the main question is can I somehow use ZLIB module to decode/decompress/inflate/unzip the buffer that represents the file content once I find its boundaries?

Yes, as long as the compression method that is used is actually deflate. I think that’s a reasonable assumption, though.

For example suppose I have a ZIP file and it contains some files, then I manually parse ZIP header and chunks or whatever there is, and I locate the start position and length of a buffer representing the file I want to extract. Then I slice it from the whole archive and how can I use zlib to convert it to original format (quivalent to uncompression), so that I can writeFileSync it to somewhere and use it?

Yes, I think that should work. 👍

@ghost
Copy link
Author

ghost commented Jan 6, 2020

good to know. thank you

@ghost ghost closed this as completed Jan 6, 2020
@maletor
Copy link

maletor commented Dec 10, 2020

@addaleax, are there technical (or political) reasons for not supporting ZIP archives with node standard library?

Or is it just a matter of not having gotten around to it as of yet?

@addaleax
Copy link
Member

@maletor I guess the question is, why do that when there’s userland libraries out there that do the same thing? The .zip format comes with a decent amount complexity on top of zlib, so I think having npm packages instead is a good choice.

@SampsonCrowley
Copy link

@addaleax one reason is that requiring userland packages means that a small portable script can't be shipped without requiring client side install; being able to rely on node packages for the most common formats and functions means you can run anywhere node is installed without extra client side setup

This issue was closed.
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

3 participants