help
help copied to clipboard
unzip a file in sync without callback
- Node.js Version: 12.14.1
- OS: windows
- Scope (install, code, runtime, meta, other?):
- Module (and version) (if relevant): fs
In this, I am planning to unzip a file and rename the file so that my require will find the correct .node binary.
But what happened was the rename file function is called even before the zip is done. How to handle that so that rename will be called only when zip extract is done.
If I use call back...require('') is not working. execution stops at require('') line.
var BUILD_FILE = path.resolve(CURRENT_DIR, 'node_modules\/ibm_db\/build.zip');
var output = path.resolve(CURRENT_DIR, 'node_modules\/ibm_db')
fs.createReadStream(BUILD_FILE).pipe(unzipper.Extract({ path: output }));
findElectronVersion();
console.log(`after findEversion ${electron_version}`);
var ODBC_BINDINGS = path.resolve(CURRENT_DIR, 'node_modules\/ibm_db\/build\/Release\/odbc_bindings.node');
var old_path = path.resolve(CURRENT_DIR, 'node_modules\/ibm_db\/build\/Release\/odbc_bindings_e' + electron_version + '.node');
fs.renameSync(old_path, ODBC_BINDINGS)
const MainController = require('./src/controllers/mainController');
Thanks, Akhil
the stream.pipe function is async, which means that while its unzipping js is executing the rest of the code. If you want to execute the rest after the unzipping finished, you can add an 'end' eventlistener on the readable stream like this:
const readStream = fs.createReadStream(BUILD_FILE);
readStream.pipe(unzipper.Extract({ path: output }));
readStream.on('end', () => {
console.log("Unzipping finished!");
// execute the code that should run after the unzip here
});
see: https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
There is no way to do it without a callback. You could wrap it with a promise and await it, but that at the end is nothing but a callback with more syntax sugar.
I think the real problem is that your require for some reason does not work in the callback.
Hi @dark-magic
Thanks for the comment.
I tried that. When I try to require('')(my code contains this) inside the end event...execution stops.
-Akhil
Sorry I missed your edit
@dark-magic If I use promise and await I need to declare this function as async right?
All this code goes to a function which cannot be declared as async.
Thanks, Akhil
yeah the function has to be async then. I recommend doing it with a callback and trying to solve that problem with require
What about using the zlib node native lib?
const fs = require('fs')
const zlib = require('zlib')
const inputFile = fs.createReadStream('file.ext.gz')
const outputFile = fs.createWriteStream('file.ext')
const stream = inputFile.pipe(zlib.createUnzip()).pipe(outputFile)
stream.on('finish', () => console.log('finished'))
if you really want everything sync
(async () => {
const fs = require('fs')
const zlib = require('zlib')
const inputFile = fs.createReadStream('file.ext.gz')
const outputFile = fs.createWriteStream('file.ext')
const stream = inputFile.pipe(zlib.createUnzip()).pipe(outputFile)
await new Promise(resolve => stream.on('finish', resolve))
})()
It seems there has been no activity on this issue for a while, and it is being closed in 30 days. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.
It seems there has been no activity on this issue for a while, and it is being closed. If you believe this issue should remain open, please leave a comment. If you need further assistance or have questions, you can also search for similar issues on Stack Overflow. Make sure to look at the README file for the most updated links.