Working with zip-files | Collab 8.0.2 - python 2.2

Hi, 

We are using Collaboration 8.0.2 (Python 2.2) and I've create a Dropbox-custom object where users upload zip-files which need to be extracted onto Collaboration to a designated location with a designated owner. 

I've been doing it before using a Windows share and csv-file, so I *think* I already have most of the code and will be able to transform it rather quickly, but I'm blocked trying to grab a hold of the zip-objects...I have the KM-objects but would need to cast them as ZipFile or something in order to work with them as such.

The Dropbox is in essence a folder holding the uploaded files, and a first step would be to do a zipfile.is_zipfile(). However, I keep getting a NoneType when I do this... It should return true and, afterwards, I would like to cast it as a ZipFile and do a testzip(), then begin extracting/converting to KM-objects.

dropbox = kmSession.getKMObjectFromLogicalName('nirond_dropbox')
zips=pylist(dropbox.getChildren())
log("Zips found: " + str(len(zips)))
for zip in zips:
    log(zip.getName() + " (" + str(zip.getSize()) +  " bytes) is zipfile? " + str(zipfile.is_zipfile(zip.getContentStream())))
    if(zipfile.is_zipfile(zip.getContentStream()) == True):#check if it's a valid zipfile, should be True (currently returning None)
        log("This is a zipfile")
        #cast as ZipFile and do a decent testzip()
        zf = zipfile.ZipFile(zip,mode='w')#this is not the correct way to do it...
        for name in zf.namelist():
            log(name + " is in the zipfile")
    else:
        log("Not a zipfile")

Does anyone have an idea how to work with zipfiles in Collaboration using Python 2.2?

Thanks in advance.

Comments

  • zipfile.is_zipfile takes a filename, so you’ll need to save the zip file to the filesystem and reference it from there:

     

    myzip = zip.getContentIntoLocalFile(“myzip.zip”)

    log(“Has zip : “+zipfile.is_zipfile(“myzip.zip”))

     

    -Curtis

     

    From: eLink Entry: Vignette Collaboration Developers Forum [mailto:vignettecollaborationdevelopersforum@elinkkc.opentext.com]
    Sent: Friday, October 14, 2016 6:57 AM
    To: eLink Recipient
    Subject: Working with zip-files | Collab 8.0.2 - python 2.2

     

    Working with zip-files | Collab 8.0.2 - python 2.2

     

    Posted by Delodder, Timothy On 10/14/2016 08:53 AM

     

     

    Hi, 

    We are using Collaboration 8.0.2 (Python 2.2) and I've create a Dropbox-custom object where users upload zip-files which need to be extracted onto Collaboration to a designated location with a designated owner. 

    I've been doing it before using a Windows share and csv-file, so I *think* I already have most of the code and will be able to transform it rather quickly, but I'm blocked trying to grab a hold of the zip-objects...I have the KM-objects but would need to cast them as ZipFile or something in order to work with them as such.

    The Dropbox is in essence a folder holding the uploaded files, and a first step would be to do a zipfile.is_zipfile(). However, I keep getting a NoneType when I do this... It should return true and, afterwards, I would like to cast it as a ZipFile and do a testzip(), then begin extracting/converting to KM-objects.

    dropbox = kmSession.getKMObjectFromLogicalName('nirond_dropbox')
    zips=pylist(dropbox.getChildren())
    log("Zips found: " + str(len(zips)))
    for zip in zips:
        log(zip.getName() + " (" + str(zip.getSize()) +  " bytes) is zipfile? " + str(zipfile.is_zipfile(zip.getContentStream())))
        if(zipfile.is_zipfile(zip.getContentStream()) == True):#check if it's a valid zipfile, should be True (currently returning None)
            log("This is a zipfile")
            #cast as ZipFile and do a decent testzip()
            zf = zipfile.ZipFile(zip,mode='w')#this is not the correct way to do it...
            for name in zf.namelist():
                log(name + " is in the zipfile")
        else:
            log("Not a zipfile")

    Does anyone have an idea how to work with zipfiles in Collaboration using Python 2.2?

    Thanks in advance.


    [To post a comment, use the normal reply function]

    Forum:

    Vignette Collaboration Developers Forum

    Content Server:

    Knowledge Center CS16

     

  • Hi Curtis,

    Thanks for your reply, meanwhile I've continued working on the script and thanks to your recommendations, managed to get the zipfile stuff working. I'm now at the point where nearly everything is working, actually, except for one thing...

    So I'm trying to upload files that are contained within the zipfile(s), everything works with plain text files, but when working with other files (like JPEG, PDF's, .doc, ...), the files seem to be corrupt....

    I've pasted below the relevant code snippit. Basically I'm 'reading' the file and then using a setContent(), followed by a commit, but maybe I need to do something more in order for the files to be readable? I also tried using setContentFromLocalFile(), but I'm assuming this won't work since the zipfile is never really unzipped onto the local filesystem... I'm assuming I won't have to manually set the MIME type for the files, right? Since I have no clue what kind of files to expect in the zipfiles.

    Thanks for your help :)

    zf = zipfile.ZipFile('name of the zipfile',mode='r')

    def createfile(owner,p,f):
        file=exists(p,f)#check if file exists, will return found object or false
        if(not(p == parents["top"].getName())):
            log("Attempting to read %s" % (p + '/' + f))
            data = zf.read(p + '/' + f)
        else:
            log("Attempting to read %s" % (f))
            data = zf.read(f)
        if(file and data != ''):#because 'file' may also be 'false' and getContent() would result in an exception. if data is empty, create an empty file
            #file already exists, check if identical and, if not, turn on versioning
            #(irrelevant/duplicate code ommited)
        else:#File does not exist yet, create a new object
            file = kmSession.prepare(kmSession.KM_OBJECT_TYPE_DOCUMENT_CODE)
            file.setName(f)
            file.setTrueParent(parents[p])
            file.setFileName(f)
            file.setContent(data)
            file.commit()
            file.setOwner(owner)#only works AFTER commit
            file.commit()
            log("Created file " + file.getName() + ".")

  • Have you tried “setContentBytes” instead of “setContent”?

     

    -Curtis

     

    From: eLink Entry: Vignette Collaboration Developers Forum [mailto:vignettecollaborationdevelopersforum@elinkkc.opentext.com]
    Sent: Friday, January 20, 2017 8:48 AM
    To: eLink Recipient
    Subject: Working with zip-files | Collab 8.0.2 - python 2.2

     

    Working with zip-files | Collab 8.0.2 - python 2.2

     

    Posted by Delodder, Timothy On 01/20/2017 10:43 AM

     

     

    Hi Curtis,

    Thanks for your reply, meanwhile I've continued working on the script and thanks to your recommendations, managed to get the zipfile stuff working. I'm now at the point where nearly everything is working, actually, except for one thing...

    So I'm trying to upload files that are contained within the zipfile(s), everything works with plain text files, but when working with other files (like JPEG, PDF's, .doc, ...), the files seem to be corrupt....

    I've pasted below the relevant code snippit. Basically I'm 'reading' the file and then using a setContent(), followed by a commit, but maybe I need to do something more in order for the files to be readable? I also tried using setContentFromLocalFile(), but I'm assuming this won't work since the zipfile is neverreally unzipped onto the local filesystem... I'm assuming I won't have to manually set the MIME type for the files, right? Since I have no clue what kind of files to expect in the zipfiles.

    Thanks for your help :)

    zf = zipfile.ZipFile('name of the zipfile',mode='r')

    def createfile(owner,p,f):
        file=exists(p,f)#check if file exists, will return found object or false
        if(not(p == parents["top"].getName())):
            log("Attempting to read %s" % (p + '/' + f))
            data = zf.read(p + '/' + f)
        else:
            log("Attempting to read %s" % (f))
            data = zf.read(f)
        if(file and data != ''):#because 'file' may also be 'false' and getContent() would result in an exception. if data is empty, create an empty file
            #file already exists, check if identical and, if not, turn on versioning
            #(irrelevant/duplicate code ommited)
        else:#File does not exist yet, create a new object
            file = kmSession.prepare(kmSession.KM_OBJECT_TYPE_DOCUMENT_CODE)
            file.setName(f)
            file.setTrueParent(parents[p])
            file.setFileName(f)
            file.setContent(data)
            file.commit()
            file.setOwner(owner)#only works AFTER commit
            file.commit()
            log("Created file " + file.getName() + ".")


    [To post a comment, use the normal reply function]

    Topic:

    Working with zip-files | Collab 8.0.2 - python 2.2

    Forum:

    Vignette Collaboration Developers Forum

    Content Server:

    Knowledge Center CS16

     

  • Hi Curtis,

    Yes, it works perfectly now!

    Thanks so much!