2

Is it possible to run a .html or .exe for example, that is inside a zipfile? I'm using the Zipfile module.

Here's my sample code:

import zipfile

z = zipfile.ZipFile("c:\\test\\test.zip", "r")
x = ""
g = ""
for filename in z.namelist():
    #print filename
    y = len(filename)
    x = str(filename)[y - 5:]
    if x == ".html":
        g = filename
f = z.open(g)

After f = z.open(g), I don't know what to do next. I tried using the .read() but it only reads whats inside of the html, what I need is for it to run or execute.

Or is there any othere similar ways to do this?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Katherina
  • 2,153
  • 7
  • 26
  • 34
  • 1
    I'm not sure behaviour you want, so it's hard to help you make it happen. Take the zip file out of the equation. Imagine you have a plain ".html" file, e.g. `g = "testfile.html"`. What behaviour do you want to see from the computer? – Jim DeLaHunt Jan 19 '12 at 03:57
  • what i would want is, for it to launch the testfile.html on a web browser. like after the g = "testfile.html" theres a code that will open it on a web browser. (but testfile.html is inside a zipfile) – Katherina Jan 19 '12 at 04:04
  • I suggest you add to your question the code to open the file in a web browser, run the executable, etc., which you'd use if the file was out of the zip file. Do you already know how to do this part? – Jim DeLaHunt Jan 19 '12 at 04:18
  • i got the code on running it outside of the zipfile, the challenge is how do i run it, if it is inside the zipfile, i wish to re create where if you open a zipfile and double clicks on the html file inside the zipfile. it will automatically opens on a web browser. this is under windows btw – Katherina Jan 19 '12 at 04:33
  • You "got the code on running it outside of the zipfile". Good. What happens when you give the file handle `f` from `z.open(g)` to this code? What doesn't work? What behaves differently? It would help if you show us this code. – Jim DeLaHunt Jan 19 '12 at 04:56

2 Answers2

1

The best approach will be to extract the required file to the Windows temp directory and execute it. I have modified your original code to create a temp file and execute it:

import zipfile
import shutil
import os

z = zipfile.ZipFile("c:\\test\\test.zip", "r")
x = ""
g = ""
basename = ""
for filename in z.namelist():
    print filename
    y = len(filename)
    x = str(filename)[y - 5:]
    if x == ".html":
        basename = os.path.basename(filename) #get the file name and extension from the return path
        g = filename
        print basename
        break #found what was needed, no need to run the loop again
f = z.open(g)

temp = os.path.join(os.environ['temp'], basename) #create temp file name
tempfile = open(temp, "wb")
shutil.copyfileobj(f, tempfile) #copy unzipped file to Windows 'temp' folder
tempfile.close()
f.close()
os.system(temp) #run the file
Abbas
  • 6,720
  • 4
  • 35
  • 49
  • Don't use `os.system()` use `os.startfile()` on Windows; `subprocess`, `webbrowser` modules on other OSes. – jfs Jan 20 '12 at 08:26
  • `tempfile` is more robust than `os.environ['temp']` – jfs Jan 20 '12 at 08:27
  • 1
    you could use `os.path.splitext()` to extract file extension or just `filename.endswith('.html')` to check it e.g., see [my answer](http://stackoverflow.com/a/8938550/4279). – jfs Jan 20 '12 at 08:30
  • hi thanks for the teaching let me study it again for a while. i need time for this to sink in my head =) thanks guys – Katherina Jan 24 '12 at 05:37
1

Run the first .html file in a zip archive specified at the command line:

#!/usr/bin/env python
import os
import shutil
import sys
import tempfile
import webbrowser
import zipfile
from subprocess import check_call
from threading  import Timer

with zipfile.ZipFile(sys.argv[1], 'r') as z:
    # find the first html file in the archive
    member = next(m for m in z.infolist() if m.filename.endswith('.html'))
    # create temporary directory to extract the file to
    tmpdir = tempfile.mkdtemp()
    # remove tmpdir in 5 minutes
    t = Timer(300, shutil.rmtree, args=[tmpdir], kwargs=dict(ignore_errors=True))
    t.start()
    # extract the file
    z.extract(member, path=tmpdir)
    filename = os.path.join(tmpdir, member.filename)

# run the file
if filename.endswith('.exe'):
    check_call([filename]) # run as a program; wait it to complete
else: # open document using default browser
    webbrowser.open_new_tab(filename) #NOTE: returns immediately

Example

T:\> open-from-zip.py file.zip

As an alternative to webbrowser you could use os.startfile(os.path.normpath(filename)) on Windows.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • hi thanks for the teaching let me study it again for a while. i need time for this to sink in my head =) thanks guys – Katherina Jan 24 '12 at 05:38