Example #1
0
function unzip(data, dest, callback) {
  yauzl.fromBuffer(data, function(err, zipfile) {
    var done, count = 0, written = 0;
    if (err) callback(err);
    zipfile.on("entry", function(entry) {
      count++;
      if (/\/$/.test(entry.fileName)) {
        utils.mkdir(path.join(dest, entry.fileName));
        if (done) callback(null);
      } else {
        zipfile.openReadStream(entry, function(err, rs) {
          if (err) return callback(err);
          var ws = fs.createWriteStream(path.join(dest, entry.fileName));
          ws.on("finish", function() {
            written++;
            if (done && written === count) {
              callback(null);
            }
          });
          rs.pipe(ws);
        });
      }
    });
    zipfile.on("end", function() {
      done = true;
    });
  });
}
Example #2
0
function unzipBuffer(contents) {
	var result = es.through();
	yauzl.fromBuffer(contents, function (err, zip) {
		if (err) { return result.emit('error', err); }
		toStream(zip).pipe(result);
	});
	return result;
}
Example #3
0
                return new Promise((resolve, reject) => {
                    // unzip data
                    unzip.fromBuffer(response.body, {
                        lazyEntries: true
                    }, (err, zip) => {
                        var manifestData;
                        var recordsData;

                        this.Log("Parsing zip file");
                        if (err) {
                            return reject(err);
                        }

                        const GetNextEntry = () => {
                            if (manifestData && recordsData) {
                                // got both the files we need, stop reading the zip file

                                // fetch next data URL
                                if (manifestData.version) {
                                    this.FetchParkData(manifestData.version).catch(() => {
                                        // as soon as we hit an error, return the current level or records data
                                        return resolve(recordsData);
                                    });
                                } else {
                                    return resolve(recordsData);
                                }
                            } else {
                                // read next entry
                                zip.readEntry();
                            }
                        };

                        zip.on("entry", (file) => {
                            this.Log(`Got zip file ${file.fileName}`);

                            // look for the two files we want
                            if (file.fileName == "manifest.json") {
                                ReadZipFile(zip, file).then((data) => {
                                    manifestData = data;

                                    GetNextEntry();
                                });
                            } else if (file.fileName == "records.json") {
                                ReadZipFile(zip, file).then((data) => {
                                    recordsData = data;

                                    GetNextEntry();
                                });
                            } else {
                                GetNextEntry();
                            }
                        });

                        // start reading file...
                        zip.readEntry();
                    });
                });
Example #4
0
 return new Promise((resolve, reject) => {
     yauzl.fromBuffer(buffer, { lazyEntries: true }, (err, zipFile) => {
         if (err) {
             let message = "C# Extension was unable to download its dependencies. Please check your internet connection. If you use a proxy server, please visit https://aka.ms/VsCodeCsharpNetworking";
             eventStream.post(new loggingEvents_1.ZipError(message));
             return reject(new NestedError_1.NestedError(message));
         }
         zipFile.readEntry();
         zipFile.on('entry', (entry) => {
             let absoluteEntryPath = path.resolve(destinationInstallPath.value, entry.fileName);
             if (entry.fileName.endsWith('/')) {
                 // Directory - create it
                 mkdirp(absoluteEntryPath, { mode: 0o775 }, err => {
                     if (err) {
                         return reject(new NestedError_1.NestedError('Error creating directory for zip directory entry:' + err.code || '', err));
                     }
                     zipFile.readEntry();
                 });
             }
             else {
                 // File - extract it
                 zipFile.openReadStream(entry, (err, readStream) => {
                     if (err) {
                         return reject(new NestedError_1.NestedError('Error reading zip stream', err));
                     }
                     mkdirp(path.dirname(absoluteEntryPath), { mode: 0o775 }, err => {
                         if (err) {
                             return reject(new NestedError_1.NestedError('Error creating directory for zip file entry', err));
                         }
                         let binaryPaths = binaries && binaries.map(binary => binary.value);
                         // Make sure executable files have correct permissions when extracted
                         let fileMode = binaryPaths && binaryPaths.indexOf(absoluteEntryPath) !== -1
                             ? 0o755
                             : 0o664;
                         readStream.pipe(fs.createWriteStream(absoluteEntryPath, { mode: fileMode }));
                         readStream.on('end', () => zipFile.readEntry());
                     });
                 });
             }
         });
         zipFile.on('end', () => {
             resolve();
         });
         zipFile.on('error', err => {
             reject(new NestedError_1.NestedError('Zip File Error:' + err.code || '', err));
         });
     });
 });
Example #5
0
 }, (err, response, body) => {
       if (!err && response.statusCode == 200) {
             yauzl.fromBuffer(body, 'xbrl', (err, zipfile) => {
                   if (err) { reject(err); return; }
                   zipfile.on("entry", function(entry) {
                         if (/\d\.xml/.test(entry.fileName)) {
                               // file entry 
                               zipfile.openReadStream(entry, function(err, readStream) {
                                     if (err) { reject(err); return; }
                                     ParseXbrl.parse(readStream).then(function(parsedDoc) {
                                           if (err) { reject(err); return; }
                                           resolve(parsedDoc);
                                     });
                               });
                         }
                   });
             });
       } else {
             reject(`Failed to download ${xbrlURL}; response status code: ${response.statusCode}`);
       }
 });
Example #6
0
	return through.obj( function( file, enc, cb ) {
		var self = this;

		if( file.isNull() ) {
			cb( null, file );
			return;
		}

		if( file.isStream() ) {
			cb( new Error( 'Streaming is not supported' ) );
			return;
		}

		if( !file.extract || !isZip( file.contents ) ) {
			cb( null, file );
			return;
		}

		yauzl.fromBuffer( file.contents, function( err, zipFile ) {
			var count = 0;

			if( err ) {
				cb( err );
				return;
			}

			zipFile.on( 'error', cb );
			zipFile.on( 'entry', function( entry ) {
				var filePath = stripDirs( entry.fileName, opts.strip );

				if( filePath === '.' ) {
					if( ++count === zipFile.entryCount ) {
						cb();
					}

					return;
				}

				var stat = new fs.Stats();
				var mode = (entry.externalFileAttributes >> 16) & 0xFFFF;

				stat.mode = mode;

				if( entry.getLastModDate() ) {
					stat.mtime = entry.getLastModDate();
				}

				if( entry.fileName.charAt( entry.fileName.length - 1 ) === '/' ) {
					if( !mode ) {
						new Mode( stat ).isDirectory( true );
					}

					self.push( new File( {
						path: filePath,
						stat: stat
					} ) );

					if( ++count === zipFile.entryCount ) {
						cb();
					}

					return;
				}

				zipFile.openReadStream( entry, function( err, readStream ) {
					if( err ) {
						cb( err );
						return;
					}

					readAllStream( readStream, null, function( err, data ) {
						if( err ) {
							cb( err );
							return;
						}

						if( !mode ) {
							new Mode( stat ).isFile( true );
						}

						self.push( new File( {
							contents: data,
							path: filePath,
							stat: stat
						} ) );

						if( ++count === zipFile.entryCount ) {
							cb();
						}
					} );
				} );
			} );
		} );
	} );
Example #7
0
	return through.obj(function (file, enc, cb) {
		var self = this;

		if (file.isNull()) {
			cb(null, file);
			return;
		}

		if (file.isStream()) {
			cb(new Error('Streaming is not supported'));
			return;
		}

		if (!file.extract || !isZip(file.contents)) {
			cb(null, file);
			return;
		}

		yauzl.fromBuffer(file.contents, function (err, zipFile) {
			var count = 0;

			if (err) {
				cb(err);
				return;
			}

			zipFile
				.on('error', cb)
				.on('entry', function (entry) {
					if (entry.fileName.charAt(entry.fileName.length - 1) === '/') {
						if (++count === zipFile.entryCount) {
							cb();
						}

						return;
					}

					zipFile.openReadStream(entry, function (err, readStream) {
						if (err) {
							cb(err);
							return;
						}

						var chunks = [];
						var len = 0;

						readStream
							.on('error', cb)
							.on('data', function (data) {
								chunks.push(data);
								len += data.length;
							})
							.on('end', function () {
								var stat;
								var mode = (entry.externalFileAttributes >> 16) & 0xFFFF;

								if (mode) {
									stat = new fs.Stats();
									stat.mode = mode;
								} else {
									stat = null;
								}

								self.push(new File({
									stat: stat,
									contents: Buffer.concat(chunks, len),
									path: stripDirs(entry.fileName, opts.strip)
								}));

								if (++count === zipFile.entryCount) {
									cb();
								}
							});
					});
			});
		});
	});
Example #8
0
        fetchDef.then(function(rawContent) {
            var left = rawContent.toString('utf8', 0, 4).substr(0, 4);
            if (left == 'PK\u0003\u0004') {
                // it's a ZIP file - go fetch all the content
                let rawContentObjectName = u.getObjectName(rawContent);
                rawContent = parseUtils.toBuffer(rawContent);
                yauzl.fromBuffer(rawContent, {
                    lazyEntries: true
                }, function(err, zipFile) {
                    if (err) {
                        reject = true;
                        let detail = 'The content may have been uploaded with a wrong encoding (i.e. utf8). ' +
                          'Here are the first 50 base64 characters of the content:' + rawContent.toString('base64', 0, 50);
                        def.reject(new Error('The zip file format is incorrect: (' + err +
                          ') (Internal: Found while processing ' + rawContentObjectName + ') ' + detail));
                    } else if (!reject) {
                        zipFile.readEntry();
                        zipFile.on('error', function(err) {
                            reject = true;
                            let detail = 'The content may have been uploaded with a wrong encoding (i.e. utf8). ' +
                              'Here are the first 50 base64 characters of the content:' + rawContent.toString('base64', 0, 50);
                            def.reject(new Error('The zip file format is incorrect: (' + err +
                              ') (Internal: Found while processing ' + rawContentObjectName + ') ' + detail));
                        });
                        zipFile.on('entry', function(entry) {
                            if (/\/$/.test(entry.fileName) || (/__MACOSX/).test(entry.fileName)) {
                                // ignore directories and MAC archives
                                zipFile.readEntry();
                            } else {
                                var strings = [];
                                zipFile.openReadStream(entry, function(err, readStream) {
                                    if (err) {
                                        reject = true;
                                        def.reject(err);
                                    } else {
                                        readStream.on('data', function(chunk) {
                                            strings.push(chunk);
                                        });
                                        readStream.on('end', function() {
                                            var nameLen = entry.fileName.length;
                                            let isXMLSuffix = entry.fileName.indexOf('.xml', nameLen - 4) != -1;
                                            let isXSDSuffix = entry.fileName.indexOf('.xsd', nameLen - 4) != -1;
                                            let isWSDLSuffix = entry.fileName.indexOf('.wsdl', nameLen - 5) != -1;

                                            // Throw an error if only allowing xsd and wsdl files
                                            // and this is an extra file
                                            if (!opts.allowExtraFiles && !isWSDLSuffix && !isXSDSuffix) {
                                                def.reject(parseUtils.cleanupError(
                                                  new Error('Only .xsd and .wsdl files are allowed in the zip.  Found: ' + entry.fileName)));
                                            }
                                            // An .xml file may contain xsd or it may contain random
                                            // xml information.  Try to read .xml files but don't report
                                            // errors.
                                            if (isWSDLSuffix || isXSDSuffix || isXMLSuffix) {
                                                // only discover WSDLs and XSDs
                                                var shortName = entry.fileName;
                                                var index = shortName.lastIndexOf('/');
                                                if (index != -1) {
                                                    shortName = shortName.substr(index + 1);
                                                }
                                                var file = {
                                                    filename: shortName,
                                                    fullName: entry.fileName,
                                                    type: 'wsdl', // temp for now determined later when we actually parse it
                                                    content: '',
                                                    context: 'zip'
                                                };
                                                file.content = Buffer.concat(strings);
                                                var encoding = parseUtils.determineEncoding(file.content, file.fullName);
                                                file.content = parseUtils.decode(file.content, encoding);
                                                totalProcessed += file.content.length;
                                                try {
                                                    // If the customer proviced a very, very large zip file containing
                                                    // many large xsds that are unreferenced, the zip file processing
                                                    // will run out of memory and the designer will hang.
                                                    // An error message is better than a hang.
                                                    // An alternative is to do mult-passes through the zip collecting
                                                    // only the schema that is needed, but that is slow and error prone.
                                                    if (totalProcessed > 64000000) {
                                                        throw new Error('The zip file contains too many large xsd/wsdl files. ' +
                                                           'Please remove unnecessary xsd/wsdl files and try again.');
                                                    }
                                                    try {
                                                        parseUtils.contentToXMLorWSDL(file);
                                                    } catch (e) {
                                                        // If the file is not a wsdl, then save the error
                                                        // and rethrow later only if it is is included or imported
                                                        if (isWSDLSuffix) {
                                                            throw e;
                                                        } else {
                                                            file.error = e;
                                                            file.type = 'xsd';
                                                        }
                                                    }
                                                    let valid = false;
                                                    if (file.error) {
                                                        files.push(file);
                                                    } else if (file.json.definitions) {
                                                        var wsdlGood = parseUtils.validateWSDLJson(file.json, file.filename);
                                                        if (wsdlGood.valid) {
                                                            files.push(file);
                                                            file.type = 'wsdl';
                                                            var namespaces = file.doc.definitions.xmlns;
                                                            file.namespaces = namespaces;
                                                            valid = true;
                                                        } else {
                                                            throw new Error(wsdlGood.reason);
                                                        }
                                                    } else if (file.json.schema) {
                                                        files.push(file);
                                                        file.type = 'xsd';
                                                        file.namespaces = {};
                                                        valid = true;
                                                    } else {
                                                        let e = new Error('Failed to parse WSDL or XSD: ' + file.filename);
                                                        if (isWSDLSuffix) {
                                                            throw e;
                                                        } else {
                                                            file.error = e;
                                                            file.type = 'xsd';
                                                            files.push(file);
                                                        }
                                                    }
                                                    // have to capture the service children elements (ports) here as they get munged later
                                                    if (valid) {
                                                        file.serviceChildren = postParse.captureServiceChildren(file.doc);
                                                        postParse.updateSchemaNamespaces(file);
                                                    }
                                                } catch (e) {
                                                    if (!isXMLSuffix) {
                                                        reject = true;
                                                        def.reject(parseUtils.cleanupError(e));
                                                    }
                                                }
                                            }
                                            zipFile.readEntry();

                                        });
                                    }
                                });
                            }
                        });
                        // End of all zip files
                        zipFile.on('end', function() {
                            def.resolve(files);
                        });
                    }
                });
            } else {
                var file = {
                    filename: filename,
                    fullName: filename,
                    type: 'wsdl',
                    content: rawContent,
                    context: 'cmd'
                };
                try {
                    if (rawContent == filename) {
                        file.filename = 'MEMORY';
                        file.fullName = 'MEMORY';
                    }
                    if (file.content == null || file.content.length == 0) {
                        throw new Error('The file has no content: ' + file.filename);
                    }
                    parseUtils.contentToXMLorWSDL(file);
                    if (file.json.definitions) {
                        var wsdlGood = parseUtils.validateWSDLJson(file.json, file.filename);
                        if (wsdlGood.valid) {
                            files.push(file);
                            file.type = 'wsdl';
                            var namespaces = file.doc.definitions.xmlns;
                            file.namespaces = namespaces;
                        } else {
                            throw new Error(wsdlGood.reason);
                        }
                    } else if (file.json.schema) {
                        files.push(file);
                        file.type = 'xsd';
                        file.namespaces = {};
                    } else {
                        throw new Error('Failed to parse WSDL or XSD: ' + file.filename);
                    }
                    // have to capture the service children elements (ports) here as they get munged later
                    file.serviceChildren = postParse.captureServiceChildren(file.doc);
                    postParse.updateSchemaNamespaces(file);
                    def.resolve(files);
                } catch (e) {
                    def.reject(parseUtils.cleanupError(e));
                }
            }
        }, function(fileErr) {