Extracting zipped files using JSZIP in javascript

后端 未结 3 1400
南笙
南笙 2021-02-04 13:21

In my webpage, a user is supposed to upload a zipped file. Within the zipped file are 2 files: another zip file and a txt file. On my server, after receiving the zip, I want to

3条回答
  •  旧巷少年郎
    2021-02-04 14:24

    It took a bit of digging in their documentation but they have an example that shows how to read the file contents from a ZIP.

    You are getting the object that describes the ZIP contents but not the actual content. Here is an adjusted version:

    var JSZip = require('JSZip');
    
    fs.readFile(filePath, function(err, data) {
        if (!err) {
            var zip = new JSZip();
            zip.loadAsync(data).then(function(contents) {
                Object.keys(contents.files).forEach(function(filename) {
                    zip.file(filename).async('nodebuffer').then(function(content) {
                        var dest = path + filename;
                        fs.writeFileSync(dest, content);
                    });
                });
            });
        }
    });
    

提交回复
热议问题