adm-zip icon indicating copy to clipboard operation
adm-zip copied to clipboard

Modified:: addLocalFolder for windows users

Open Manjiz opened this issue 9 years ago • 4 comments

Modified addLocalFolder in adm-zip.js:

addLocalFolder : function(/*String*/localPath, /*String*/zipPath, /*RegExp|Function*/filter) {
            if (filter === undefined) {
              filter = function() { return true; };
            } else if (filter instanceof RegExp) {
              filter = function(filter) {
                return function(filename) {
                  return filter.test(filename);
                }
              }(filter);
            }

            if(zipPath){
                zipPath=zipPath.split("\\").join("/");
                if(zipPath.charAt(zipPath.length - 1) != "/"){
                    zipPath += "/";
                }
            }else{
                zipPath="";
            }
            // localPath = localPath.split("\\").join("/"); //windows fix
            localPath = pth.normalize(localPath);
            // if (localPath.charAt(localPath.length - 1) != "/")
            //     localPath += "/";
            if (fs.existsSync(localPath)) {

                var items = Utils.findFiles(localPath),
                    self = this;

                if (items.length) {
                    items.forEach(function(path) {
                        // var p = path.split("\\").join("/").replace( new RegExp(localPath, 'i'), ""); //windows fix
                        var p = pth.relative(localPath, pth.normalize(path));
                        console.log(p)
                        if (filter(p)) {
                            if (!fs.statSync(path).isDirectory()) {
                                console.log('AAA:')
                                self.addFile(zipPath+p, fs.readFileSync(path), "", 0)
                            } else {
                                self.addFile(zipPath+p, new Buffer(0), "", 0)
                            }
                        }
                    });
                }
            } else {
                throw Utils.Errors.FILE_NOT_FOUND.replace("%s", localPath);
            }
        },

Manjiz avatar May 12 '16 12:05 Manjiz

If this is a code change, shouldn't it be submitted as a pull request?

Danielv123 avatar Jul 05 '16 19:07 Danielv123

Yes, please fix this. @Manjiz's proposed changes are an improvement.

Without these fixes, adding a folder of files behaves entirely differently (and incorrectly) under Windows than under macOS. Under Windows, the relative portion of the localPath was not properly removed by the silly RegExp-based string replacement. (Why in world was it case insensitive?! Not to mention likely prone to Very Strange Behavior if the localPath happened to contain any syntax recognized as a regular expression. Good grief.)

The improved version above using the proper path module functions (which should ALWAYS be used when working with paths) should behave correctly under either OS.

mattflix avatar Jun 22 '17 08:06 mattflix

@Manjiz, could you please update the title of this issue to better reflect the problem? I would suggest, "addLocalFolder() does not handle localPath and zipPath properly under Windows".

Better yet, could submit a pull request with your change?

Btw, existing pull request #132 also seems to implement a fix similar to that proposed above, though the changes are not exactly the same.

Could somebody please resolve this addLocalFolder() on Windows issue please?

mattflix avatar Jun 22 '17 17:06 mattflix

I still have the issue. The path is wrongly set with backslash on my end

riderx avatar Jun 19 '24 14:06 riderx