node-archiver icon indicating copy to clipboard operation
node-archiver copied to clipboard

.glob() with options.cwd is not working at 5.x

Open hyoo opened this issue 5 years ago • 7 comments

Just found the source problem, in the pr

https://github.com/archiverjs/node-archiver/pull/433

The non-backward compatibility needs to be documented or clearly communicated to the users !!!

Is there any workaround that can avoid including absolute path?

hyoo avatar Sep 22 '20 18:09 hyoo

This was communicated in the changes for 5.0. Its also why it was released as a new major version per semver.

please provide an example of your usage

ctalkington avatar Sep 24 '20 00:09 ctalkington

I ran into the same problem, found the breaking change note in the changelog, and updated my usage:

-archive.glob('../frontend/build/static/js/main.*.js', null, { name: 'ghost-inspector.js', prefix });
+archive.glob('main.*.js', { cwd: '../frontend/build/static/js/'}, { name: 'ghost-inspector.js', prefix });

However the problem I'm having now is that it doesn't seem to be using options.name anymore. The final archive is using the original filenames it found via glob.

EDIT: it appears in the previous version, it only overwrote the entrydata name if options.cwd was set. In version 5.x, it always overwrites the name: https://github.com/archiverjs/node-archiver/pull/433/files#diff-826e5ed9069c92baa50ec46dde8074a6789b4c90837c5ede0e6b0438e3c34309L734-L737

simpixelated avatar Jan 05 '21 20:01 simpixelated

@simpixelated can you provide sn example of file names inside archive and expected names?

ctalkington avatar Jan 07 '21 04:01 ctalkington

@ctalkington

filename inside the archive (matches original filename):

main.7f7102c9.js

expected (with {name: 'ghost-inspector.js'}:

ghost-inspector.js

simpixelated avatar Jan 07 '21 18:01 simpixelated

Here is my use-case. I have files in /a/b/c1/fileN/ and /a/b/c2/filesN for example, and I want to build archive files based on the level of C, keeping the sub-directory structures. So, when user downloaded and unzipped, it shows like c1/filesN and c2/filesN.

hyoo avatar Jan 11 '21 16:01 hyoo

@hyoo you should be able to do that with:

archive.directory('a/b/c1', 'c1')

ctalkington avatar Feb 06 '21 07:02 ctalkington

I feel like an idiot but I can't seem to get glob working. I have an array of paths that I want. I'm confused by cwd and to what it refers. I'm using 5.3.0

const output = fs.createWriteStream('output.zip')

const allowedItems = [
  'file.xml',
  'directory1',
  'directory2',
]

archiver.pipe(output)
archiver.directory('/a/directory/path', false)

for (const path of allowedItems) {
  archiver.glob(path, { cwd: '????' })
}

archiver.finalize()

Is it the relative path of the real file path or what it will be in the zip? Thanks!

grantholle avatar Aug 27 '21 09:08 grantholle

cwd stands for current working directory and tells glob where it should base its search. the glob pattern should be relative to cwd

ctalkington avatar Sep 04 '23 05:09 ctalkington