4 zip File Creation

▼ |  | File: Racket File and Format Libraries

1 | Convertible: Data-Conversion Protocol
2 | gzip Compression and File Creation
3 | gzip Decompression
4 | zip File Creation
5 | zip File Extraction
6 | tar File Creation
7 | tar File Extraction
8 | tar+gzip File Extraction
9 | MD5 Message Digest
10 | SHA1 Message Digest
11 | GIF File Writing
12 | ICO File Reading and Writing
13 | Windows Registry
14 | Caching
15 | Globbing
| Bibliography
| Index

On this page:

zip
zip->output
make-zip-entry
zip-entry
zip-verbose

Racket
top contents ← prev up next →

## 4 zip File Creation🔗ℹ

(require file/zip) | package: base

The file/zip library provides utilities to create zip archive files, which are compatible with both Windows and Unix (including Mac OS) unpacking. The actual compression is implemented by deflate.

> > procedure
> > (zip |  | zip-file |  |  |  |
> > |  | path-or-entry ... |  |  |  |
> > | [ | #:level level |  |  |  |
> > |  | #:timestamp timestamp |  |  |  |
> > |  | #:get-timestamp get-timestamp |  |  |  |
> > |  | #:utc-timestamps? utc-timestamps? |  |  |  |
> > |  | #:round-timestamps-down? round-timestamps-down? |  |  |  |
> > |  | #:path-prefix path-prefix |  |  |  |
> > |  | #:system-type sys-type]) |  | → |  | void?
> zip-file : path-string?
> path-or-entry : (or/c path-string? zip-entry?)
> level : exact-integer? = 6
> timestamp : (or/c #f exact-integer?) = #f
> | get-timestamp |  | : |  | (path? . -> . exact-integer?)
> |  |  | = |  |
> (if timestamp
> (lambda (p) timestamp)
> file-or-directory-modify-seconds)
> utc-timestamps? : any/c = #f
> round-timestamps-down? : any/c = #f
> path-prefix : (or/c #f path-string?) = #f
> sys-type : symbol? = (system-type)

Creates zip-file, which holds the complete content of all path-or-entrys. Each path-or-entry is either a path that refers to an existing file or directory on the filesystem, or it is a zip-entry that can override the compression level for that entry or describe an entry without needing corresponding content on the filesystem.

The given paths among path-or-entrys are all expected to be relative path names of existing directories and files (i.e., relative to (current-directory)). If a nested path is provided as a path or string, its ancestor directories are also added to the resulting zip archive, up to the current directory (using pathlist-closure). The path-or-entrys will appear within the zip archive in the same order in which they are provided. Settings contained in a zip-entry will be used for that entry in the archive in preference to values provided as keyword arguments to this function.

Files are packaged as usual for zip files, including permission bits for both Windows and Unix (including Mac OS). The permission bits are determined by file-or-directory-permissions, which does not preserve the distinction between owner/group/other permissions. Also, symbolic links are always followed.

The get-timestamp function is used to obtain the modification date to record in the archive for a file or directory. Normally, zip archives record modification dates in local time, but if utc-timestamps? is true, then the UTC time is recorded. Timestamps in zip archives are precise only to two seconds; by default, the time is rounded toward the future (like WinZip or PKZIP), but time is rounded toward the past (like Java) if round-timestamps-down? is true.

The sys-type argument determines the system type recorded in the archive.

If path-prefix is not #f, then it prefixes the name of each path as it is written in the zip file, and directory entries are added for each element of path-prefix.

The level argument controls compression from 0 to 9. A level of 0 stores file entries without compression, while higher levels write Deflate-compressed entries.

This interface can be used for formats like EPUB, where specific files must appear first in the archive and be stored without compression.

Changed in version 6.0.0.3 of package base: Added the #:get-timestamp and #:system-type arguments.
Changed in version 6.0.1.12: Added the #:path-prefix, #:utc-timestamps?, and #:utc-timestamps-down? arguments.
Changed in version 9.2.0.5: Added support for zip-entry arguments.

> > procedure
> > (zip->output |  | paths-and-entries |
> > | [ | out |
> > |  | #:level level |
> > |  | #:timestamp timestamp |
> > |  | #:get-timestamp get-timestamp |
> > |  | #:utc-timestamps? utc-timestamps? |
> > |  | #:round-timestamps-down? round-timestamps-down? |
> > |  | #:path-prefix path-prefix |
> > |  | #:system-type sys-type]) |
> → void?
> paths-and-entries : (listof (or/c path-string? zip-entry?))
> out : output-port? = (current-output-port)
> level : exact-integer? = 6
> timestamp : (or/c #f exact-integer?) = #f
> | get-timestamp |  | : |  | (path? . -> . exact-integer?)
> |  |  | = |  |
> (if timestamp
> (lambda (p) timestamp)
> file-or-directory-modify-seconds)
> utc-timestamps? : any/c = #f
> round-timestamps-down? : any/c = #f
> path-prefix : (or/c #f path-string?) = #f
> sys-type : symbol? = (system-type)

Like zip, but packages each element of the given paths-and-entries in a zip file written directly to out. The specified paths-and-entries are included as-is (except for adding path-prefix, if any); if a directory is specified, its content is not automatically added, and nested directories are added without parent directories.

As with zip, a level of 0 stores file entries without compression, while higher levels write Deflate-compressed entries.

Changed in version 6.0.0.3 of package base: Added the #:get-timestamp and #:system-type arguments.
Changed in version 6.0.1.12: Added the #:path-prefix, #:utc-timestamps?, and #:utc-timestamps-down? arguments.
Changed in version 9.2.0.5: Added support for zip-entry arguments.

> procedure (make-zip-entry path level) → zip-entry?
> path : (and/c path-string? relative-path?)
> level : exact-integer?
> (make-zip-entry |  | kind |  |  |  |
> |  | path |  |  |  |
> |  | content |  |  |  |
> |  | size |  |  |  |
> |  | attribs |  |  |  |
> |  | level) |  | → |  | zip-entry?
> kind : (or/c 'path 'file 'directory)
> path : (and/c path-string? relative-path?)
> content : (or/c input-port? (-> input-port?) bytes? #f)
> size : (or/c #f exact-nonnegative-integer?)
> attribs : (hash/c symbol? any/c)
> level : exact-integer?

Creates a zip-entry value.

The two-argument form is a shorthand for an existing filesystem path whose archive representation should use the given compression level.

The six-argument form accepts arguments matching all zip-entry fields:

- kind is one of 'path, 'file, or 'directory.

- path is always a relative archive path.

- If kind is 'path, then path names an existing filesystem path, and content and size are ignored.

- If kind is 'file, then path names the archive entry, and content supplies the bytes using an input port, a thunk that produces an input port, or a byte string. If size is not #f, then it must match the number of bytes supplied by content.

- If kind is 'directory, then path names the directory entry, and content is expected to be #f.

- The attribs hash can contain 'permissions and 'modify-seconds to override the default metadata that is recorded in the archive.

- level controls compression using the same 0-to-9 convention as zip. A level of 0 stores the entry without compression, while higher levels write Deflate-compressed entries.

This form makes it possible to create zip archives directly from in-memory content without constructing a corresponding directory tree on the filesystem.

Added in version 9.2.0.5 of package base.

> struct (struct zip-entry (kind path content size attribs level))
> kind : (or/c 'path 'file 'directory)
> path : (and/c path-string? relative-path?)
> content : (or/c input-port? (-> input-port?) bytes? #f)
> size : (or/c #f exact-nonnegative-integer?)
> attribs : (hash/c symbol? any/c)
> level : exact-integer?

Represents a zip archive entry to be included by zip or zip->output. See make-zip-entry.

Added in version 9.2.0.5 of package base.

> parameter (zip-verbose) → boolean?
> (zip-verbose on?) → void?
> on? : any/c

A parameter that controls output during a zip operation. Setting this parameter to a true value causes zip to display to (current-error-port) the filename that is currently being compressed.
top contents ← prev up next →