我正在使用html5/js混合移動應用程序。它有一個功能下載zip文件,然後解壓縮它們。下載功能不是問題,但我不知道如何解壓縮文件(使用JavaScript)。 很多人都提到zip.js,但它似乎只讀zip文件(不解壓縮/解壓縮到新文件夾)如何解壓JavaScript文件
非常感謝,如果有人能幫助我!
我正在使用html5/js混合移動應用程序。它有一個功能下載zip文件,然後解壓縮它們。下載功能不是問題,但我不知道如何解壓縮文件(使用JavaScript)。 很多人都提到zip.js,但它似乎只讀zip文件(不解壓縮/解壓縮到新文件夾)如何解壓JavaScript文件
非常感謝,如果有人能幫助我!
看看zip.js文檔和demo page。還請注意使用JavaScript filesystem API來讀取/寫入文件並創建臨時文件。
如果zip文件包含多個條目,您可以閱讀zip文件條目並顯示一個鏈接表來下載每個單獨的文件,如上面的演示。
如果你看一下演示頁面的源代碼,你看到下面的代碼(代碼從zip.js Github上演示頁粘貼)(我已經添加了註釋來解釋):
function(obj) {
//Request fileSystemObject from JavaScript library for native support
var requestFileSystem = obj.webkitRequestFileSystem || obj.mozRequestFileSystem || obj.requestFileSystem;
function onerror(message) {
alert(message);
}
//Create a data model to handle unzipping and downloading
var model = (function() {
var URL = obj.webkitURL || obj.mozURL || obj.URL;
return {
getEntries : function(file, onend) {
zip.createReader(new zip.BlobReader(file), function(zipReader) {
zipReader.getEntries(onend);
}, onerror);
},
getEntryFile : function(entry, creationMethod, onend, onprogress) {
var writer, zipFileEntry;
function getData() {
entry.getData(writer, function(blob) {
var blobURL = creationMethod == "Blob" ? URL.createObjectURL(blob) : zipFileEntry.toURL();
onend(blobURL);
}, onprogress);
}
//Write the entire file as a blob
if (creationMethod == "Blob") {
writer = new zip.BlobWriter();
getData();
} else {
//Use the file writer to write the file clicked by user.
createTempFile(function(fileEntry) {
zipFileEntry = fileEntry;
writer = new zip.FileWriter(zipFileEntry);
getData();
});
}
}
};
})();
(function() {
var fileInput = document.getElementById("file-input");
var unzipProgress = document.createElement("progress");
var fileList = document.getElementById("file-list");
var creationMethodInput = document.getElementById("creation-method-input");
//The download function here gets called when the user clicks on the download link for each file.
function download(entry, li, a) {
model.getEntryFile(entry, creationMethodInput.value, function(blobURL) {
var clickEvent = document.createEvent("MouseEvent");
if (unzipProgress.parentNode)
unzipProgress.parentNode.removeChild(unzipProgress);
unzipProgress.value = 0;
unzipProgress.max = 0;
clickEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
a.href = blobURL;
a.download = entry.filename;
a.dispatchEvent(clickEvent);
}, function(current, total) {
unzipProgress.value = current;
unzipProgress.max = total;
li.appendChild(unzipProgress);
});
}
if (typeof requestFileSystem == "undefined")
creationMethodInput.options.length = 1;
fileInput.addEventListener('change', function() {
fileInput.disabled = true;
//Create a list of anchor links to display to download files on the web page
model.getEntries(fileInput.files[0], function(entries) {
fileList.innerHTML = "";
entries.forEach(function(entry) {
var li = document.createElement("li");
var a = document.createElement("a");
a.textContent = entry.filename;
a.href = "#";
//Click event handler
a.addEventListener("click", function(event) {
if (!a.download) {
download(entry, li, a);
event.preventDefault();
return false;
}
}, false);
li.appendChild(a);
fileList.appendChild(li);
});
});
}, false);
})();
})(this);
感謝您的回覆。我以前曾嘗試過此來源。但仍不符合我的要求。請注意,我正在處理移動和功能下載zip文件,然後解壓縮它們在後臺運行。 – thanh0812474
也許你可以看看[鏈接](http://stackoverflow.com/questions/2095697/unzipping-files) –
提取它們到哪裏? JS不能保存任何文件在特定的文件夾... – dandavis
@Sudipta Mondal:我看看你的鏈接,但仍然沒有解決方案 – thanh0812474