javascript 如何使用javascript在客户端系统中提取Zip文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4508595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 13:56:24  来源:igfitidea点击:

how to extract the Zip file in client system using javascript

javascriptunzip

提问by Sreedhar

I have a Zip file in server.i am downloading the zip file save into client system.now i want to extract file using javascript. anybody please help me. thanks in advance.

我在服务器中有一个 Zip 文件。我正在下载保存到客户端系统中的 zip 文件。现在我想使用 javascript 提取文件。任何人请帮助我。提前致谢。

回答by Cheeso

You can unzip zipfiles in memory, within a browser, using Javascript.

您可以在浏览器中使用 Javascript 解压缩内存中的 zipfile。

This answershows how.

这个答案显示了如何。

The js code in the browser looks like this:

浏览器中的js代码是这样的:

var doneReading = function(zip){
    DoSomethingWithEntries(zip);
};

var zipFile = new ZipFile(url, doneReading); 

Inside the DoSomethingWithEntriesmethod, which youprovide, you can fiddle with an object that represents the extracted zip file.

提供的DoSomethingWithEntries方法中,您可以摆弄一个代表提取的 zip 文件的对象。

function DoSomethingWithEntries(zip){ 
  // for each entry in the zip...
  for (var i=0; i<zip.entries.length; i++) {
    var entry = zip.entries[i];
    var entryInfo = "<h4><a>" + entry.name + "</a></h4>\n<div>";  
    // put that into a div, if you like.
    // etc...
  }
}

As shown above, you can emit lists of the entries with their name, size, date, and so on.

如上所示,您可以发出条目的名称、大小、日期等列表。

You can also call an extract()method on each zip entry. (not shown here) If you extract, the extraction happens asynchronously. The content gets expanded into byte arrays or strings (depending on whether the entries are binary or text) that are maintained in the memory of the browser javascript environment. You could then display the extracted content from the zipped entries, or whatever you like.

您还可以extract()在每个 zip 条目上调用一个方法。(此处未显示)如果您提取,提取将异步进行。内容被扩展为字节数组或字符串(取决于条目是二进制还是文本),它们保存在浏览器 javascript 环境的内存中。然后,您可以显示从压缩条目中提取的内容,或者您​​喜欢的任何内容。

I don't believe you can interact with the filesystem, either reading or writing, unless you resort to something outside of vanilla javascript - like Google Gears, Silverlight, and Flash.

我不相信您可以与文件系统进行交互,无论是读取还是写入,除非您求助于 vanilla javascript 之外的东西 - 如 Google Gears、Silverlight 和 Flash。

回答by Jamiec

No way. not possible (with straight javascript). ever.

没门。不可能(使用直接的 javascript)。曾经。

Think of the implication - I could visit a website which would be able to downloadd a bajillion Mb zip file to a location of their choice on my system AND then extract it.

想想这意味着 - 我可以访问一个网站,该网站能够将一个 bajillion Mb zip 文件下载到他们在我的系统上选择的位置,然后将其解压缩。

回答by Shurdoof

By design javascript can't access the filesystem.

按照设计,javascript 无法访问文件系统。

It maybe possible with ActiveX, java applets etc...

可能是可能使用ActiveX,Java小程序等..

回答by Gustav Barkefors

If you are using Internet Explorer on Windows you can use ActiveX to exploit a bunch of COM objects that are available by default, such as using WScript.Shellto perform shell executes:

如果您在 Windows 上使用 Internet Explorer,您可以使用 ActiveX 来利用一堆默认可用的 COM 对象,例如WScript.Shell用于执行 shell 执行:

var shell = new ActiveXObject('WScript.Shell');
shell.run( '"unzip.exe command line stuff or whatever you want to do here..."' ); 

Obviously, this would require quite ridiculous security settings on the user's side and unless this is something you're putting together for your own use you should leave it up to the users to decide whether they want to download and extract your files or not.

显然,这将需要用户方面非常荒谬的安全设置,除非这是您将其放在一起供自己使用的东西,否则您应该让用户决定是否要下载和提取您的文件。