Skip to content

Commit

Permalink
Compress tar.gz helper fns, and update deps
Browse files Browse the repository at this point in the history
  • Loading branch information
luposlip committed May 29, 2024
1 parent 1e906be commit 28b25d8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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!_
Expand Down
10 changes: 5 additions & 5 deletions project.clj
Original file line number Diff line number Diff line change
@@ -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})
22 changes: 22 additions & 0 deletions src/nd_db/compress.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand All @@ -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))
Expand All @@ -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.)]
Expand All @@ -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")
Expand Down

0 comments on commit 28b25d8

Please sign in to comment.