I would like to zip a folder recursevely with clojure. An example of such a folder would be
├── a
├── b
│ ├── c
│ │ └── ccc.txt
│ └── bb.txt
├── c
├── a.txt
└── b.txt
Option 1 : using the OS from within Clojure
What works with zip
in Ubuntu is to execute following in the root of this structure :
zip -r result.zip *
But you have to be in the working dir in order to do this. Using absolute paths will yield other results and omiting paths all together will flatten the structure of course.
Problem is that you cannot change the working directory in Clojure, not that I'm aware of that is..
Option 2 : using native Clojure (or Java)
This should be possible, and I find some zip implementations in Clojure or Java wrappers. Most of them however for single files.
This might be a solution : http://www.java-forums.org/blogs/java-io/973-how-work-zip-files-java.html
but before I try that out I'd like to now or there isn't a good Clojure library around.
What about something like this using clojure.core/file-seq
(require '[clojure.java.io :as io])
(import '[java.util.zip ZipEntry ZipOutputStream])
(with-open [zip (ZipOutputStream. (io/output-stream "foo.zip"))]
(doseq [f (file-seq (io/file "/path/to/directory")) :when (.isFile f)]
(.putNextEntry zip (ZipEntry. (.getPath f)))
(io/copy f zip)
(.closeEntry zip)))