I wonder to get zipped data from an URL (https://developers.google.com/transit/gtfs/examples/sample-feed.zip) and convert each csv file to an Javascript Array of Objects binding.
For example, from routes.txt, I want to get:
routes = [
{
route_id: 'AB'
agency_id: 'DTA'
route_short_name: '10'
route_long_name: 'Airport - Bullfrog'
route_desc: null
route_type: 3
route_url: null
route_color: null
route_text_color: null
},
{
route_id: 'BFC'
agency_id: 'DTA'
route_short_name: '20'
route_long_name: 'Bullfrog - Furnace Creek Resort'
route_desc: null
route_type: 3
route_url: null
route_color: null
route_text_color: null
}
...
]
All operations should be done directly in browser.
First, I tried to use Zip.js, but I think I don't know how to control promise use
let GTFS_URL = `http://sjc.infobus.engdb.com.br/infoponto/google_transit.zip`
const GTFS_ZIP = new zip.ZipReader(new zip.HttpReader(GTFS_URL))
const entries = await GTFS_ZIP.getEntries()
console.log(entries)
if (entries.length) {
// get first entry content as text by using a TextWriter
const text = await entries[0].getData(
// writer
new zip.TextWriter(),
// options
{
onprogress: (index, max) => {
// onprogress callback
}
}
);
// text contains the entry data as a String
console.log(text);
}
// close the ZipReader
await reader.close();
}
)
Uncaught SyntaxError: await is only valid in async functions and the top level bodies of modules
Then, I tried JSzip solution
let GTFS_URL = `https://developers.google.com/transit/gtfs/examples/sample-feed.zip`
var zip = new JSZip();
zip.loadAsync(GTFS_URL)
.then(function (GTFS_URL) {
console.log(GTFS_URL)
alert("OK")
}, function () {
alert("Not a valid zip file")
});
I saw a lot of similar questions here in stackoverflow, but I couldn't get any of these to work the way I need