io.archive zipped file access

Author: Chris Zheng  (z@caudate.me)
Date: 27 November 2018
Repository: https://github.com/zcaudate/hara
Version: 3.0.2

1    Introduction

hara.io.archive allows access to .jar and .zip files using the java.nio framework.

1.1    Installation

Add to project.clj dependencies:

[hara/io.archive "3.0.2"]

All functions are in the hara.io.archive namespace.

 (use (quote hara.io.archive))

2    API



archive ^

puts files into an archive

v 3.0
(defn archive
  ([archive root]
   (let [ach (open archive)
         res (protocol.archive/-archive ach
                                        root
                                        (fs/select root {:exclude [fs/directory?]}))]
     (.close ach)
     res))
  ([archive root inputs]
   (protocol.archive/-archive (open archive) root inputs)))
link
(archive "dev/scratch/hello.jar" "src") => coll?

create ^

creats a zip file

v 3.0
(defn create
  [archive]
  (if (fs/exists? archive)
    (throw (ex-info "Archive already exists" {:path archive}))
    (let [path (fs/path archive)]
      (do (fs/create-directory (fs/parent path))
          (FileSystems/newFileSystem
           (URI. (str "jar:file:" path))
           {"create" "true"})))))
link
(fs/delete "dev/scratch/hello.jar") (create "dev/scratch/hello.jar") => zip-system?

extract ^

extracts all file from an archive

v 3.0
(defn extract
  ([archive]
   (extract archive (fs/parent (url archive))))
  ([archive output]
   (extract archive output (list archive)))
  ([archive output entries]
   (protocol.archive/-extract (open archive {:create false}) output entries)))
link
(extract "dev/scratch/hello.jar") => coll?

has? ^

checks if the archive has a particular entry

v 3.0
(defn has?
  [archive entry]
  (protocol.archive/-has? (open archive {:create false}) entry))
link
(has? "dev/scratch/hello.jar" "world.java") => false

insert ^

inserts a file to an entry within the archive

v 3.0
(defn insert
  [archive entry input]
  (protocol.archive/-insert (open archive) entry input))
link
(insert "dev/scratch/hello.jar" "project.clj" "project.clj") => fs/path?

list ^

lists all the entries in the archive

v 3.0
(defn list
  [archive]
  (protocol.archive/-list (open archive {:create false})))
link
(map str (list "dev/scratch/hello.jar")) => ["/"]

open ^

either opens an existing archive or creates one if it doesn't exist

v 3.0
(defn open
  ([archive]
   (open archive {:create true}))
  ([archive opts]
   (cond (instance? FileSystem archive)
         archive

         :else
         (let [path (fs/path archive)]
           (cond (fs/exists? path)
                 (FileSystems/newFileSystem path nil)

                 (:create opts)
                 (create archive)

                 :else
                 (throw (ex-info "Archive does not exist" {:path archive})))))))
link
(open "dev/scratch/hello.jar" {:create true}) => zip-system?

path ^

returns the url of the archive

v 3.0
(defn path
  [archive entry]
  (protocol.archive/-path (open archive {:create false}) entry))
link
(-> (open "dev/scratch/hello.jar") (path "world.java") (str)) => "world.java"

remove ^

removes an entry from the archive

v 3.0
(defn remove
  [archive entry]
  (protocol.archive/-remove (open archive {:create false}) entry))
link
(remove "dev/scratch/hello.jar" "project.clj") => #{"project.clj"}

stream ^

creates a stream for an entry wthin the archive

v 3.0
(defn stream
  [archive entry]
  (protocol.archive/-stream (open archive {:create false}) entry))
link
(do (insert "dev/scratch/hello.jar" "project.clj" "project.clj") (slurp (stream "dev/scratch/hello.jar" "project.clj"))) => (slurp "project.clj")

url ^

returns the url of the archive

v 3.0
(defn url
  [archive]
  (protocol.archive/-url archive))
link
(url (open "dev/scratch/hello.jar")) => (str (fs/path "dev/scratch/hello.jar"))

write ^

writes files to an archive

v 3.0
(defn write
  [archive entry stream]
  (protocol.archive/-write (open archive {:create false}) entry stream))
link
(doto "dev/scratch/hello.jar" (fs/delete) (open) (write "test.stuff" (binary/input-stream (.getBytes "Hello World")))) (slurp (stream (open "dev/scratch/hello.jar") "test.stuff")) => "Hello World"

zip-system? ^

checks if object is a `ZipSystem`

v 3.0
(defn zip-system?
  [obj]
  (= ZipFileSystem (type obj)))
link
(zip-system? (open "dev/scratch/hello.jar")) => true