gpt4 book ai didi

zip - 如何使用 zip.js 将多个文件添加到一个 zip 中?

转载 作者:数据小太阳 更新时间:2023-10-29 05:10:28 38 4
gpt4 key购买 nike

我正在使用 javascript zip.js图书馆。我到处搜索,但找不到将多个文件添加到 zip 的示例。

这是我的代码,但它生成了一个“损坏的”zip。

var len = results.rows.length, i;
var k=1;
zip.createWriter(new zip.BlobWriter(), function(writer) {
for (i = 0; i < len; i++){
// get the image url from a sqlite request
url = results.rows.item(i).url;


var img = new Image();
img.onload = function() {
var a = document.createElement('a');
a.href = this.src;
var filename= a.pathname.split('/').pop(); // filename.php
timest = new Date().getTime();
// use a TextReader to read the String to add

writer.add(timest+".jpg", new zip.Data64URIReader(getBase64Image(img)), function() {
// onsuccess callback
k++;
if(k==len){
setTimeout(function(){
writer.close(function(blob) {

// blob contains the zip file as a Blob object
$('#test').attr("href", window.URL.createObjectURL(blob));
$('#test').attr("download", "woeii.zip");

});
},1000);
}
}, function(currentIndex, totalIndex) {
// onprogress callback
});



};
img.src = url;
}
});

有什么让它发挥作用的想法吗? :)

最佳答案

如果您正在寻找处理多个文件的代码示例,see here .然后你可以 view the source code .

这是演示的主要来源(略有修改):

var obj = this;
var model = (function() {
var zipFileEntry, zipWriter, writer, creationMethod, URL = obj.webkitURL || obj.mozURL || obj.URL;

return {
setCreationMethod : function(method) {
creationMethod = method;
},
addFiles : function addFiles(files, oninit, onadd, onprogress, onend) {
var addIndex = 0;

function nextFile() {
var file = files[addIndex];
onadd(file);
// Modified here to use the Data64URIReader instead of BlobReader
zipWriter.add(file.name, new zip.Data64URIReader(file.data), function() {
addIndex++;
if (addIndex < files.length)
nextFile();
else
onend();
}, onprogress);
}

function createZipWriter() {
zip.createWriter(writer, function(writer) {
zipWriter = writer;
oninit();
nextFile();
}, onerror);
}

if (zipWriter)
nextFile();
else if (creationMethod == "Blob") {
writer = new zip.BlobWriter();
createZipWriter();
} else {
createTempFile(function(fileEntry) {
zipFileEntry = fileEntry;
writer = new zip.FileWriter(zipFileEntry);
createZipWriter();
});
}
},
getBlobURL : function(callback) {
zipWriter.close(function(blob) {
var blobURL = creationMethod == "Blob" ? URL.createObjectURL(blob) : zipFileEntry.toURL();
callback(blobURL);
zipWriter = null;
});
},
getBlob : function(callback) {
zipWriter.close(callback);
}
};
})();

用法:假设 <a id="downloadLink">Download</a>元素存在以在准备就绪后提供下载。

// Prepare your images
var files = [];
for (i = 0; i < len; i++) {

// Get the image URL from a SQLite request
var url = results.rows.item(i).url;

(function(url){
var img = new Image();
img.onload = function() {
// Add to file array [{name, data}]
var a = document.createElement('a');
a.href = this.src;
var filename= a.pathname.split('/').pop();

console.log("Loaded file " + filename);
files.push({name: filename, data: getBase64Image(img) });
}
img.src = url;
})(url);
}

// Wait for the image to load
var check = setInterval(function(){
if(files.length==images.length) {
clearInterval(check);

// Set the mode
model.setCreationMethod("Blob");

// Add the files to the zip
model.addFiles(files,
function() {
// Initialise Method
console.log("Initialise");
}, function(file) {
// OnAdd
console.log("Added file");
}, function(current, total) {
// OnProgress
console.log("%s %s", current, total);
}, function() {
// OnEnd
// The zip is ready prepare download link
// <a id="downloadLink" href="blob:url">Download Zip</a>
model.getBlobURL(function(url) {
document.getElementById("downloadLink").href = url;
document.getElementById("downloadLink").style.display = "block";
document.getElementById("downloadLink").download = "filename.zip";
});
});

}
}, 500);

您可以使用示例源代码添加进度指示器。希望这会有所帮助,这种方法的好处是,如果您将其设为自己的 JS 文件,则 zip 模型很容易重用。


另一个想法:我假设你正在使用 getBase64Image函数 from here ,如果是这样并且您仍然遇到损坏问题,也许尝试将返回修改为简单的 return dataURL;并注释掉 .replace(... , 作为 Data64URIReader可能需要前缀。

关于zip - 如何使用 zip.js 将多个文件添加到一个 zip 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17510979/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com