xref: /freebsd/usr.bin/compress/compress.1 (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1.\" Copyright (c) 1986, 1990, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" James A. Woods, derived from original work by Spencer Thomas
6.\" and Joseph Orost.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\"    must display the following acknowledgement:
18.\"	This product includes software developed by the University of
19.\"	California, Berkeley and its contributors.
20.\" 4. Neither the name of the University nor the names of its contributors
21.\"    may be used to endorse or promote products derived from this software
22.\"    without specific prior written permission.
23.\"
24.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34.\" SUCH DAMAGE.
35.\"
36.\"     @(#)compress.1	8.2 (Berkeley) 4/18/94
37.\" $FreeBSD$
38.\"
39.Dd May 17, 2002
40.Dt COMPRESS 1
41.Os
42.Sh NAME
43.Nm compress ,
44.Nm uncompress
45.Nd compress and expand data
46.Sh SYNOPSIS
47.Nm
48.Op Fl fv
49.Op Fl b Ar bits
50.Op Ar
51.Nm
52.Fl c
53.Op Fl b Ar bits
54.Op Ar file
55.Nm uncompress
56.Op Fl f
57.Op Ar
58.Nm uncompress
59.Fl c
60.Op Ar file
61.Sh DESCRIPTION
62The
63.Nm
64utility reduces the size of files using adaptive Lempel-Ziv coding.
65Each
66.Ar file
67is renamed to the same name plus the extension
68.Pa .Z .
69A
70.Ar file
71argument with a
72.Pa .Z
73extension will be ignored except it will cause an
74error exit after other arguments are processed.
75If compression would not reduce the size of a
76.Ar file ,
77the file is ignored.
78.Pp
79The
80.Nm uncompress
81utility restores compressed files to their original form, renaming the
82files by deleting the
83.Pa .Z
84extensions.
85A file specification need not include the file's
86.Pa .Z
87extension.
88If a file's name in its file system does not have a
89.Pa .Z
90extension, it will not be uncompressed and it will cause
91an error exit after other arguments are processed.
92.Pp
93If renaming the files would cause files to be overwritten and the standard
94input device is a terminal, the user is prompted (on the standard error
95output) for confirmation.
96If prompting is not possible or confirmation is not received, the files
97are not overwritten.
98.Pp
99As many of the modification time, access time, file flags, file mode,
100user ID, and group ID as allowed by permissions are retained in the
101new file.
102.Pp
103If no files are specified or a
104.Ar file
105argument is a single dash
106.Pq Sq Fl ,
107the standard input is compressed or uncompressed to the standard output.
108If either the input and output files are not regular files, the checks for
109reduction in size and file overwriting are not performed, the input file is
110not removed, and the attributes of the input file are not retained
111in the output file.
112.Pp
113The options are as follows:
114.Bl -tag -width ".Fl b Ar bits"
115.It Fl b Ar bits
116The code size (see below) is limited to
117.Ar bits ,
118which must be in the range 9..16.
119The default is 16.
120.It Fl c
121Compressed or uncompressed output is written to the standard output.
122No files are modified.
123The
124.Fl v
125option is ignored.
126Compression is attempted even if the results will be larger than the
127original.
128.It Fl f
129Files are overwritten without prompting for confirmation.
130Also, for
131.Nm compress ,
132files are compressed even if they are not actually reduced in size.
133.It Fl v
134Print the percentage reduction of each file.
135Ignored by
136.Nm uncompress
137or if the
138.Fl c
139option is also used.
140.El
141.Pp
142The
143.Nm
144utility uses a modified Lempel-Ziv algorithm.
145Common substrings in the file are first replaced by 9-bit codes 257 and up.
146When code 512 is reached, the algorithm switches to 10-bit codes and
147continues to use more bits until the
148limit specified by the
149.Fl b
150option or its default is reached.
151.Pp
152After the limit is reached,
153.Nm
154periodically checks the compression ratio.
155If it is increasing,
156.Nm
157continues to use the existing code dictionary.
158However, if the compression ratio decreases,
159.Nm
160discards the table of substrings and rebuilds it from scratch.
161This allows
162the algorithm to adapt to the next "block" of the file.
163.Pp
164The
165.Fl b
166option is unavailable for
167.Nm uncompress
168since the
169.Ar bits
170parameter specified during compression
171is encoded within the output, along with
172a magic number to ensure that neither decompression of random data nor
173recompression of compressed data is attempted.
174.Pp
175The amount of compression obtained depends on the size of the
176input, the number of
177.Ar bits
178per code, and the distribution of common substrings.
179Typically, text such as source code or English is reduced by 50\-60%.
180Compression is generally much better than that achieved by Huffman
181coding (as used in the historical command pack), or adaptive Huffman
182coding (as used in the historical command compact), and takes less
183time to compute.
184.Sh EXIT STATUS
185.Ex -std compress uncompress
186.Pp
187The
188.Nm compress
189utility exits 2 if attempting to compress a file would not reduce its size
190and the
191.Fl f
192option was not specified and if no other error occurs.
193.Sh SEE ALSO
194.Xr gunzip 1 ,
195.Xr gzexe 1 ,
196.Xr gzip 1 ,
197.Xr zcat 1 ,
198.Xr zmore 1 ,
199.Xr znew 1
200.Rs
201.%A Welch, Terry A.
202.%D June, 1984
203.%T "A Technique for High Performance Data Compression"
204.%J "IEEE Computer"
205.%V 17:6
206.%P pp. 8-19
207.Re
208.Sh STANDARDS
209The
210.Nm compress
211and
212.Nm uncompress
213utilities conform to
214.St -p1003.1-2001 .
215.Sh HISTORY
216The
217.Nm
218command appeared in
219.Bx 4.3 .
220.Sh BUGS
221Some of these might be considered otherwise-undocumented features.
222.Pp
223.Nm compress :
224If the utility does not compress a file because doing so would not
225reduce its size, and a file of the same name except with an
226.Pa .Z
227extension exists, the named file is not really ignored as stated above;
228it causes a prompt to confirm the overwriting of the file with the extension.
229If the operation is confirmed, that file is deleted.
230.Pp
231.Nm uncompress :
232If an empty file is compressed (using
233.Fl f ) ,
234the resulting
235.Pa .Z
236file is also empty.
237That seems right, but if
238.Nm uncompress
239is then used on that file, an error will occur.
240.Pp
241Both utilities: If a
242.Sq Fl
243argument is used and the utility prompts the user, the standard input
244is taken as the user's reply to the prompt.
245.Pp
246Both utilities:
247If the specified file does not exist, but a similarly-named one with (for
248.Nm compress )
249or without (for
250.Nm uncompress )
251a
252.Pa .Z
253extension does exist, the utility will waste the user's time by not
254immediately emitting an error message about the missing file and
255continuing.
256Instead, it first asks for confirmation to overwrite
257the existing file and then does not overwrite it.
258