Skip to content

Commit

Permalink
v0.9.0-beta2
Browse files Browse the repository at this point in the history
  • Loading branch information
luposlip committed Mar 23, 2023
1 parent 3cadc3c commit 0951059
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ All notable changes to this project will be documented in this file. This change

- Append documents (v1.0.0)

## [0.9.0-beta2] - 2023-03-23

### Added

- `nd-db.compress` namespace containing input- and output-stream convenience fns


## [0.9.0-beta1] - 2023-03-19

### Added
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-beta1"]
[com.luposlip/nd-db "0.9.0-beta2"]
```

_Newline Delimited (read-only) Databases!_
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject com.luposlip/nd-db "0.9.0-beta1"
(defproject com.luposlip/nd-db "0.9.0-beta2"
: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"
Expand Down
32 changes: 32 additions & 0 deletions src/nd_db/compress.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(ns nd-db.compress
(:require [clojure.java.io :as io])
(:import [java.io ByteArrayInputStream BufferedInputStream BufferedOutputStream]
[org.apache.commons.compress.archivers.zip
ZipArchiveEntry ZipArchiveOutputStream]
[org.apache.commons.compress.compressors
CompressorInputStream CompressorStreamFactory]))

(defn compressed-input-stream ^CompressorInputStream [filename]
(let [in ^BufferedInputStream (io/input-stream filename)]
(.createCompressorInputStream (CompressorStreamFactory.) in)))

(defn zip-output-stream ^ZipArchiveOutputStream [filename]
(let [out ^BufferedOutputStream (io/output-stream filename)]
(ZipArchiveOutputStream. out)))

(defn write-zip-entry! [^ZipArchiveOutputStream zip-os ^"[B" bytes ^String entry-name]
(let [ze (ZipArchiveEntry. entry-name)]
(.setSize ze (count bytes))
(with-open [is ^ByteArrayInputStream (ByteArrayInputStream. bytes)]
(.putArchiveEntry zip-os ze)
(io/copy is zip-os)
(.closeArchiveEntry zip-os))))

(defn finish-and-close-zip-outputstream! [^ZipArchiveOutputStream 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")
(.finish zos))
11 changes: 4 additions & 7 deletions src/nd_db/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
[io :as ndio]
[id-fns :as ndid]
[csv :as csv]])
(:import [java.io File BufferedInputStream Writer BufferedWriter]
[org.apache.commons.compress.compressors CompressorInputStream CompressorStreamFactory]))
(:import [java.io File Writer BufferedWriter]))

(defn tmpdir []
(System/getProperty "java.io.tmpdir"))
Expand All @@ -38,7 +37,9 @@
(.write (->str data))
(.newLine)))

(defn path->folder+filename [filepath]
(defn path->folder+filename
"Splits filename into path/folder and filename"
[filepath]
(let [ptrn (re-pattern File/separator)
parts (s/split filepath ptrn)]
[(s/join File/separator (butlast parts)) (last parts)]))
Expand Down Expand Up @@ -193,7 +194,3 @@

(defn mv-file [source target]
(shell/sh "mv" source target))

(defn compressed-input-stream ^CompressorInputStream [filename]
(let [in ^BufferedInputStream (io/input-stream filename)]
(.createCompressorInputStream (CompressorStreamFactory.) in)))

0 comments on commit 0951059

Please sign in to comment.