xref: /openbsd-src/usr.bin/compress/compress.1 (revision 3ab258a16e35bc5259bb711e9cca5c9e26d26fc9)
1.\"	$OpenBSD: compress.1,v 1.49 2022/10/22 14:41:27 millert Exp $
2.\"	$NetBSD: compress.1,v 1.5 1995/03/26 09:44:34 glass Exp $
3.\"
4.\" Copyright (c) 1986, 1990, 1993
5.\"	The Regents of the University of California.  All rights reserved.
6.\"
7.\" This code is derived from software contributed to Berkeley by
8.\" James A. Woods, derived from original work by Spencer Thomas
9.\" and Joseph Orost.
10.\"
11.\" Redistribution and use in source and binary forms, with or without
12.\" modification, are permitted provided that the following conditions
13.\" are met:
14.\" 1. Redistributions of source code must retain the above copyright
15.\"    notice, this list of conditions and the following disclaimer.
16.\" 2. Redistributions in binary form must reproduce the above copyright
17.\"    notice, this list of conditions and the following disclaimer in the
18.\"    documentation and/or other materials provided with the distribution.
19.\" 3. Neither the name of the University nor the names of its contributors
20.\"    may be used to endorse or promote products derived from this software
21.\"    without specific prior written permission.
22.\"
23.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33.\" SUCH DAMAGE.
34.\"
35.\"     @(#)compress.1	8.2 (Berkeley) 4/18/94
36.\"
37.Dd $Mdocdate: October 22 2022 $
38.Dt COMPRESS 1
39.Os
40.Sh NAME
41.Nm compress ,
42.Nm uncompress ,
43.Nm zcat
44.Nd compress and expand data (compress mode)
45.Sh SYNOPSIS
46.Nm compress
47.Op Fl 123456789cdfghlNnOqrtv
48.Op Fl b Ar bits
49.Op Fl o Ar filename
50.Op Fl S Ar suffix
51.Op Ar
52.Nm uncompress
53.Op Fl cfhlNnqrtv
54.Op Fl o Ar filename
55.Op Ar
56.Nm zcat
57.Op Fl fghqr
58.Op Ar
59.Sh DESCRIPTION
60The
61.Nm
62utility
63reduces the size of the named files using adaptive Lempel-Ziv coding,
64in compress mode.
65If invoked as
66.Nm compress Fl g ,
67the deflate mode of compression is chosen;
68see
69.Xr gzip 1
70for more information.
71Each file is renamed to the same name plus the extension
72.Dq .Z .
73As many of the modification time, access time, file flags, file mode,
74user ID, and group ID as allowed by permissions are retained in the
75new file.
76If compression would not reduce the size of a file,
77the file is ignored (unless
78.Fl f
79is used).
80.Pp
81The
82.Nm uncompress
83utility restores compressed files to their original form, renaming the
84files by removing the extension (or by using the stored name if the
85.Fl N
86flag is specified).
87It has the ability to restore files compressed by
88.Nm ,
89.Xr gzip 1 ,
90and
91.Xr zip 1 ,
92recognising the following extensions:
93.Dq .Z ,
94.Dq -Z ,
95.Dq _Z ,
96.Dq .gz ,
97.Dq -gz ,
98.Dq _gz ,
99.Dq .tgz ,
100.Dq -tgz ,
101.Dq _tgz ,
102.Dq .taz ,
103.Dq -taz ,
104and
105.Dq _taz .
106The
107.Fl S
108option can be used to support other file extensions.
109Extensions ending in
110.Dq tgz
111and
112.Dq taz
113are not removed when decompressing, instead they are converted to
114.Dq tar .
115Files in zip format are only supported if they contain a single member
116either compressed with the deflate scheme or stored uncompressed.
117.Pp
118The
119.Nm zcat
120command is equivalent in functionality to
121.Nm uncompress
122.Fl c .
123.Pp
124If renaming the files would cause files to be overwritten and the standard
125input device is a terminal, the user is prompted (on the standard error
126output) for confirmation.
127If prompting is not possible or confirmation is not received, the files
128are not overwritten.
129.Pp
130If no files are specified, the standard input is compressed or uncompressed
131to the standard output.
132If either the input or output files are not regular files, the checks for
133reduction in size and file overwriting are not performed, the input file is
134not removed, and the attributes of the input file are not retained.
135.Pp
136By default, when compressing using the deflate scheme
137.Pf ( Fl g ) ,
138the original file name and time stamp are stored in the compressed file.
139When uncompressing, this information is not used.
140Instead, the uncompressed file inherits the time stamp of the
141compressed version and the uncompressed file name is generated from
142the name of the compressed file as described above.
143These defaults may be overridden by the
144.Fl N
145and
146.Fl n
147flags, described below.
148.Pp
149The options are as follows:
150.Bl -tag -width Ds
151.It Fl 1...9
152Use the deflate scheme, with compression factor of
153.Fl 1
154to
155.Fl 9 .
156Compression factor
157.Fl 1
158is the fastest, but provides a poorer level of compression.
159Compression factor
160.Fl 9
161provides the best level of compression, but is relatively slow.
162The default is
163.Fl 6 .
164This option implies
165.Fl g .
166.It Fl b Ar bits
167Specify the
168.Ar bits
169code limit
170.Pq see below .
171.It Fl c
172Compressed or uncompressed output is written to the standard output.
173No files are modified (force
174.Nm zcat
175mode).
176.It Fl d
177Decompress the source files instead of compressing them (force
178.Nm uncompress
179mode).
180.It Fl f
181Force compression of
182.Ar file ,
183even if it is not actually reduced in size.
184Additionally, files are overwritten without prompting for confirmation.
185If the input data is not in a format recognized by
186.Nm
187and if the option
188.Fl c
189is also given, copy the input data without change
190to the standard output: let
191.Nm zcat
192behave as
193.Xr cat 1 .
194.It Fl g
195Use the deflate scheme, which reportedly provides better compression rates
196(force
197.Xr gzip 1
198mode).
199.It Fl h
200Print a short help message.
201.It Fl l
202List information for the specified compressed files.
203The following information is listed:
204.Bl -tag -width "compression ratio"
205.It compressed size
206Size of the compressed file.
207.It uncompressed size
208Size of the file when uncompressed.
209.It compression ratio
210Ratio of the difference between the compressed and uncompressed
211sizes to the uncompressed size.
212.It uncompressed name
213Name the file will be saved as when uncompressing.
214.El
215.Pp
216If the
217.Fl v
218option is specified, the following additional information is printed:
219.Bl -tag -width "compression method"
220.It compression method
221Name of the method used to compress the file.
222.It crc
22332-bit CRC
224.Pq cyclic redundancy code
225of the uncompressed file.
226.It "time stamp"
227Date and time corresponding to the last data modification time
228(mtime) of the compressed file (if the
229.Fl n
230option is specified, the time stamp stored in the compressed file
231is printed instead).
232.El
233.It Fl N
234When uncompressing or listing, use the time stamp and file name stored
235in the compressed file, if any, for the uncompressed version.
236This information is only available when the deflate scheme
237.Pf ( Fl g )
238is used.
239.It Fl n
240When compressing, do not store the original file name and time stamp
241in the header of the compressed file.
242.It Fl O
243Use compress mode
244(the default).
245.It Fl o Ar filename
246Set the output file name.
247.It Fl q
248Be quiet: suppress all messages.
249.It Fl r
250Recursive mode:
251.Nm
252will descend into specified directories.
253.It Fl S Ar suffix
254When compressing, use the specified
255.Ar suffix
256as the extension when creating output files.
257When uncompressing, recognize file names with the specified
258.Ar suffix
259as compressed files.
260.It Fl t
261Test the integrity of each file leaving any files intact.
262.It Fl v
263Print the percentage reduction of each file and other information.
264.El
265.Pp
266.Nm
267uses a modified Lempel-Ziv algorithm
268.Pq LZW .
269Common substrings in the file are first replaced by 9-bit codes 257 and up.
270When code 512 is reached, the algorithm switches to 10-bit codes and
271continues to use more bits until the
272limit specified by the
273.Fl b
274flag is reached.
275.Ar bits
276must be between 9 and 16
277.Pq the default is 16 .
278.Pp
279After the
280.Ar bits
281limit is reached,
282.Nm
283periodically checks the compression ratio.
284If it is increasing,
285.Nm
286continues to use the existing code dictionary.
287However, if the compression ratio decreases,
288.Nm
289discards the table of substrings and rebuilds it from scratch.
290This allows the algorithm to adapt to the next
291.Dq block
292of the file.
293.Pp
294The
295.Fl b
296flag is omitted for
297.Nm uncompress
298since the
299.Ar bits
300parameter specified during compression
301is encoded within the output, along with
302a magic number to ensure that neither decompression of random data nor
303recompression of compressed data is attempted.
304.Pp
305The amount of compression obtained depends on the size of the
306input, the number of
307.Ar bits
308per code, and the distribution of common substrings.
309Typically, text such as source code or English is reduced by 50 \- 60% using
310.Nm .
311Compression is generally much better than that achieved by Huffman
312coding (as used in the historical command pack), or adaptive Huffman
313coding (as used in the historical command compact), and takes less
314time to compute.
315.Sh EXIT STATUS
316The
317.Nm compress
318utility exits with one of the following values:
319.Pp
320.Bl -tag -width Ds -offset indent -compact
321.It 0
322Success.
323.It 1
324An error occurred.
325.It 2
326At least one of the specified files was not compressed since
327.Fl f
328was not specified and compression would have resulted in a size
329increase.
330.It \*(Gt2
331An error occurred.
332.El
333.Pp
334.Ex -std uncompress zcat
335.Sh SEE ALSO
336.Xr gzexe 1 ,
337.Xr gzip 1 ,
338.Xr zdiff 1 ,
339.Xr zforce 1 ,
340.Xr zmore 1 ,
341.Xr znew 1 ,
342.Xr compress 3
343.Rs
344.%A Welch, Terry A.
345.%D June, 1984
346.%T "A Technique for High Performance Data Compression"
347.%J "IEEE Computer"
348.%V 17:6
349.%P pp. 8\-19
350.Re
351.Sh STANDARDS
352The
353.Nm ,
354.Nm uncompress ,
355and
356.Nm zcat
357utilities are compliant with the
358X/Open System Interfaces option of the
359.St -p1003.1-2008
360specification.
361.Pp
362The
363.Nm
364flags
365.Op Fl 123456789dghlNnOoqrSt ,
366.Nm uncompress
367flags
368.Op Fl hlNnoqrt ,
369and the
370.Nm zcat
371flags
372.Op Fl fghqr
373are extensions to that specification.
374.Pp
375.St -p1003.1-2008
376specifies a maximum bits limit
377.Pq Fl b
378of 14 to
379.Qq achieve portability to all systems .
380.Sh HISTORY
381The
382.Nm
383command appeared in
384.Bx 4.3 .
385Deflate compression support was added in
386.Ox 2.1 .
387