From 28b25d88a82cd239962e28396cdff2eda556c156 Mon Sep 17 00:00:00 2001 From: Henrik Mohr Date: Wed, 29 May 2024 15:50:38 +0200 Subject: [PATCH] Compress tar.gz helper fns, and update deps --- CHANGELOG.md | 10 ++++++++++ README.md | 2 +- project.clj | 10 +++++----- src/nd_db/compress.clj | 22 ++++++++++++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e1e30a..b2f053a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ All notable changes to this project will be documented in this file. This change - Timestamps for historical versions - Optimize (speed+size of) low level index format +## [0.9.0-beta10] - 2024-05-29 + +### Enhanced + +- Added compress fns to work with .tar.gz archives + +### Updated + +- Updated deps + ## [0.9.0-beta9] - 2024-02-07 ### Enhanced diff --git a/README.md b/README.md index 0b39dce..73eba06 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # nd-db ```clojure -[com.luposlip/nd-db "0.9.0-beta9"] +[com.luposlip/nd-db "0.9.0-beta10"] ``` _Newline Delimited (read-only) Databases!_ diff --git a/project.clj b/project.clj index 372f9f9..f2d0983 100644 --- a/project.clj +++ b/project.clj @@ -1,12 +1,12 @@ -(defproject com.luposlip/nd-db "0.9.0-beta9" +(defproject com.luposlip/nd-db "0.9.0-beta10" :description "Clojure library to use newline delimited files as fast read-only databases." :url "https://github.com/luposlip/nd-db" :license {:name "Apache License, Version 2.0" :url "https://www.apache.org/licenses/LICENSE-2.0"} - :dependencies [[org.clojure/clojure "1.11.1"] - [com.taoensso/nippy "3.3.0"] - [org.apache.commons/commons-compress "1.25.0"] + :dependencies [[org.clojure/clojure "1.11.3"] + [com.taoensso/nippy "3.4.2"] + [org.apache.commons/commons-compress "1.26.2"] [digest "1.4.10"] - [cheshire "5.12.0"]] + [cheshire "5.13.0"]] :global-vars {*warn-on-reflection* true} :repl-options {:init-ns nd-db.core}) diff --git a/src/nd_db/compress.clj b/src/nd_db/compress.clj index fc310f4..3415884 100644 --- a/src/nd_db/compress.clj +++ b/src/nd_db/compress.clj @@ -5,6 +5,8 @@ BufferedInputStream BufferedOutputStream] [org.apache.commons.compress.archivers.zip ZipArchiveEntry ZipArchiveOutputStream ZipFile] + [org.apache.commons.compress.archivers.tar + TarArchiveEntry TarArchiveOutputStream TarFile] [org.apache.commons.compress.compressors CompressorInputStream CompressorStreamFactory])) @@ -16,6 +18,10 @@ (let [out ^BufferedOutputStream (io/output-stream filename)] (ZipArchiveOutputStream. out))) +(defn tar-output-stream ^TarArchiveOutputStream [filename] + (let [out ^BufferedOutputStream (io/output-stream filename)] + (TarArchiveOutputStream. out))) + (defn write-zip-entry! [^ZipArchiveOutputStream zip-os ^"[B" bytes ^String entry-name] (let [ze (ZipArchiveEntry. entry-name)] (.setSize ze (count bytes)) @@ -24,6 +30,17 @@ (io/copy is zip-os) (.closeArchiveEntry zip-os)))) +(defn write-tar-entry! [^TarArchiveOutputStream tar-os ^"[B" bytes ^String entry-name] + (let [te (TarArchiveEntry. entry-name)] + (.setSize te (count bytes)) + (with-open [is ^ByteArrayInputStream (ByteArrayInputStream. bytes)] + (.putArchiveEntry tar-os te) + (io/copy is tar-os) + (.closeArchiveEntry tar-os)))) + +(defn tar-file [^String filename] + (TarFile. (io/file filename))) + (defn read-zip-entry! ^"[B" [^ZipFile zf ^String entry-name] (let [ze (.getEntry zf entry-name) baos (ByteArrayOutputStream.)] @@ -41,6 +58,11 @@ (doto zos (.finish) (.close))) + +(defn finish-and-close-tar-outputstream! [^TarArchiveOutputStream zos] + (doto zos + (.finish) + (.close))) #_ (with-open [zos (zip-output-stream "filename.zip")] (write-zip-entry! zos (.getBytes "bytes-to-write") "zip-entry-name.txt")