Python: directly decompress the encrypted zip file using the pexpect Library

Source: Internet
Author: User
Tags gz file

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.