On this page:
unzip
make-filesystem-entry-reader
read-zip-directory
zip-directory?
zip-directory-entries
zip-directory-contains?
zip-directory-includes-directory?
unzip-entry
path->zip-path
exn:  fail:  unzip:  no-such-entry

5 zip File Extraction

David Herman

 (require file/unzip) package: base
The file/unzip library provides a function to extract items from a zip archive.

procedure

(unzip in [entry-reader])  void?

  in : (or/c path-string? input-port)
  entry-reader : (bytes? boolean? input-port? . -> . any)
   = (make-filesystem-entry-reader)
Unzips an entire zip archive from in.

For each entry in the archive, the entry-reader procedure is called with three arguments: the byte string representing the entry name, a boolean flag indicating whether the entry represents a directory, and an input port containing the inflated contents of the entry. The default entry-reader unpacks entries to the filesystem; call make-filesystem-entry-reader to configure aspects of the unpacking, such as the destination directory.

procedure

(make-filesystem-entry-reader [#:dest dest-path 
  #:strip-count strip-count 
  #:exists exists]) 
  (bytes? boolean? input-port? . -> . any)
  dest-path : (or/c path-string? #f) = #f
  strip-count : exact-nonnegative-integer? = 0
  exists : 
(or/c 'skip 'error 'replace 'truncate
      'truncate/replace 'append 'update
      'can-update 'must-truncate)
 = 'error
Creates a zip entry reader that can be used with either unzip or unzip-entry and whose behavior is to save entries to the local filesystem. Intermediate directories are always created if necessary before creating files. Directory entries are created as directories in the filesystem, and their entry contents are ignored.

If dest-path is not #f, every path in the archive is prefixed to determine the destination path of the extracted entry.

If strip-count is positive, then strip-count path elements are removed from the entry path from the archive (before prefixing the path with dest-path); if the item’s path contains strip-count elements, then it is not extracted.

If exists is 'skip and the file for an entry already exists, then the entry is skipped. Otherwise, exists is passed on to open-output-file for writing the entry’s inflated content.

Reads the central directory of a zip file and generates a zip directory representing the zip file’s contents. If in is an input port, it must support position setting via file-position.

This procedure performs limited I/O: it reads the list of entries from the zip file, but it does not inflate any of their contents.

procedure

(zip-directory? v)  boolean?

  v : any/c
Returns #t if v is a zip directory, #f otherwise.

procedure

(zip-directory-entries zipdir)  (listof bytes?)

  zipdir : zip-directory?
Extracts the list of entries for a zip archive.

procedure

(zip-directory-contains? zipdir name)  boolean?

  zipdir : zip-directory?
  name : (or/c bytes? path-string?)
Determines whether the given entry name occurs in the given zip directory. If name is not a byte string, it is converted using path->zip-path.

Directory entries match with or without trailing slashes.

procedure

(zip-directory-includes-directory? zipdir    
  name)  boolean?
  zipdir : zip-directory?
  name : (or/c bytes? path-string?)
Determines whether the given name is included anywhere in the given zip directory as a filesystem directory, either as an entry itself or as the containing directory of other entries. If name is not a byte string, it is converted using path->zip-path.

procedure

(unzip-entry path zipdir entry [entry-reader])  void?

  path : (or/c path-string? input-port?)
  zipdir : zip-directory?
  entry : (or/c bytes? path-string?)
  entry-reader : (bytes? boolean? input-port? . -> . any)
   = (make-filesystem-entry-reader)
Unzips a single entry from a zip archive based on a previously read zip directory, zipdir, from read-zip-directory. If in is an input port, it must support position setting via file-position.

The entry parameter is a byte string whose name must be found in the zip file’s central directory. If entry is not a byte string, it is converted using path->zip-path.

The read-entry argument is used to read the contents of the zip entry in the same way as for unzip.

If entry is not in zipdir, an exn:fail:unzip:no-such-entry exception is raised.

procedure

(path->zip-path path)  bytes?

  path : path-string?
Converts a file name potentially containing path separators in the current platform’s format to use path separators recognized by the zip file format: /.

struct

(struct exn:fail:unzip:no-such-entry exn:fail (entry)
  #:extra-constructor-name
  make-exn:fail:unzip:no-such-entry)
  entry : bytes?
Raised when a requested entry cannot be found in a zip archive. The entry field is a byte string representing the requested entry name.