4

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.

Sreedhar
  • 41
  • 1
  • 1
  • 2

4 Answers4

10

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.

Community
  • 1
  • 1
Cheeso
  • 180,104
  • 92
  • 446
  • 681
2

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

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.

Jamiec
  • 118,012
  • 12
  • 125
  • 175
1

By design javascript can't access the filesystem.

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

Shurdoof
  • 1,659
  • 14
  • 16
-2

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.

Gustav Barkefors
  • 4,626
  • 24
  • 30
  • This is pointless, you still cant guarantee (or determine) where the user will save the zip, so you cant provide the correct path to unzip.exe – Jamiec Dec 22 '10 at 12:12
  • In theory, you could do stuff like that with the COM object Scripting.FileSystemObject. Anyway, the bottom line of my answer was that unless this is strictly for his own use at home, I'm having a hard time imagining a situation where it would be appropriate/desirable. – Gustav Barkefors Dec 22 '10 at 12:36
  • thanks for replaying ..Actually i have some list of files in server.i just want to print that files in client machine with out opening the file.i give the option for user like printall.if user click printall prints need to come from client system. but i done this functionality in java ,it is giving prints to server printer not to client printer. – Sreedhar Dec 22 '10 at 13:11