-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fedc09c
Showing
5 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/target | ||
/classes | ||
/checkouts | ||
profiles.clj | ||
pom.xml | ||
pom.xml.asc | ||
*.jar | ||
*.class | ||
/.lein-* | ||
/.nrepl-port | ||
/.prepl-port | ||
.hgignore | ||
.hg/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Change Log | ||
All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). | ||
|
||
## [Unreleased] | ||
|
||
- nothing yet | ||
|
||
## 0.1.0 - 2024-06-11 | ||
### Added | ||
- Initial public release - **EVERYTHING** may change! | ||
|
||
[Unreleased]: https://github.com/luposlip/clarch/compare/0.1.1...HEAD | ||
[0.1.0]: https://github.com/luposlip/clarch/compare/HEAD...0.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
![Clojure CI](https://github.com/luposlip/clarch/workflows/Clojure%20CI/badge.svg?branch=main) [![Clojars Project](https://img.shields.io/clojars/v/com.luposlip/clarch.svg)](https://clojars.org/com.luposlip/clarch) [![Dependencies Status](https://versions.deps.co/luposlip/clarch/status.svg)](https://versions.deps.co/luposlip/clarch) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) | ||
|
||
# clarch | ||
|
||
```clojure | ||
[com.luposlip/clarch "0.1.0"] | ||
``` | ||
|
||
_CLojure ARCHiver_ | ||
|
||
A Clojure library for creating, reading and handling (compressed) file archives | ||
of various sort, such as .zip, .tar, .tar.gz. | ||
|
||
## Usage | ||
|
||
**Will follow soon!** | ||
|
||
Use on own risk - functions, functionality and everything may change! | ||
|
||
## Copyright & License | ||
|
||
Copyright © 2024 Henrik Mohr | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(defproject com.luposlip/clarch "0.1.0" | ||
:description "Clojure Archiving library" | ||
:url "https://github.com/luposlip/clarch" | ||
:license {:name "Apache License, Version 2.0" | ||
:url "https://www.apache.org/licenses/LICENSE-2.0"} | ||
:dependencies [[org.clojure/clojure "1.11.3"] | ||
[org.apache.commons/commons-compress "1.26.2"]] | ||
:repl-options {:init-ns clarch.core}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
(ns clarch.core | ||
(:require [clojure.java.io :as io]) | ||
(:import [java.io | ||
ByteArrayInputStream ByteArrayOutputStream | ||
BufferedInputStream BufferedOutputStream] | ||
[org.apache.commons.compress.archivers.zip | ||
ZipArchiveEntry ZipArchiveOutputStream ZipFile] | ||
[org.apache.commons.compress.archivers.tar TarFile | ||
TarArchiveEntry TarArchiveOutputStream TarArchiveInputStream] | ||
[org.apache.commons.compress.compressors | ||
CompressorInputStream CompressorStreamFactory] | ||
[org.apache.commons.compress.compressors.gzip | ||
GzipCompressorOutputStream GzipCompressorInputStream] | ||
[org.apache.commons.compress.utils IOUtils])) | ||
|
||
(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 tar-output-stream "Creates an uncompressed tar ball" | ||
^TarArchiveOutputStream [filename] | ||
(let [out ^BufferedOutputStream (io/output-stream filename)] | ||
(TarArchiveOutputStream. out))) | ||
|
||
(defn tar-gz-output-stream | ||
"Creates a compressed TAR ball" | ||
^TarArchiveOutputStream [filename] | ||
(let [bos ^BufferedOutputStream (io/output-stream filename) | ||
gzo (GzipCompressorOutputStream. bos)] | ||
(TarArchiveOutputStream. gzo))) | ||
|
||
(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 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.)] | ||
(when ze | ||
(io/copy (.getInputStream zf ze) baos) | ||
(.toByteArray ^ByteArrayOutputStream baos)))) | ||
|
||
(defn read-first-zip-entry! ^"[B" [^String zip-file] | ||
(let [zf (ZipFile. zip-file) | ||
bytes (read-zip-entry! | ||
zf | ||
(.getName ^ZipArchiveEntry (.nextElement (.getEntries zf))))] | ||
(.close zf) | ||
bytes)) | ||
|
||
(defn finish-and-close-zip-outputstream! [^ZipArchiveOutputStream zos] | ||
(doto zos | ||
(.finish) | ||
(.close))) | ||
|
||
(defn finish-and-close-tar-outputstream! [^TarArchiveOutputStream zos] | ||
(doto zos | ||
(.finish) | ||
(.close))) | ||
|
||
(defn ^"[B" read-current-tar-entry [^TarArchiveInputStream tar-input] | ||
(let [bais (ByteArrayOutputStream. (.getSize (.getCurrentEntry tar-input)))] | ||
(IOUtils/copy tar-input bais) | ||
(.toByteArray bais))) | ||
|
||
(defn combine-tar-gz [sources target] | ||
{:pre [(every? string? sources) | ||
(string? target)]} | ||
(with-open [out ^TarArchiveOutputStream (tar-gz-output-stream target)] | ||
(doseq [^String input sources] | ||
(with-open [in ^TarArchiveInputStream (->> input | ||
io/input-stream | ||
(GzipCompressorInputStream.) | ||
(TarArchiveInputStream.))] | ||
(loop [entry ^TarArchiveEntry (.getNextEntry in)] | ||
(when entry | ||
(write-tar-entry! out | ||
(read-current-tar-entry in) | ||
(.getName entry)) | ||
(recur (.getNextEntry in)))))))) | ||
#_ | ||
(with-open [zos (zip-output-stream "filename.zip")] | ||
(write-zip-entry! zos (.getBytes "bytes-to-write") "zip-entry-name.txt") | ||
(.finish zos)) |