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