首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >提取Node.js jszip库

提取Node.js jszip库
EN

Stack Overflow用户
提问于 2015-03-13 18:02:11
回答 2查看 4K关注 0票数 6

我正在编写一些节点代码,并使用jszip压缩和解压缩一些文件。我知道如何压缩,但不知道如何解压缩或解压缩。堆栈溢出上有几个链接不起作用。有人找到解决办法了吗?以下是我尝试过的

代码语言:javascript
复制
var fs = require('fs');
var JSZip   = require('jszip');
var zipName = "C:/test.zip";
var unzip = "C:/unzip";


fs.readFile(zipName, function (err, data) {
    if (err) throw err;
    var zip = new JSZip();
    zip.folder(unzip).load(data);
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-13 19:37:56

JSZip没有在磁盘上写入文件的方法。为此,您需要在zip.files上进行迭代:

代码语言:javascript
复制
var path = require("path");
Object.keys(zip.files).forEach(function(filename) {
  var content = zip.files[filename].asNodeBuffer();
  var dest = path.join(unzip, filename);
  fs.writeFileSync(dest, content);
}

在zip文件中,文件夹用斜杠'/‘表示,我认为path.join()会创建正确的路径,但我不能测试这个路径。

票数 9
EN

Stack Overflow用户

发布于 2017-04-24 21:02:06

可能是由于我对asNodeBuffer的错误实现和对JS缺乏经验,但我一直错误地提取文件。我想分享我的工作结果,我用一个250+ MB文件测试了它。

代码语言:javascript
复制
...
fs.readFile(tmpFilePath, function (err, data) {
    if (err) {
        throw err;
    }
    logger.debug('[method] Before extracting file...');
    JSZip.loadAsync(data).then(function (zip) {
        var files = Object.keys(zip.files);
        logger.debug('[method] files to be created ' + files.length);
        // in my case, the folders where not being created while "inflating" the content. created the folders in a separated loop 
        // O(n) for those geeks on complexity. 
        createDirectories(files);
        createFiles(files, zip, someOtherFunctionReference);
    }).catch(function (err) {
        deferred.reject(err);
    });
});
...

/**
 * Sync opperation to create the folders required for the files.
 * @param files
 */
function createDirectories(files) {
    files.forEach(function (filename) {
        var dest = path.join(folderName, filename);
        ensureDirectoryExistence(dest);
    });
}

/**
 * recursive create directory function
 * @param filePath
 * @returns {boolean}
 */
function ensureDirectoryExistence(filePath) {
    var dirname = path.dirname(filePath);
    if (fs.existsSync(dirname)) {
        return true;
    }
    ensureDirectoryExistence(dirname);
    fs.mkdirSync(dirname);
}

/**
 * Create files sync or blocking
 * @param files
 * @param zip
 * @param cb
 */
function createFiles(files, zip, cb) {
    try {
        var countFilesCreated = 0;
        files.forEach(function (filename) {
            var dest = path.join(folderName, filename);
            // skip directories listed
            if (dest.charAt(dest.length - 1) === '/') {
                countFilesCreated++;
                return;
            }
            return zip.file(filename).async('nodebuffer').then(function(content){
                // var content = zip.files[filename].nodeStream();
                fs.writeFileSync(dest, content);
                countFilesCreated++;
                // proably someone with more experience in JS can implement a promice like solution. 
                // I thought that in here instead of the counter we coud use an indexOf to return an error in case not all the elements where created correctly. 
                // but if a file throw an error, its handled by the catch ... 
                if (countFilesCreated >= files.length) {
                    logger.debug('All files created!!');
                    cb();
                }
            });
        });
    } catch (err) {
        throw err;
    }
}

我希望这能帮到你。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29038985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档