|
| 1 | +// Copyright Joyent, Inc. and other Node contributors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a |
| 4 | +// copy of this software and associated documentation files (the |
| 5 | +// "Software"), to deal in the Software without restriction, including |
| 6 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 7 | +// distribute, sublicense, and/or sell copies of the Software, and to permit |
| 8 | +// persons to whom the Software is furnished to do so, subject to the |
| 9 | +// following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included |
| 12 | +// in all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
| 17 | +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 18 | +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 19 | +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 20 | +// USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + |
| 22 | +// test unzipping a file that was created by concatenating multiple gzip |
| 23 | +// streams. |
| 24 | + |
| 25 | +var common = require('../common'); |
| 26 | +var assert = require('assert'); |
| 27 | +var zlib = require('zlib'); |
| 28 | + |
| 29 | +var util = require('util'); |
| 30 | + |
| 31 | +var HUGE = 64 * 1024; |
| 32 | + |
| 33 | +var originalBuffer = new Buffer(3 * HUGE); |
| 34 | +var originalOffset = 0; |
| 35 | + |
| 36 | +var gzipBuffer = new Buffer(3 * HUGE); |
| 37 | +var gzipOffset = 0; |
| 38 | + |
| 39 | +function getRandomLetter() { |
| 40 | + return (Math.random() * (122 - 97)) + 97; |
| 41 | +} |
| 42 | + |
| 43 | +function generateHugeStream() { |
| 44 | + var buffer = new Buffer(HUGE); |
| 45 | + for (var i = 0; i < HUGE; i++) |
| 46 | + buffer.writeUInt8(getRandomLetter(), i); |
| 47 | + |
| 48 | + buffer.copy(originalBuffer, originalOffset); |
| 49 | + originalOffset += HUGE; |
| 50 | + |
| 51 | + return buffer; |
| 52 | +} |
| 53 | + |
| 54 | +function gzipAppend(data) { |
| 55 | + data.copy(gzipBuffer, gzipOffset); |
| 56 | + gzipOffset += data.length; |
| 57 | +} |
| 58 | + |
| 59 | +function writeGzipStream(text, cb) { |
| 60 | + var gzip = zlib.createGzip(); |
| 61 | + gzip.on('data', gzipAppend); |
| 62 | + gzip.write(text, function() { |
| 63 | + gzip.flush(function() { |
| 64 | + gzip.end(function() { |
| 65 | + cb(); |
| 66 | + }); |
| 67 | + }); |
| 68 | + }); |
| 69 | +} |
| 70 | + |
| 71 | +writeGzipStream(generateHugeStream(), function() { |
| 72 | + writeGzipStream(generateHugeStream(), function() { |
| 73 | + writeGzipStream(generateHugeStream(), function() { |
| 74 | + var gunzip = zlib.createGunzip(); |
| 75 | + var gunzippedData = new Buffer(3 * HUGE); |
| 76 | + var gunzippedOffset = 0; |
| 77 | + gunzip.on('data', function (data) { |
| 78 | + data.copy(gunzippedData, gunzippedOffset); |
| 79 | + gunzippedOffset += data.length; |
| 80 | + }); |
| 81 | + gunzip.on('end', function() { |
| 82 | + var gunzippedStr = gunzippedData.toString('utf8', 0, gunzippedOffset); |
| 83 | + var originalStr = originalBuffer.toString('utf8', 0, 3 * HUGE); |
| 84 | + |
| 85 | + assert.equal(gunzippedStr, originalStr); |
| 86 | + }); |
| 87 | + |
| 88 | + gunzip.write(gzipBuffer.slice(0, gzipOffset), 'binary', function() { |
| 89 | + gunzip.end(); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments