0
votes

My problem is that I have to generate a zip file using the linux zip console command. My command is as follows:

zip -r /folder1/folder2/EXP_45.zip /folder1/folder2/EXP_45/

That returns a correct zip only that includes the root folders I want:

Returns

EXP_45.zip

-folder1

--folder2

---EXP_45

...

I want

EXP_45.zip

-EXP_45

...

EXP_45 is a folder that can contain files and folders and they must be present in the zip. I just want the tree structure to start with the EXP_45 folder.

Is there any solution?

The reason why I need it to be a single command is that it is an action of a job in a PL SQL function like:

BEGIN
DBMS_SCHEDULER.CREATE_JOB ( 
    JOB_NAME=>'compress_files',       --- job name 
    JOB_ACTION=>'/usr/bin/zip',    --- executable file with path 
    JOB_TYPE=>'executable',        -----   job type
    NUMBER_OF_ARGUMENTS=>4,  --  parameters in numbers                   
    AUTO_DROP =>false,
    CREDENTIAL_NAME=>'credentials'   -- give credentials name which you have created before "credintial"
    );

dbms_scheduler.set_job_argument_value('compress_files',1,'-r');
dbms_scheduler.set_job_argument_value('compress_files',2,'-m');
dbms_scheduler.set_job_argument_value('compress_files',3,'/folder1/folder2/EXP_45.zip');
dbms_scheduler.set_job_argument_value('compress_files',4,'/folder1/folder2/EXP_45/');
DBMS_SCHEDULER.RUN_JOB('compress_files');
END;
2

2 Answers

0
votes

I haven't been able to find a solution to this problem using zip but I have found it using jar. The command would be:

jar cMf /folder1/folder2/EXP_45.zip -C /folder1/folder2/EXP_45 .

Also, the solution using a job in pl sql in case it works for someone would be:

BEGIN
DBMS_SCHEDULER.CREATE_JOB ( 
    JOB_NAME=>'compress_files',       --- job name 
    JOB_ACTION=>'/usr/bin/jar',    --- executable file with path 
    JOB_TYPE=>'executable',        -----   job type
    NUMBER_OF_ARGUMENTS=>5,  --  parameters in numbers                   
    AUTO_DROP =>false,
    CREDENTIAL_NAME=>'credentials'   -- give credentials name which you have created before "credintial"
    );

dbms_scheduler.set_job_argument_value('compress_files',1,'cMf');
dbms_scheduler.set_job_argument_value('compress_files',2,'/folder1/folder2/EXP_45.zip');
dbms_scheduler.set_job_argument_value('compress_files',3,'-C');
dbms_scheduler.set_job_argument_value('compress_files',4,'/folder1/folder2/EXP_45');
dbms_scheduler.set_job_argument_value('compress_files',5,'.');
DBMS_SCHEDULER.RUN_JOB('compress_files');
END;
0
votes

You want to use the -j (or --junk-paths) option when you are creating the zip file. Below is from the zip man page.

   -j
   --junk-paths
          Store just the name of a saved file (junk the path), and do not store directory names. 
          By default, zip will store the full path (relative to the current directory).

Update following Question Clarification

Why not put the equivalent to the code below in a shell script & get the SQL function to invoke that? You just need to pass the directory name to cd into and the name of the output zip.

cd folder1/folder2
zip -r /tmp/EXP_45.zip EXP_45