help icon indicating copy to clipboard operation
help copied to clipboard

unzip a file in sync without callback

Open akhilravuri1 opened this issue 4 years ago • 7 comments

  • 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

akhilravuri1 avatar Oct 28 '20 13:10 akhilravuri1

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.

Lancear avatar Oct 29 '20 08:10 Lancear

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

akhilravuri1 avatar Oct 29 '20 08:10 akhilravuri1

Sorry I missed your edit

akhilravuri1 avatar Oct 29 '20 08:10 akhilravuri1

@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

akhilravuri1 avatar Oct 29 '20 08:10 akhilravuri1

yeah the function has to be async then. I recommend doing it with a callback and trying to solve that problem with require

Lancear avatar Oct 29 '20 10:10 Lancear

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))
})()

jfoclpf avatar Sep 21 '22 21:09 jfoclpf

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.

github-actions[bot] avatar May 11 '24 01:05 github-actions[bot]

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.

github-actions[bot] avatar Jun 10 '24 01:06 github-actions[bot]