compress a file with folder structure to tgz

Post Reply
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

compress a file with folder structure to tgz

Post by bkromer »

I need to compress the incoming file (.png) to .tgz and it needs to be in a specific directory like "/folder1/folder2/myfile.png".
I found this npm module https://www.npmjs.com/package/compressing

So the incoming file will be a single file in the format of .png.
Maybe I can create a tmp folder with this file in it then compress it and send that tmp folder as new job?
What would be the fastest most reliable way?

I tried some with pump https://www.npmjs.com/package/pump. Pump the reading stream into the compress filestream and pass that to the write stream but could'nt get it to work yet.

Any suggestions?
Benjamin
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: compress a file with folder structure to tgz

Post by freddyp »

The easiest way is indeed to create a temporary folder with a certain structure and the file inside and to send that temporary folder to the output as a job and then let the "Archive" element take care of the zipping.

Do not forget to send the incoming job to null and to clean up the temporary folder after having sent it as a new child job.
bkromer
Member
Posts: 99
Joined: Thu Jul 11, 2019 10:41 am

Re: compress a file with folder structure to tgz

Post by bkromer »

Can archive create tgz? I thought about compressing it within the script.

Something like:

Code: Select all

const fs = require("fs");
const tmp = require('tmp');
const compressing = require("compressing");

async function jobArrived(s, flowElement, job) {
    const entranceJob = await job.get( AccessLevel.ReadWrite );
    const jobname = await job.getName(false);
    const jobnameWithExtension = await job.getName(true);
    await job.log(LogLevel.Info,`entranceJob: ${entranceJob} jobname: ${jobname} jobnameWithExtension: ${jobnameWithExtension}`);
    //CREATE A TMP FOLDER
    const tmpFolder = tmp.dir("/0100/02");
    //READ AND COMPRESS TO TMP FOLDER
    new compressing.tgz.compressStream({ source:`${entranceJob}` })
    .on('error', (err) => { job.log(LogLevel.Error, err) })
    .pipe(fs.createWriteStream(`${tmpFolder.name}/${jobname}.tgz`))
    .on('error', (err) => { job.log(LogLevel.Error, err) })
    const newJob = await flowElement.createJob(`${tempFolder.name}/${jobname}.tgz`);
    await newJob.sendToSingle(); 
}
Getting errors I dont understand: "undefined is not a constructor TypeError: undefined is not a constructor at jobArrived"
Benjamin
Post Reply