# How to zip folders

I know that one can archive data with Mathematica using: CreateArchive[source,path]

I have a lot of folders (folder1, folder2,folder2 etc.) that I would like to compress as a batch. The result should be: folder1.zip, folder2.zip, folder3.zip.

Anyone knows how to do that with mMathematica ?

could be a starting point: https://stackoverflow.com/questions/44644428/batch-file-to-zip-folders-with-original-names

• I feel this is one of those times where you should be using a dedicated program, instead of trying to do it in Mathematica. Oct 20, 2018 at 14:39
• @J.M.iscomputer-less I know, but it would be so convenient. I don't care if it takes a bit longer. Oct 20, 2018 at 14:41
• I think if you wish to add to an already compress zip folder it would require unzipping it, and re-zipping it with the new contents.
– ktm
Oct 20, 2018 at 14:43
• @user6014 Or simply putting everything into a folder and zipping it, and then deleting the original files Oct 20, 2018 at 15:13
• @james Can you describe in more detail exactly what you are after? I think one of the main reasons you haven't gotten a ton of replies here is that it's entirely unclear what you want.
– ktm
Oct 22, 2018 at 20:08

This can be achieved with Map (/@).

1) Identify the folders you want to compress:

In[30]:= FileNames["folder*"]

Out[30]= {"folder1", "folder2", "folder3"}


2) Map CreateArchive across them:

In[31]:= CreateArchive /@ FileNames["folder*"]

Out[31]= {"/Users/user/Desktop/folder1.zip", \
"/Users/user/Desktop/folder2.zip", \
"/Users/user/Desktop/folder3.zip"}


Edit:

To delete the uncompressed folders after, you can run

DeleteDirectory[#, DeleteContents -> True]& /@ {"folder1", "folder2", "folder3"}


(Using FileNames["folder*"] here will possibly delete the .zip folders a well, so I'd refernce the original files directly)

• Thanks a lot ! This is exactly what I was looking for ! Is it also possible to delete the uncompressed folders afterwards ? Oct 23, 2018 at 12:24
• @james added that
– ktm
Oct 23, 2018 at 13:26
• I did that a lot recently, but every now and then, I had some temporary files left over. Mathematica creates a temporary folder and sometimes just leaves it there. Has someone else seen this? Adding a Pause[0.25] helped a bit, but not always. Oct 23, 2018 at 15:19
• To avoid the temporary folders, try the option CreateIntermediateDirectories -> False with the CreateArchive command. Oct 23, 2018 at 18:38