顯示OpenOffice 檔案(.odt和.odp) )在網頁瀏覽器中,需要先解壓縮它們。雖然 inflate.js 可能不足以完成此任務,但有一個強大的 JavaScript 解決方案可以非同步解壓縮 ZIP 檔案。
已經開發了 JavaScript 解壓縮器,其中包含 Andy G.P. 的元素。 Na 的二進位檔案閱讀器和 Notmasteryet 的 RFC1951 膨脹邏輯。 ZipFile 類別處理核心功能。
以下JavaScript 程式碼示範了解壓縮器的用法:
// Instantiate a ZipFile and provide a callback for when the ZIP is read. zipFile = new ZipFile(url, doneReading); // The doneReading callback is triggered when the ZIP is read and handles the extraction of entries. function doneReading(zip) { extractEntries(zip); } // Extract entries from the ZIP file. function extractEntries(zip) { for (var i = 0; i < zip.entries.length; i++) { var entry = zip.entries[i]; // Create an entry info div. var entryInfo = "<h4><a>" + entry.name + "</a></h4>\n<div>"; // Create a unique ID for the accordion panel and inject the extracted content into it. var randomId = "id-" + Math.floor((Math.random() * 1000000000)); entryInfo += "<span class='inputDiv'><h4>Content:</h4><span id='" + randomId + "'></span></span></div>\n"; $("#report").append(entryInfo); // Asynchronously extract the entry and pass a callback. entry.extract(extractCb(randomId)); } } // Extract callback function to add the extracted content to an accordion on the page. function extractCb(id) { return (function(entryName, entryText){ var content = entryText.replace(new RegExp( "\n", "g" ), "<br/>"); $("#"+id).html(content); $('#report').accordion('destroy'); $('#report').accordion({collapsible:true, active:false}); }); }
以上是如何使用 JavaScript 在 Web 瀏覽器中非同步解壓縮檔案?的詳細內容。更多資訊請關注PHP中文網其他相關文章!