Skip to main content

Hello all,

I am trying to build a PythonCaller that replicates the Windows command of “Send to Compressed (zipped) folder”

The goal is to prepare a fileGDB dataset for transfer by ftp by zipping it

I want to give the PythonCaller a filePath such as D:\2024\03\project\data.gdb

Which then creates a zip file D:\2024\03\project\data.gdb.zip (that is, the zip file is sitting in the same folder as the data.gdb)

And, when the end user opens the zip file they see the folder data.gdb which they can then extract

I am trying using this code

    def input(self, feature):
        import os, shutil
        folder = feature.getAttribute('folder')
        fileGDB = feature.getAttribute('fileGDB')
        zip_file = feature.getAttribute('zip_file')
        if os.path.exists(fileGDB):
            print('fileGDB exists')
            if os.path.exists(zip_file):
                os.remove(zip_file)
            else:
                print('zip file DOES NOT exist')
            shutil.make_archive(fileGDB, 'zip', folder)
        else:
            print('fileGDB DOES NOT exist')
        self.pyoutput(feature)

But! the zip file contains itself! Which is wrong

 

 

I think I have worked it out

First, setup some User Parameters, both text

workingFolder is the folder that contains the fileGDB

fileGDB is the name of the fileGDB (without the folder)

Then, use Attribute Creator to create attributes from the user parameters

working_folder is the workingFolder parameter

fileGDB_name is the fileGDB parameter

fileGDB_contents is $(fileGDB)\*

zipFile_name is $(fileGDB).zip

 

Finally the PythonCaller

    def input(self, feature):
        print('+++++++++++++++++')
        print('start zip fileGDB')
        print('+++++++++++++++++')
        import zipfile
        import glob
        import os
        my_working_folder = feature.getAttribute('working_folder')
        my_fileGDB_name = feature.getAttribute('fileGDB_name')
        my_fileGDB_contents = feature.getAttribute('fileGDB_contents')
        my_zipFile_name = feature.getAttribute('zipFile_name')
        os.chdir(my_working_folder)
        if os.path.exists(my_fileGDB_name):
            print('fileGDB exists')
            if os.path.exists(my_zipFile_name):
                print('deleting zip file')
                os.remove(my_zipFile_name)
            else:
                print('zip file DOES NOT exist')
            print('writing zip file')
            with zipfile.ZipFile(os.path.normpath(my_zipFile_name), 'w') as f:
                for file in glob.glob(my_fileGDB_contents):
                    f.write(file)
        else:
            print('fileGDB DOES NOT exist')
        print('+++++++++++++++++')
        print('end zip fileGDB')
        print('+++++++++++++++++')
        self.pyoutput(feature)


I believe you can tell FME to write the FGDB directly to a zip file by appending “.zip” on the writer.

https://docs.safe.com/fme/html/FME-Form-Documentation/FME-Form/Workbench/zip_files_writing.htm


Hi @david_r yes that is true, if you are creating the fileGDB from scratch.

In my case, I am modifying the records in an existing fileGDB. Then I want to prepare the fileGDB for transport.


I have attempted to modify the records in an existing fileGDB that is zipped, and I cannot get that to work

 


Reply