Lock file in clojure

I want to archive the file in clojure, and I cannot find any libraries for this.

Do you know a good way to zip a file or folder in Clojure? Should I use a java library?

+4
source share
3 answers

Java has a stock of ZipOutputStream that can be used from Clojure. I do not know if there is a library anywhere. I use simple Java functions with a little helper macro:

 (defmacro ^:private with-entry [zip entry-name & body] `(let [^ZipOutputStream zip# ~zip] (.putNextEntry zip# (ZipEntry. ~entry-name)) ~@body (flush) (.closeEntry zip#))) 

Obviously, each ZIP entry describes a file.

 (require '[clojure.java.io :as io]) (with-open [file (io/output-stream "foo.zip") zip (ZipOutputStream. file) wrt (io/writer zip)] (binding [*out* wrt] (doto zip (with-entry "foo.txt" (println "foo")) (with-entry "bar/baz.txt" (println "baz"))))) 

To pin a file, you can do something like this:

 (with-open [output (ZipOutputStream. (io/output-stream "foo.zip")) input (io/input-stream "foo")] (with-entry output "foo" (io/copy input output))) 
+12
source

All file compression and decompression can be done with a simple shell command, accessed through clojure.java.shell

Using the same method, you can also compress and decompress any type of compression that you usually use from your terminal.

 (use '[clojure.java.shell :only [sh]]) (defn unpack-resources [in out] (clojure.java.shell/sh "sh" "-c" (str " unzip " in " -d " out))) (defn pack-resources [in out] (clojure.java.shell/sh "sh" "-c" (str " zip " in " -r " out))) (unpack-resources "/path/to/my/zip/foo.zip" "/path/to/store/unzipped/files") (pack-resources "/path/to/store/archive/myZipArchiveName.zip" "/path/to/my/file/myTextFile.csv") 
+2
source

You can import this (gzip) https://gist.github.com/bpsm/1858654 This is pretty interesting. Or rather, you can use this

 (defn gzip [input output & opts] (with-open [output (-> output clojure.java.io/output-stream GZIPOutputStream.)] (with-open [rdr (clojure.java.io/reader input)] (doall (apply clojure.java.io/copy rdr output opts))))) 
0
source

Source: https://habr.com/ru/post/1494451/


All Articles