No Description

index.js 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. var compressions = require('../compressions');
  3. var ZipFileWorker = require('./ZipFileWorker');
  4. /**
  5. * Find the compression to use.
  6. * @param {String} fileCompression the compression defined at the file level, if any.
  7. * @param {String} zipCompression the compression defined at the load() level.
  8. * @return {Object} the compression object to use.
  9. */
  10. var getCompression = function (fileCompression, zipCompression) {
  11. var compressionName = fileCompression || zipCompression;
  12. var compression = compressions[compressionName];
  13. if (!compression) {
  14. throw new Error(compressionName + " is not a valid compression method !");
  15. }
  16. return compression;
  17. };
  18. /**
  19. * Create a worker to generate a zip file.
  20. * @param {JSZip} zip the JSZip instance at the right root level.
  21. * @param {Object} options to generate the zip file.
  22. * @param {String} comment the comment to use.
  23. */
  24. exports.generateWorker = function (zip, options, comment) {
  25. var zipFileWorker = new ZipFileWorker(options.streamFiles, comment, options.platform, options.encodeFileName);
  26. var entriesCount = 0;
  27. try {
  28. zip.forEach(function (relativePath, file) {
  29. entriesCount++;
  30. var compression = getCompression(file.options.compression, options.compression);
  31. var compressionOptions = file.options.compressionOptions || options.compressionOptions || {};
  32. var dir = file.dir, date = file.date;
  33. file._compressWorker(compression, compressionOptions)
  34. .withStreamInfo("file", {
  35. name : relativePath,
  36. dir : dir,
  37. date : date,
  38. comment : file.comment || "",
  39. unixPermissions : file.unixPermissions,
  40. dosPermissions : file.dosPermissions
  41. })
  42. .pipe(zipFileWorker);
  43. });
  44. zipFileWorker.entriesCount = entriesCount;
  45. } catch (e) {
  46. zipFileWorker.error(e);
  47. }
  48. return zipFileWorker;
  49. };