[zip-devel] Addition to the ZIP file reading ability

Jonathan Lee 2jlee at earthlink.net
Sat Oct 29 21:21:16 UTC 2005


Thank you for developing this package.  I found it very useful when I 
was working on the
PythonChallenge using Lisp as my "language of choice." During one 
particular challenge
puzzle I needed to be able to access the comments added to each file so 
I added the ability
to extract the comments for each file within a Zip archive to your 
code.  I've listed the Lisp
forms that I  changed below. If you feel that this is a useful change, 
please add it to your
project.

The comments can be extracted for each file entry using the syntax below:

(file-entry-comment file-entry) => string

Here are the changed forms in the "zip.lisp" file:

(defstruct zipfile-entry
  name
  stream
  offset
  size
  compressed-size
  comment)

(defun read-entry-object (s external-format)
  (let* ((header (make-directory-entry s))
     (name (make-array (cd/name-length header)
                    :element-type '(unsigned-byte 8)))
         (comment (make-array (cd/comment-length header)
                    :element-type '(unsigned-byte 8))))
    (assert (= (cd/signature header) #x02014b50))
    (read-sequence name s)
    (setf name (octets-to-string name external-format))
    (file-position s (+ (file-position s)
                        (cd/extra-length header)))
    (read-sequence comment s)
    (setf comment (octets-to-string comment external-format))
    (make-zipfile-entry :name name
                        :stream s
                        :offset (cd/offset header)
                        :size (cd/size header)
                        :compressed-size (cd/compressed-size header)
                        :comment comment)))

In the "package.lisp" file, I simply added the new function to the 
export list like so:

(defpackage :zip
  (:use :cl)
  (:export #:zipfile                     ;reading ZIP files
       #:open-zipfile
       #:close-zipfile
       #:with-zipfile
       #:zipfile-entries
       #:get-zipfile-entry
       #:zipfile-entry-name
       #:zipfile-entry-size
       #:zipfile-entry-comment
       #:do-zipfile-entries
       #:zipfile-entry-contents
       #:unzip
       #:with-output-to-zipfile     ;writing ZIP files
       #:write-zipentry
       #:zip
       #:inflate                            ;inflate.lisp
       #:skip-gzip-header
       #:compress                      ;deflate.lisp
       #:store)
  #-allegro
  (:import-from #+sbcl :sb-gray
                #+lispworks :stream
                #-(or sbcl lispworks) ...
                #:fundamental-binary-output-stream
                #:stream-write-sequence
                #:fundamental-binary-input-stream
                #:stream-read-byte
                #:stream-read-sequence))

Sincerely,
Jonathan Lee
(format t "~A@~A.~A" '2jlee 'earthlink 'net)



More information about the zip-devel mailing list