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