Skip to content

Commit

Permalink
Release v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
luposlip committed Jul 17, 2024
1 parent fedc09c commit 1781b27
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ All notable changes to this project will be documented in this file. This change

## [Unreleased]

- nothing yet
...

## 0.2.0 - 2024-07-17

### Added
- fn `targz-entries` that return a lazy seq of entry data.

## 0.1.0 - 2024-06-11
### Added
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/clarch "0.1.0"
(defproject com.luposlip/clarch "0.2.0"
:description "Clojure Archiving library"
:url "https://github.com/luposlip/clarch"
:license {:name "Apache License, Version 2.0"
Expand Down
38 changes: 28 additions & 10 deletions src/clarch/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@
GzipCompressorOutputStream GzipCompressorInputStream]
[org.apache.commons.compress.utils IOUtils]))

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

(defn targz-input-stream ^TarArchiveInputStream [filename]
(->> filename
io/input-stream
(GzipCompressorInputStream.)
(TarArchiveInputStream.)))

(defn zip-output-stream ^ZipArchiveOutputStream [filename]
(let [out ^BufferedOutputStream (io/output-stream filename)]
(ZipArchiveOutputStream. out)))
Expand All @@ -26,7 +32,7 @@
(let [out ^BufferedOutputStream (io/output-stream filename)]
(TarArchiveOutputStream. out)))

(defn tar-gz-output-stream
(defn targz-output-stream
"Creates a compressed TAR ball"
^TarArchiveOutputStream [filename]
(let [bos ^BufferedOutputStream (io/output-stream filename)
Expand Down Expand Up @@ -69,12 +75,16 @@
(.close zf)
bytes))

(defn finish-and-close-zip-outputstream! [^ZipArchiveOutputStream zos]
(defmulti finish-and-close-outputstream! type)

(defmethod finish-and-close-outputstream! ZipArchiveOutputStream
[^ZipArchiveOutputStream zos]
(doto zos
(.finish)
(.close)))

(defn finish-and-close-tar-outputstream! [^TarArchiveOutputStream zos]
(defmethod finish-and-close-outputstream! TarArchiveOutputStream
[^TarArchiveOutputStream zos]
(doto zos
(.finish)
(.close)))
Expand All @@ -84,21 +94,29 @@
(IOUtils/copy tar-input bais)
(.toByteArray bais)))

(defn combine-tar-gz [sources target]
(defn combine-targz [sources target]
{:pre [(every? string? sources)
(string? target)]}
(with-open [out ^TarArchiveOutputStream (tar-gz-output-stream target)]
(with-open [out ^TarArchiveOutputStream (targz-output-stream target)]
(doseq [^String input sources]
(with-open [in ^TarArchiveInputStream (->> input
io/input-stream
(GzipCompressorInputStream.)
(TarArchiveInputStream.))]
(with-open [in ^TarArchiveInputStream (targz-input-stream input)]
(loop [entry ^TarArchiveEntry (.getNextEntry in)]
(when entry
(write-tar-entry! out
(read-current-tar-entry in)
(.getName entry))
(recur (.getNextEntry in))))))))

(defn targz-entries
"Return a lazy seq of entries from TarArchiveInputstream.
Returned as a map with keys :filename and :data (byte array)"
[^TarArchiveInputStream targz-in]
(lazy-seq
(when-let [entry ^TarArchiveEntry (.getNextEntry targz-in)]
(cons {:filename (.getName entry)
:data (read-current-tar-entry targz-in)}
(targz-entries targz-in)))))

#_
(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 1781b27

Please sign in to comment.