解压缩文件 [英] Unzipping files

查看:160
本文介绍了解压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在客户端使用 OpenOffice 文件,.odt和.odp进行显示一个网络浏览器。

I want to display OpenOffice files, .odt and .odp at client side using a web browser.

这些文件是压缩文件。使用Ajax,我可以从服务器获取这些文件,但这些是压缩文件。我必须使用 JavaScript 解压缩它们,我尝试过使用inflate.js, http://www.onicos.com/staff/iz/amuse/javascript/expert/ inflate.txt ,但没有成功。

These files are zipped files. Using Ajax, I can get these files from server but these are zipped files. I have to unzip them using JavaScript, I have tried using inflate.js, http://www.onicos.com/staff/iz/amuse/javascript/expert/inflate.txt, but without success.

我该怎么做?

推荐答案

我在Javascript中写了一个unzipper。有用。

I wrote an unzipper in Javascript. It works.

它依赖于 Andy G.P. Na的二进制文件阅读器一些RFC1951从notmasteryet膨胀逻辑。我添加了ZipFile类。

It relies on Andy G.P. Na's binary file reader and some RFC1951 inflate logic from notmasteryet. I added the ZipFile class.

工作示例:

http://cheeso.members.winisp.net/Unzip-Example.htm (死链接)

来源:

http://cheeso.members.winisp.net/srcview .aspx?dir = js-unzip (死链接)

NB :链接已经死亡;我很快就会找到一个新的主人。

NB: the links are dead; I'll find a new host soon.

源包含一个ZipFile.htm演示页面和3个不同的脚本,一个用于zipfile类,一个用于inflate类,一个用于二进制文件读者课。该演示还依赖于jQuery和jQuery UI。如果您只是下载js-zip.zip文件,那么所有必需的源代码都在那里。

Included in the source is a ZipFile.htm demonstration page, and 3 distinct scripts, one for the zipfile class, one for the inflate class, and one for a binary file reader class. The demo also depends on jQuery and jQuery UI. If you just download the js-zip.zip file, all of the necessary source is there.

以下是Javascript中的应用程序代码:

Here's what the application code looks like in Javascript:

// In my demo, this gets attached to a click event.
// it instantiates a ZipFile, and provides a callback that is
// invoked when the zip is read.  This can take a few seconds on a
// large zip file, so it's asynchronous. 
var readFile = function(){
    $("#status").html("<br/>");
    var url= $("#urlToLoad").val();
    var doneReading = function(zip){
        extractEntries(zip);
    };

    var zipFile = new ZipFile(url, doneReading);
};


// this function extracts the entries from an instantiated zip
function extractEntries(zip){
    $('#report').accordion('destroy');

    // clear
    $("#report").html('');

    var extractCb = function(id) {
        // this callback is invoked with the entry name, and entry text
        // in my demo, the text is just injected into an accordion panel.
        return (function(entryName, entryText){
            var content = entryText.replace(new RegExp( "\\n", "g" ), "<br/>");
            $("#"+id).html(content);
            $("#status").append("extract cb, entry(" + entryName + ")  id(" + id + ")<br/>");
            $('#report').accordion('destroy');
            $('#report').accordion({collapsible:true, active:false});
        });
    }

    // for each entry in the zip, extract it. 
    for (var i=0; i<zip.entries.length;  i++) {
        var entry = zip.entries[i];

        var entryInfo = "<h4><a>" + entry.name + "</a></h4>\n<div>";

        // contrive an id for the entry, make it unique
        var randomId = "id-"+ Math.floor((Math.random() * 1000000000));

        entryInfo += "<span class='inputDiv'><h4>Content:</h4><span id='" + randomId +
            "'></span></span></div>\n";

        // insert the info for one entry as the last child within the report div
        $("#report").append(entryInfo);

        // extract asynchronously
        entry.extract(extractCb(randomId));
    }
}






该演示分为几个步骤:点击触发 readFile fn,并实例化ZipFile对象,该对象读取zip文件。读取完成时有一个异步回调(对于合理大小的拉链,通常在不到一秒的时间内发生) - 在此演示中,回调保存在doneReading局部变量中,该变量只调用 extractEntries ,其中
只是盲目解压缩所提供的zip文件的所有内容。在真实的应用程序中,您可能会选择一些要提取的条目(允许用户选择,或以编程方式选择一个或多个条目等)。


The demo works in a couple of steps: The readFile fn is triggered by a click, and instantiates a ZipFile object, which reads the zip file. There's an asynchronous callback for when the read completes (usually happens in less than a second for reasonably sized zips) - in this demo the callback is held in the doneReading local variable, which simply calls extractEntries, which just blindly unzips all the content of the provided zip file. In a real app you would probably choose some of the entries to extract (allow the user to select, or choose one or more entries programmatically, etc).

extractEntries fn迭代所有条目,并调用 extract(),通过回调。对于zipfile中的每个条目,条目的解压缩需要时间,可能是1或更多,这意味着异步是合适的。提取回调只是将提取的内容添加到页面上的jQuery手风琴中。如果内容是二进制的,则它被格式化(未示出)。

The extractEntries fn iterates over all entries, and calls extract() on each one, passing a callback. Decompression of an entry takes time, maybe 1s or more for each entry in the zipfile, which means asynchrony is appropriate. The extract callback simply adds the extracted content to an jQuery accordion on the page. If the content is binary, then it gets formatted as such (not shown).

它有效,但我认为效用有限。

It works, but I think that the utility is somewhat limited.

一件事:它很慢。从PKWare解压缩140k AppNote.txt文件大约需要4秒钟。在.NET程序中,可以在不到0.5秒的时间内完成相同的解压缩。 编辑:在IE9和Chrome中,Javascript ZipFile的解包速度比现在快得多。它仍然比编译的程序慢,但它对于正常的浏览器使用速度非常快。

For one thing: It's very slow. Takes ~4 seconds to unzip the 140k AppNote.txt file from PKWare. The same uncompress can be done in less than .5s in a .NET program. EDIT: The Javascript ZipFile unpacks considerably faster than this now, in IE9 and in Chrome. It is still slower than a compiled program, but it is plenty fast for normal browser usage.

对于另一个:它不进行流式传输。它基本上将zipfile的全部内容融入到内存中。在真实编程环境中,您只能读取zip文件的元数据(例如,每个条目64个字节),然后根据需要读取和解压缩其他数据。据我所知,在javascript中没有办法像这样做IO,因此唯一的选择是将整个zip读入内存并在其中进行随机访问。这意味着它会对大型zip文件的系统内存提出不合理的要求。对于较小的zip文件而言,这不是一个问题。

For another: it does not do streaming. It basically slurps in the entire contents of the zipfile into memory. In a "real" programming environment you could read in only the metadata of a zip file (say, 64 bytes per entry) and then read and decompress the other data as desired. There's no way to do IO like that in javascript, as far as I know, therefore the only option is to read the entire zip into memory and do random access in it. This means it will place unreasonable demands on system memory for large zip files. Not so much a problem for a smaller zip file.

另外:它不处理一般情况zip文件 - 我有很多拉链选项在unzipper中没有实现 - 比如ZIP加密,WinZip加密,zip64, UTF-8编码文件名,等。 (编辑 - 它现在处理UTF-8编码的文件名)。但是,ZipFile类处理基础知识。其中一些事情并不难实现。我在Javascript中 AES加密类;可以集成以支持加密。支持Zip64对于大多数Javascript用户来说可能没用,因为它旨在支持> 4gb压缩文件 - 不需要在浏览器中提取它们。

Also: It doesn't handle the "general case" zip file - there are lots of zip options that I didn't bother to implement in the unzipper - like ZIP encryption, WinZip encryption, zip64, UTF-8 encoded filenames, and so on. (EDIT - it handles UTF-8 encoded filenames now). The ZipFile class handles the basics, though. Some of these things would not be hard to implement. I have an AES encryption class in Javascript; that could be integrated to support encryption. Supporting Zip64 would probably useless for most users of Javascript, as it is intended to support >4gb zipfiles - don't need to extract those in a browser.

我也没有测试解压缩二进制内容的情况。现在它解压缩文本。如果你有一个压缩的二进制文件,你需要编辑ZipFile类来正确处理它。我没弄清楚如何干净利落地做到这一点。 它现在也做二进制文件。

I also did not test the case for unzipping binary content. Right now it unzips text. If you have a zipped binary file, you'd need to edit the ZipFile class to handle it properly. I didn't figure out how to do that cleanly. It does binary files now, too.

编辑 - 我更新了JS解压缩库和演示。除了文本之外,它现在还会执行二进制文件。我已经使它更具弹性和更通用 - 您现在可以指定在读取文本文件时使用的编码。此外,该演示还展开了 - 它显示了在浏览器中解压缩XLSX文件等等。

EDIT - I updated the JS unzip library and demo. It now does binary files, in addition to text. I've made it more resilient and more general - you can now specify the encoding to use when reading text files. Also the demo is expanded - it shows unzipping an XLSX file in the browser, among other things.

所以,虽然我认为它的效用和兴趣有限,但它确实有效。我猜它可以在Node.js中使用。

So, while I think it is of limited utility and interest, it works. I guess it would work in Node.js.

这篇关于解压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆