javascriptnode.jsnode.js-fs

How to wait till all compressed files are extracted


For the below posted code in section code-1, I extract the contents of .tar file to a specific path. Then I want to rename the the extracted files as shown in the code. The number of extracted files are six, and I can see them listed in the vs-code project explorer. The propblem i am having is, the exectuion of the code of .on('finish') is faster than the process of extraction of the compressed files of the tar. At run time, all the renaming processes are completed except the last one mentioned in the code, despite the project explorer of vs-code shows that the extracted file with name SysConstants.CONST_BAND_5_NAME.description is listed.

To solve this issue, i wrapped the code of the rename process inside setTimeout, and executed the code, then all the six extracted files got renamed. So I concluded, that the extraction process is still getting occuring while the execution of the code of the rename process is completed.

In the code shown in section code-2, is my attempt to solve this issue but it also did not work

How can force the rename process to occure after all the six files are extracted

code-1:

onst tarStream = fs.createReadStream(pathToTarFileTobeExtracted);
tarStream.pipe(tarFs.extract(untarPath + outputFolderPerUntared + '\\' + outputFolderPerUntaredForTS2))
.on('finish', () => {
    setTimeout(() => {
        fs.renameSync(
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BANDS_COMPOSITE_NAME.description,
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[0]);
        fs.renameSync(
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_1_NAME.description,
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[1]);
        fs.renameSync(
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_2_NAME.description,
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[2]);
        fs.renameSync(
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_3_NAME.description,
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[3]);
        fs.renameSync(
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_4_NAME.description,
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[4]);
        fs.renameSync(
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_5_NAME.description,
            untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[5]);

            
        console.log('xcx ***** old:', untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_5_NAME.description)
        console.log('xcx ***** new:', untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[5])
    }, 1000);
})

code-2

Attempt to solve the problem

const tarStream = fs.createReadStream(pathToTarFileTobeExtracted);
        const extractedStream = tarFs.extract(untarPath + outputFolderPerUntared + '\\' + outputFolderPerUntaredForTS2);
        tarStream.pipe(extractedStream);
        extractedStream
        .on('close', () => {
            fs.renameSync(
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BANDS_COMPOSITE_NAME.description,
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[0]);
            fs.renameSync(
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_1_NAME.description,
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[1]);
            fs.renameSync(
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_2_NAME.description,
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[2]);
            fs.renameSync(
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_3_NAME.description,
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[3]);
            fs.renameSync(
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_4_NAME.description,
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[4]);
            fs.renameSync(
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_5_NAME.description,
                untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[5]);

                
            console.log('xcx ***** old:', untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + SysConstants.CONST_BAND_5_NAME.description)
            console.log('xcx ***** new:', untarPath + outputFolderPerUntared + '\\' +  outputFolderPerUntaredForTS2 + '\\' + titlesOfBandsAsGeoTIFFForTS2[5])
        })

explained above in the question


Solution

  • I solved it be using const tar = require('tar'); lib, as shown below in the code

    code:

     tar.x({
        file: pathToTarFileTobeExtracted,
        cwd: untarPath + outputFolderPerUntared + '\\' + outputFolderPerUntaredForTS2,
        sync: false,
    })