Sadly you can't pipe the response stream into the unzip job as node zlib
lib allows you to do, you have to cache and wait the end of the response. I suggest you to pipe the response to a fs
stream in case of big files, otherwise you will full fill your memory in a blink!
I don't completely understand what you are trying to do, but imho this is the best approach. You should keep your data in memory only the time you really need it, and then stream to the csv parser.
If you want to keep all your data in memory you can replace the csv parser method fromPath
with from
that takes a buffer instead and in getData return directly unzipped
You can use the AMDZip
(as @mihai said) instead of node-zip
, just pay attention because AMDZip
is not yet published in npm so you need:
$ npm install git://github.com/cthackers/adm-zip.git
N.B. Assumption: the zip file contains only one file
var request = require('request'),
fs = require('fs'),
csv = require('csv')
NodeZip = require('node-zip')
function getData(tmpFolder, url, callback) {
var tempZipFilePath = tmpFolder + new Date().getTime() + Math.random()
var tempZipFileStream = fs.createWriteStream(tempZipFilePath)
request.get({
url: url,
encoding: null
}).on('end', function() {
fs.readFile(tempZipFilePath, 'base64', function (err, zipContent) {
var zip = new NodeZip(zipContent, { base64: true })
Object.keys(zip.files).forEach(function (filename) {
var tempFilePath = tmpFolder + new Date().getTime() + Math.random()
var unzipped = zip.files[filename].data
fs.writeFile(tempFilePath, unzipped, function (err) {
callback(err, tempFilePath)
})
})
})
}).pipe(tempZipFileStream)
}
getData('/tmp/', 'http://bdn-ak.bloomberg.com/precanned/Comdty_Calendar_Spread_Option_20120428.txt.zip', function (err, path) {
if (err) {
return console.error('error: %s' + err.message)
}
var metadata = []
csv().fromPath(path, {
delimiter: '|',
columns: true
}).transform(function (data){
// do things with your data
if (data.NAME[0] === '#') {
metadata.push(data.NAME)
} else {
return data
}
}).on('data', function (data, index) {
console.log('#%d %s', index, JSON.stringify(data, null, ' '))
}).on('end',function (count) {
console.log('Metadata: %s', JSON.stringify(metadata, null, ' '))
console.log('Number of lines: %d', count)
}).on('error', function (error) {
console.error('csv parsing error: %s', error.message)
})
})
zlib
doesn't handle zip file format, it only handles gzip and deflate formats. Thezlib.unzip
function is misleadingly named as it only decompresses gzip and deflate formats. You need a zip format library.zlib
format (which in turn uses deflate). But that's totally irrelevant here, so +1 :-)request.get
which automatically callstoString()
on the returned data. Butadam-zip
requires aBuffer
not aString
. Userequest({url: url, encoding: null}, function(err, res, zipFile) { ...
instead ofrequest.get
to makerequest
return aBuffer
. (Although when I did that I got aCRC32 checksum failed
error :( You should really just skiprequest
and use mihai's answer from below.