ZipUtils.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*--
  2. Copyright 2009-2010 by Stefan Rusterholz.
  3. All rights reserved.
  4. You can choose between MIT and BSD-3-Clause license. License file will be added later.
  5. --*/
  6. /**
  7. * mixin cc.Codec
  8. */
  9. cc.Codec = {name:'Jacob__Codec'};
  10. /**
  11. * Unpack a gzipped byte array
  12. * @param {Array} input Byte array
  13. * @returns {String} Unpacked byte string
  14. */
  15. cc.unzip = function () {
  16. return cc.Codec.GZip.gunzip.apply(cc.Codec.GZip, arguments);
  17. };
  18. /**
  19. * Unpack a gzipped byte string encoded as base64
  20. * @param {String} input Byte string encoded as base64
  21. * @returns {String} Unpacked byte string
  22. */
  23. cc.unzipBase64 = function () {
  24. var tmpInput = cc.Codec.Base64.decode.apply(cc.Codec.Base64, arguments);
  25. return cc.Codec.GZip.gunzip.apply(cc.Codec.GZip, [tmpInput]);
  26. };
  27. /**
  28. * Unpack a gzipped byte string encoded as base64
  29. * @param {String} input Byte string encoded as base64
  30. * @param {Number} bytes Bytes per array item
  31. * @returns {Array} Unpacked byte array
  32. */
  33. cc.unzipBase64AsArray = function (input, bytes) {
  34. bytes = bytes || 1;
  35. var dec = this.unzipBase64(input),
  36. ar = [], i, j, len;
  37. for (i = 0, len = dec.length / bytes; i < len; i++) {
  38. ar[i] = 0;
  39. for (j = bytes - 1; j >= 0; --j) {
  40. ar[i] += dec.charCodeAt((i * bytes) + j) << (j * 8);
  41. }
  42. }
  43. return ar;
  44. };
  45. /**
  46. * Unpack a gzipped byte array
  47. * @param {Array} input Byte array
  48. * @param {Number} bytes Bytes per array item
  49. * @returns {Array} Unpacked byte array
  50. */
  51. cc.unzipAsArray = function (input, bytes) {
  52. bytes = bytes || 1;
  53. var dec = this.unzip(input),
  54. ar = [], i, j, len;
  55. for (i = 0, len = dec.length / bytes; i < len; i++) {
  56. ar[i] = 0;
  57. for (j = bytes - 1; j >= 0; --j) {
  58. ar[i] += dec.charCodeAt((i * bytes) + j) << (j * 8);
  59. }
  60. }
  61. return ar;
  62. };
  63. /**
  64. * string to array
  65. * @param {String} input
  66. * @returns {Array} array
  67. */
  68. cc.StringToArray = function (input) {
  69. var tmp = input.split(","), ar = [], i;
  70. for (i = 0; i < tmp.length; i++) {
  71. ar.push(parseInt(tmp[i]));
  72. }
  73. return ar;
  74. };