Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

200
Vistas
Zip archive with nested folder inside does not unzip with yauzl

I am writing software which, among other things, downloads a zip archive using the Dropbox API and then unzips that archive using yauzl.

The way the files are stored and downloaded from DB often ends up with nested folders, and I need to keep it that way.

However my implementation of yauzl is not capable of unzipping while keeping that nested folder structure, if there is a nested folder in the archive it does not unzip at all.

Here is my unzip function, which is the default yauzl example with the addition of local file write at the end.

const unzip = () => {
    let zipPath = "pathToFile.zip"
    let extractPath = "pathToExtractLocation"

        yauzl.open(zipPath, {lazyEntries: true}, function(err, zipfile) {
            if (err) throw err;
            zipfile.readEntry();
            zipfile.on("entry", function(entry) {
            if (/\/$/.test(entry.fileName)) {
                // Directory file names end with '/'.
                // Note that entries for directories themselves are optional.
                // An entry's fileName implicitly requires its parent directories to exist.
                zipfile.readEntry();
            } else {
                // file entry
                zipfile.openReadStream(entry, function(err, readStream) {
                if (err) throw err;
                readStream.on("end", function() {
                    zipfile.readEntry();
                });
                const writer = fs.createWriteStream(path.join(extractPath, entry.fileName));
                readStream.pipe(writer);
                });
            }
            });
        });
}

Removing the if (/\/$/.test(entry.fileName)) check treats the top level folder as a file, extracting it with no file extension and 0kb size. What I want it to do is extract the archive including subfolders (to at least a depth of 2, being aware of the risk of zip bombing).

Is that possible using yauzl?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The code needs to create the directory tree at the extract path. You may use fs.mkdir with the recursive option to ensure that a directory exists before extract to it.

if (/\/$/.test(entry.fileName)) {
  // Directory file names end with '/'.
  // Note that entries for directories themselves are optional.
  // An entry's fileName implicitly requires its parent directories to exist.
  zipfile.readEntry();
} else {
  // file entry
  fs.mkdir(
    path.join(extractPath, path.dirname(entry.fileName)),
    { recursive: true },
    (err) => {
      if (err) throw err;
      zipfile.openReadStream(entry, function (err, readStream) {
        if (err) throw err;
        readStream.on("end", function () {
          zipfile.readEntry();
        });
        const writer = fs.createWriteStream(
          path.join(extractPath, entry.fileName)
        );
        readStream.pipe(writer);
      });
    }
  );
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda