โ12-22-2021 04:02 AM
โ12-22-2021 04:20 AM
Hey @peter,
Yes, I think itโs possible. In my case I was extracting only the csv files out of zip files but it should be similar. Check the Python script I was using in a Script snap, and try to adjust it per your needs. Let us know if you have any difficulties.
from com.snaplogic.scripting.language import ScriptHook
import zipfile
import io
class TransformScript(ScriptHook):
def __init__(self, input, output, error, log):
self.input = input
self.output = output
self.error = error
self.log = log
# The "execute()" method is called once when the pipeline is started
# and allowed to process its inputs or just send data to its outputs.
def execute(self):
self.log.info("Executing Transform script")
while self.input.hasNext():
try:
# Read the next input document, store it in a new dictionary, and write this as an output document.
inDoc = self.input.next()
content = inDoc.get('content')
files = zipfile.ZipFile(io.BytesIO(content))
for info in files.infolist():
if files.read(info).find("xml version=") != -1:
filecontent = files.read(info)
outDoc = {
'content' : filecontent,
'original' : inDoc
}
self.output.write(inDoc, outDoc)
except Exception as e:
errDoc = {
'error' : str(e)
}
self.log.error("Error in python script")
self.error.write(errDoc)
self.log.info("Script executed")
# The "cleanup()" method is called after the snap has exited the execute() method
def cleanup(self):
self.log.info("Cleaning up")
# The Script Snap will look for a ScriptHook object in the "hook"
# variable. The snap will then call the hook's "execute" method.
hook = TransformScript(input, output, error, log)
I hope youโll find this helpful,
Bojan
โ12-23-2021 04:31 AM
Thanks,
I am not sure how to add this a snap to read a file or how to convert the script to read a rar file.
โ12-28-2021 04:03 PM
@peter - search the community site here for questions about file access and youโll see several examples of how to access directories and files. If you donโt find what you need, ask here again, but do look there first.
Unfortunately, RAR is a proprietary format. So while your script snap will contain Python or Javascript similar to the above, youโll need a RAR-specific packageโฆ and probably a stand alone executable on your Snaplex node that understands RAR: 7zip
and unrar
are two possibilities.
What OS do you run your Snaplex(es) on?
โforbin
โ12-28-2021 04:05 PM
Here are some python packages that support RAR (if python is your thing; it isnโt mine).
https://rarfile.readthedocs.io/api.html
Also, Stack Overflow is [sometimes] your friend: