Stuk / jszip

Create, read and edit .zip files with Javascript
https://stuk.github.io/jszip/
Other
9.81k stars 1.3k forks source link

randomly missing files #588

Open warlock257 opened 5 years ago

warlock257 commented 5 years ago

Hi. I'm using jszip to zip 6 different folders with multiple files in each. The result of my code, is a generated file, but only some of the contents are there. If there's less folders, it seems to do better, but if there's something in each folder, I'm lucky to get 4 of the 6. Here's my code:

app.post('/zip', (req, res) =>{
  console.log(req.body)
  userName = req.body.userName
  let zipPath = `./public/uploads/${userName}`
  console.log("ZIP user name set to " + userName)
  console.log("zip path set to: "+ zipPath)

  let zip = new JSZip();

    if (fs.existsSync(`${zipPath}/chrono`)){
      let chronoFolder = zip.folder(`chrono`)
      let chronofiles = fs.readdirSync(`./public/uploads/${userName}/chrono/`)
      chronofiles.forEach(function(file){
        chronoFolder.file(file, fs.readFileSync(`./${zipPath}/chrono/${file}`)); 
      })
    }
    if (fs.existsSync(`${zipPath}/family`)){
      let familyFolder = zip.folder(`family`)
      let familyfiles = fs.readdirSync(`./public/uploads/${userName}/family/`)
      familyfiles.forEach(function(file){
        familyFolder.file(file, fs.readFileSync(`./${zipPath}/family/${file}`)); 
      })
    }
    if (fs.existsSync(`${zipPath}/extended`)){
      let extFolder = zip.folder(`extended`)
      let extfiles = fs.readdirSync(`./public/uploads/${userName}/extended/`)
      extfiles.forEach(function(file){
        extFolder.file(file, fs.readFileSync(`./${zipPath}/extended/${file}`)); 
      })
    }
    if (fs.existsSync(`${zipPath}/friends`)){
      let friendsFolder = zip.folder(`friends`)
      let friendsfiles = fs.readdirSync(`./public/uploads/${userName}/friends/`)
      friendsfiles.forEach(function(file){
        friendsFolder.file(file, fs.readFileSync(`./${zipPath}/friends/${file}`)); 
      })
    }
    if (fs.existsSync(`${zipPath}/fun`)){
      let funFolder = zip.folder(`fun`)
      let funfiles = fs.readdirSync(`./public/uploads/${userName}/fun/`)
      funfiles.forEach(function(file){
        funFolder.file(file, fs.readFileSync(`./${zipPath}/fun/${file}`)); 
      })
    }
    if (fs.existsSync(`${zipPath}/unsorted`)){
      let unsortedFolder = zip.folder(`unsorted`)
      let unsortedfiles = fs.readdirSync(`./public/uploads/${userName}/unsorted/`)
      unsortedfiles.forEach(function(file){
        unsortedFolder.file(file, fs.readFileSync(`./${zipPath}/unsorted/${file}`)); 
      })
    }

    zip
    .generateNodeStream({type:'nodebuffer',streamFiles:true})
    .pipe(fs.createWriteStream(`public/uploads/${userName}/${userName}-photos.zip`))
    .on('finish', function () {
        console.log("photos.zip written.");

        res.send(`http://localhost:8080/uploads/${userName}/${userName}-photos.zip`);
    });
})
joeyhub commented 5 years ago

I'm getting this as well. I wonder if it's throwing an error when the file isn't there?