I have a a directory with zip archives containing .jpg, .png, .gif images. I want to unzip each archive taking the images only and putting them in a folder with the name of the archive.
So:
files/archive1.zip
files/archive2.zip
files/archive3.zip
files/archive4.zip
Open archive1.zip - take sunflower.jpg, rose_sun.gif. Make a folder files/archive1/ and add the images to that folder, so files/archive1/folder1.jpg, files/archive1/rose_sun.gif. Do this to each archive.
I really don't know how this can be done, all suggestions are welcome. I have over 600 archives and an automatic solution would be a lifesaver, preferably a linux solution.
Perl's Archive-Zip is a good library for zipping/unzipping.
You can write a program using a zip library. If you do Mono, you can use DotNetZip.
The code would look like this:
In Short
You can do this with a one-liner find + unzip.
In Detail
unzip
allows you to specify the files you want:And
-d
a target directory:Combine that with a
find
, and you can extract all the images in all zips:Using
unzip -j
to junk the extraction of the zip's internal directory structure, we can do it all in one command. This gives you the flat image list separated by zip name that you desire as a one-liner.A limitation is that
unzip -d
won't create more than one new level of directories, so justmkdir images
first. Enjoy.7zip can do this, and has a Linux version.
(Just tested it, it works.)
Something along the lines of:
This is tested and will handle spaces in filenames (both the zip files and the extracted files). You may have collisions if the zip file has the same file name in different directories (you can't avoid this if you're going to flatten the directory structure).
Here's my take on the first answer...
or, if you'd just like to use the regular unzip command...
I haven't tested this, but it should work... or something along these lines. Definitely more efficient than the first solution. :)
Hope this helps!