I have a folder structure like this:
- project:
-- folder 01:
--- file1.cpp
--- file2.cpp
-- folder 02:
--- file1.cpp
--- file2.cpp
I want to zip the content of the project folder in a way I get(when i unzip) this structure:
- folder 01:
-- file1.cpp
-- file2.cpp
- folder 02:
-- file1.cpp
-- file2.cpp
My Problem is now that I always get a parent folder with the same name as my zip file which contains folder 01 and 02. Is there a way I can zip without getting this parent folder ?
zip -r foo ./
assuming **
./
** i.e., present working directory is project in your case.
-r for recursively zipping
foo is the name of your zip file, i.e., foo.zip is the final zipped product you want.
Related
I'm using the 7zip command line interface to extract archives, like so:
7za.exe x -y {path_to_zipfile} -o{path_to_target_folder}
If my zipfile is named my_archive.7z, then I get the following filestructure in the target folder:
🗁 target_folder
└─ 🗁 my_archive
├─ 🗋 foo.png
├─ 🗁 bar
│ ├─ 🗋 baz.txt
│ └─ 🗋 qux.txt
...
However, I don't want the subfolder 🗁 my_archive. I'm looking for flags to apply on the 7zip command such that everything extracts directly in the target folder, without creating the 🗁 my_archive subfolder.
NOTES
I can't replace x with e because the filestructure shouldn't be lost (the e flag pushes all files to the toplevel).
I'm working on a Windows 10 computer, but the solution must also work on Linux.
I'm using the following version: 7-Zip (a) 19.00 (x64)
Some background info: I'm calling 7zip from a Python program, like so:
# Variables:
# 'sevenzip_abspath': absolute path to 7za executable
# 'zipfile_abspath': absolute path to zipped file (`.7z` format)
# 'targetdir_abspath': absolute path to target directory
commandlist = [
sevenzip_abspath,
'x',
'-y',
zipfile_abspath,
f'-o{targetdir_abspath}',
]
output = subprocess.Popen(
commandlist,
stdout=subprocess.PIPE,
shell=False,
).communicate()[0]
if output is not None:
print(output.decode('utf-8'))
I know I could do all kinds of things in Python after the unzipping has finished (move/rename directories, etc etc), but that's for plan B. First I want to check if there is an elegant solution.
I'd like to stick to 7zip for reasons that would lead us too far here.
You can rename the top level folder to match the target folder before extracting the archive.
7za rn {path_to_zipfile} my_archive target_folder
This will permanently change the archive. If you don't want that, take a copy first.
I want to zip just a file in Python which present in a folder. I am finding hard to create zip file with the below code snippet. It does create zip file, but it has complete folder structure inside.
import zipfile as zip
root=r"C:\XXXX\YYYYYY\ZZZZ\"
file="abc.txt"
zipper=zip.ZipFile(file=os.path.join(root,file.replace("txt","zip")),mode="w",compression=zip.ZIP_DEFLATED)
zipper.write(os.path.join(root,file))
zipper.close()
Actual output:
#################
abc.zip
|
XXXX - Folder
|
YYYYYY - Folder
|
ZZZZ - Folder
|
abc.txt
Expected output
###############
abc.zip
|
abc.txt
One way I learnt and working is :
os.chdir(root)
To set the working directory to the folder where the files are present. Then, pass just the filename instead of complete path to create zip.
Not sure, if it is the correct and best way.
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;
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;
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
How to zip files and folders which are in a particular directory using Node JS ? I have abc directory under which there are some .xml, .rels etc. files, and a few folder workbook, styles, etc. Now I want to zip files under abc folder with the name abc.zip
I don't want the zip to happen with the abc directory i.e. abc.zip -> abc -> and those files; I want abc.zip-> those files.
Currently using JSZIP which is creating the files on generate. but i want to create the files and folders first and then ZIP them
Expected->
on unzipping the abc.zip file ->
workbook (Folder)
styles (Folder)
content.xml (File)
should be extracted instead of "abc" folder.
You can use zip-lib.
var zl = require("zip-lib");
zl.archiveFolder("path/to/abc", "path/to/abc.zip").then(function () {
console.log("done");
}, function (err) {
console.log(err);
});
path/to/abc.zip archive file directory will be as follows:
path/to/abc.zip
.
├── file.xml
├── workbook
├── styles
└── file2.rels
I have a folder containing the following XMLs:
/student/class1/roll1.xml
/student/class1/roll2.xml
.
.
/student/class1/roll90.xml
I have to create class1.zip in /student. I tried
zip -r /student/class1.zip /student/class1/
But it results in a zipped file containing structure:
student/class1/roll1.xml
student/class1/roll2.xml
.
.
student/class1/roll90.xml
I want it (class1.zip) to be created in such a way that it doesn't include the path, it should only include the XML files, like:
class1/roll1.xml
class1/roll2.xml
.
.
class1/roll90.xml
One solution is to put this script (zip -r /student/class1.zip /student/class1/) in /student folder.
Can anyone suggest something else?
Thanks!
Within your script, you should be able to do something like this:
cd /student
zip -r class1.zip class1/
If you have several of these to do under /student you could:
cd /student
for class in class* ; do
zip -r $class.zip $class
done