×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    153

    zipfile + password coding problems

    Hello fellows,

    import zipfile

    I've a problem to unzip zip-files that are protected by a password.
    This is my code:
    Code:
    def unpack():
        a = update_folder + update_file
        ##Extract every file from it
        for i in a.namelist():
            string = basics()
            b = open(unpack_folder +i, 'r', 'password')
            b.write(a.read(i))
            b.close()
    
    unpack()
    By doing so I always get this error: "TypeError: an integer is required"

    Also the documentation of zipfile was not really helpfull to me.


    Could someone please shed some lite on this?
    What is goinig wrong here?

    Thx
    novis

  2. #2
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: zipfile + password coding problems

    If you don't get any help here you should try asking this on a standard Python forum, since it's not really a question specific to PyS60. Like this one. And when you do, make sure you specify which version of Python you're using and give code that doesn't rely on unkown things, like update_folder or basics. It would be a lot easier to help you that way.

  3. #3
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    153

    Re: zipfile + password coding problems

    Sorry, my fault,
    Code looks like this (where "test.zip" is protected by a password):
    Code:
    import zipfile
    
    def unpack():
        a = "C:\\test\\test.zip"
        ##Extract every file from it
        for i in a.namelist():
            b = open(unpack_folder +i, 'r', 'password')
            b.write(a.read(i))
            b.close()
    
    unpack()
    By doing so I always get this error: "TypeError: an integer is required"

    I still hope that someone was using password protected zip-folders in Python for S60 already and is able to help.
    But anyway, thx bogdan.galiceanu for the link, I'll give it a go.
    thx novis
    Last edited by novis; 2009-07-15 at 18:46.

  4. #4
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: zipfile + password coding problems

    Quote Originally Posted by novis View Post
    Code looks like this (where "test.zip" is protected by a password):
    Code:
    import zipfile
    
    def unpack():
        a = "C:\\test\\test.zip"
        ##Extract every file from it
        for i in a.namelist():
            b = open(unpack_folder +i, 'r', 'password')
            b.write(a.read(i))
            b.close()
    
    unpack()
    That code still uses unpack_folder which isn't defined (but that's not such a big problem). The problem is that a is a string and doesn't have the namelist method. Please fix the code

  5. #5
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    153

    Re: zipfile + password coding problems

    Sorry again and thx for your patience
    Here is all the code (for sure this time, I promise )
    Code:
    import zipfile
    
    folder = "C:\\Python\\lib\\"           ## place of the zip-file?
    unpack_folder = "C:\\Python\\lib\\"    ## where to put the unpacked files?
    
    #file = "free.zip"              ## name of an unprotected zip-file?
    file = "protected.zip"          ## name of a password protected zip-file?
    
    
    def getzip(filename, ignoreable=100): 
        try: 
            return zipfile.ZipFile(filename) 
        except zipfile.BadZipfile: 
            original = open(filename, 'rb') 
            try: 
             data = original.read() 
            finally: 
             original.close() 
            position = data.rindex(zipfile.stringEndArchive, 
                                -(22 + ignoreable), -20) 
            coredata = cStringIO.StringIO(data[: 22 + position]) 
            return zipfile.ZipFile(coredata) 
    
    def unpack():
        a = getzip(folder + file)
        ##Extract every file from it
        for i in a.namelist():
            #b = open(unpack_folder +i, 'wb') # if there is no password
            b = open(unpack_folder +i, 'wb', 'abc') # if there is a password (password is 'abc')
            b.write(a.read(i))
            b.close()
    
        ##Close the archive
        a.close()
    
    unpack()
    Non password protected zip-files are working fine.
    The error message for all password protected zip-files is "TypeError: an integer is required"

  6. #6
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: zipfile + password coding problems

    Well I guess there's no easy way to do this before Python 2.6, which has all those useful methods described in the documentation. So here's what I did: downloaded the source for Python 2.6.2 from here, took zipfile.py from Python-2.6.2\Lib, put it on the phone's memory card and named it zipfile2.py. Then I took a simpler example and it worked. Here it is, I'm sure you can adapt it to your needs:
    Code:
    #Let's assume we have test.zip with the password "abc"
    
    #Tell Python where to look for zipfile2.py
    import sys
    sys.path.append("E:\\")
    
    import zipfile2 as zipfile
    
    
    #Open the archive
    a = zipfile.ZipFile("C:\\test.zip", "r")
    
    #Extract all the files
    for i in a.namelist():
    	b = open("C:\\" + i , "wb")
    	b.write(a.read(i, "abc"))
    	b.close()
    a.close()
    That should work. If it doesn't, we'll have to wait for someone else to clear this up or you can try posting on that other forum.
    Last edited by bogdan.galiceanu; 2009-07-15 at 20:45.

  7. #7
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    153

    Re: zipfile + password coding problems

    Think I could live with an additional zipfile2 library.
    I'll try your sollution tomorrow.

    Thx for the great support bogdan.galiceanu

  8. #8
    Regular Contributor
    Join Date
    Mar 2007
    Posts
    153

    Re: zipfile + password coding problems

    It's working,
    I just tried it on my PC

    Just to conclude it:
    Using Python 2.5 it is not possible to open password secured zip-files.
    But it is working from Python 2.6.2 onwards.
    So the way to go is to use a newer version of the zipfile library (at least from 2.6.2).

    cu
    novis

Similar Threads

  1. User Password Settings - Need help ...
    By cbrueckner in forum Symbian User Interface
    Replies: 2
    Last Post: 2007-01-03, 04:22
  2. Password dialog problem
    By bernie31 in forum Symbian
    Replies: 1
    Last Post: 2006-01-26, 20:23
  3. Problem in setting Password
    By SachinKumar in forum Symbian
    Replies: 0
    Last Post: 2005-06-21, 14:38
  4. Problems with Password delivery
    By Alex Roca Sánchez in forum Mobile Java Tools & SDKs
    Replies: 8
    Last Post: 2005-02-06, 08:06
  5. Memory Card Password Locked from 3650 to 6600
    By rpl in forum General Development Questions
    Replies: 0
    Last Post: 2003-12-02, 12:21

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×