In fact, 'unzip-P password zipfile' can automatically decompress the encrypted file. But to get familiar with python library pexpect. Use python to write a code to automatically decompress the zip file. Such code can be called by the server page script to decompress the uploaded encrypted file.
1. Because the pexpect library is not a standard library that comes with python, you must first download it when using it. The fedora user can directly download yum.
Su-c 'yum install pexpect'
Or download the gz file and install it on your own:
Http://pypi.python.org/pypi/pexpect/#downloads
Here is an official website for introduction to the pexpect library, which provides a detailed description of the structure and functions of the Library:
Http://pexpect.sourceforge.net/pexpect.html
2. Go to the topic and run the following code:
1 #! /Usr/bin/env python
2 '''
3 @ version: 1.0
4 @ author: Nathan Huang
5 '''
6 import pexpect
7 import OS
8 import sys
9 import getopt
10
11 def unzip_encrypted_file (abspath = '', filename ='', mypassword = ''):
12 # check the folder and file
13 if OS. path. isdir (abspath) and OS. path. isfile (OS. path. join (abspath, filename) and mypassword:
14 # go into the folder
15 OS. chdir (abspath)
16 # unzip encrypted file
17 child = pexpect. spawn ("unzip % s" % OS. path. join (abspath, filename ))
18 try:
19 child. Verify CT ("password :")
20 child. sendline (mypassword)
21 child. wait ()
22 TB predictionp0000ct:
23 print "Error"
24 else:
25 print "path or file does not exists! "
26 sys. exit (2)
27
28 def usage ():
29 print "unzipencryptedfile-h-d-n-p"
30 print "-h help"
31 print "-d path"
32 print "-n filename"
33 print "-p password"
34
35 def main ():
36 abspath =''
37 filename =''
38 mypassword =''
39 # get args from linear line
40 try:
41 opts, args = getopt. getopt (sys. argv [1:], "hd: n: p :")
42 response t getopt. GetoptError, err:
43 print str (err)
44 usage ()
45 sys. exit (2)
46
47 for o, a in opts:
48 if o = '-H ':
49 usage ()
50 sys. exit (0)
51 elif o = '-d ':
52 abspath =
53 elif o = '-N ':
54 filename =
55 elif o = '-P ':
56 mypassword =
57 # execute unzip_encrypted_file ()
58 unzip_encrypted_file (abspath, filename, mypassword)
59
60 if _ name __= = '_ main __':
61 main ()
From CCJPP