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