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

发布于 2024-10-08 21:54:46 字数 115 浏览 3 评论 0原文

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

锦欢 2024-10-15 21:54:46

您可以使用 Javascript 在浏览器中将 zip 文件解压缩到内存中。

此答案展示了如何操作。

浏览器中的 js 代码如下所示:

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

var zipFile = new ZipFile(url, doneReading); 

提供的 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...
  }
}

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

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

我不相信你可以与文件系统交互,无论是读还是写,除非你求助于普通 javascript 之外的东西——比如 Google Gears、Silverlight 和 Flash。

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

This answer shows how.

The js code in the browser looks like this:

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

var zipFile = new ZipFile(url, doneReading); 

Inside the DoSomethingWithEntries method, which you provide, you can fiddle with an object that represents the extracted zip file.

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.

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.

蝶…霜飞 2024-10-15 21:54:46

根据设计,JavaScript 无法访问文件系统。

可能可以通过ActiveX、java applet 等实现...

By design javascript can't access the filesystem.

It may be possible with ActiveX, java applets etc...

静赏你的温柔 2024-10-15 21:54:46

如果您在 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..."' ); 

显然,这需要相当荒谬的用户端的安全设置,除非这是您自己使用的东西,否则您应该让用户决定是否要下载和提取您的文件。

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.Shell to perform shell executes:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文