1.\" $OpenBSD: compress.1,v 1.48 2014/03/17 14:23:50 jmc 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: March 17 2014 $ 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 both 88.Nm 89and 90.Xr gzip 1 , 91recognising the following extensions: 92.Dq .Z , 93.Dq -Z , 94.Dq _Z , 95.Dq .gz , 96.Dq -gz , 97.Dq _gz , 98.Dq .tgz , 99.Dq -tgz , 100.Dq _tgz , 101.Dq .taz , 102.Dq -taz , 103and 104.Dq _taz . 105Extensions ending in 106.Dq tgz 107and 108.Dq taz 109are not removed when decompressing, instead they are converted to 110.Dq tar . 111.Pp 112The 113.Nm zcat 114command is equivalent in functionality to 115.Nm uncompress 116.Fl c . 117.Pp 118If renaming the files would cause files to be overwritten and the standard 119input device is a terminal, the user is prompted (on the standard error 120output) for confirmation. 121If prompting is not possible or confirmation is not received, the files 122are not overwritten. 123.Pp 124If no files are specified, the standard input is compressed or uncompressed 125to the standard output. 126If either the input or output files are not regular files, the checks for 127reduction in size and file overwriting are not performed, the input file is 128not removed, and the attributes of the input file are not retained. 129.Pp 130By default, when compressing using the deflate scheme 131.Pf ( Fl g ) , 132the original file name and time stamp are stored in the compressed file. 133When uncompressing, this information is not used. 134Instead, the uncompressed file inherits the time stamp of the 135compressed version and the uncompressed file name is generated from 136the name of the compressed file as described above. 137These defaults may be overridden by the 138.Fl N 139and 140.Fl n 141flags, described below. 142.Pp 143The options are as follows: 144.Bl -tag -width Ds 145.It Fl 1...9 146Use the deflate scheme, with compression factor of 147.Fl 1 148to 149.Fl 9 . 150Compression factor 151.Fl 1 152is the fastest, but provides a poorer level of compression. 153Compression factor 154.Fl 9 155provides the best level of compression, but is relatively slow. 156The default is 157.Fl 6 . 158This option implies 159.Fl g . 160.It Fl b Ar bits 161Specify the 162.Ar bits 163code limit 164.Pq see below . 165.It Fl c 166Compressed or uncompressed output is written to the standard output. 167No files are modified (force 168.Nm zcat 169mode). 170.It Fl d 171Decompress the source files instead of compressing them (force 172.Nm uncompress 173mode). 174.It Fl f 175Force compression of 176.Ar file , 177even if it is not actually reduced in size. 178Additionally, files are overwritten without prompting for confirmation. 179If the input data is not in a format recognized by 180.Nm 181and if the option 182.Fl c 183is also given, copy the input data without change 184to the standard output: let 185.Nm zcat 186behave as 187.Xr cat 1 . 188.It Fl g 189Use the deflate scheme, which reportedly provides better compression rates 190(force 191.Xr gzip 1 192mode). 193.It Fl h 194Print a short help message. 195.It Fl l 196List information for the specified compressed files. 197The following information is listed: 198.Bl -tag -width "compression ratio" 199.It compressed size 200Size of the compressed file. 201.It uncompressed size 202Size of the file when uncompressed. 203.It compression ratio 204Ratio of the difference between the compressed and uncompressed 205sizes to the uncompressed size. 206.It uncompressed name 207Name the file will be saved as when uncompressing. 208.El 209.Pp 210If the 211.Fl v 212option is specified, the following additional information is printed: 213.Bl -tag -width "compression method" 214.It compression method 215Name of the method used to compress the file. 216.It crc 21732-bit CRC 218.Pq cyclic redundancy code 219of the uncompressed file. 220.It "time stamp" 221Date and time corresponding to the last data modification time 222(mtime) of the compressed file (if the 223.Fl n 224option is specified, the time stamp stored in the compressed file 225is printed instead). 226.El 227.It Fl N 228When uncompressing or listing, use the time stamp and file name stored 229in the compressed file, if any, for the uncompressed version. 230This information is only available when the deflate scheme 231.Pf ( Fl g ) 232is used. 233.It Fl n 234When compressing, do not store the original file name and time stamp 235in the header of the compressed file. 236.It Fl O 237Use compress mode 238(the default). 239.It Fl o Ar filename 240Set the output file name. 241.It Fl q 242Be quiet: suppress all messages. 243.It Fl r 244Recursive mode: 245.Nm 246will descend into specified directories. 247.It Fl S Ar suffix 248Set the suffix for compressed files. 249.It Fl t 250Test the integrity of each file leaving any files intact. 251.It Fl v 252Print the percentage reduction of each file and other information. 253.El 254.Pp 255.Nm 256uses a modified Lempel-Ziv algorithm 257.Pq LZW . 258Common substrings in the file are first replaced by 9-bit codes 257 and up. 259When code 512 is reached, the algorithm switches to 10-bit codes and 260continues to use more bits until the 261limit specified by the 262.Fl b 263flag is reached. 264.Ar bits 265must be between 9 and 16 266.Pq the default is 16 . 267.Pp 268After the 269.Ar bits 270limit is reached, 271.Nm 272periodically checks the compression ratio. 273If it is increasing, 274.Nm 275continues to use the existing code dictionary. 276However, if the compression ratio decreases, 277.Nm 278discards the table of substrings and rebuilds it from scratch. 279This allows the algorithm to adapt to the next 280.Dq block 281of the file. 282.Pp 283The 284.Fl b 285flag is omitted for 286.Nm uncompress 287since the 288.Ar bits 289parameter specified during compression 290is encoded within the output, along with 291a magic number to ensure that neither decompression of random data nor 292recompression of compressed data is attempted. 293.Pp 294The amount of compression obtained depends on the size of the 295input, the number of 296.Ar bits 297per code, and the distribution of common substrings. 298Typically, text such as source code or English is reduced by 50 \- 60% using 299.Nm . 300Compression is generally much better than that achieved by Huffman 301coding (as used in the historical command pack), or adaptive Huffman 302coding (as used in the historical command compact), and takes less 303time to compute. 304.Sh EXIT STATUS 305The 306.Nm compress 307utility exits with one of the following values: 308.Pp 309.Bl -tag -width Ds -offset indent -compact 310.It 0 311Success. 312.It 1 313An error occurred. 314.It 2 315At least one of the specified files was not compressed since 316.Fl f 317was not specified and compression would have resulted in a size 318increase. 319.It \*(Gt2 320An error occurred. 321.El 322.Pp 323.Ex -std uncompress zcat 324.Sh SEE ALSO 325.Xr gzexe 1 , 326.Xr gzip 1 , 327.Xr zdiff 1 , 328.Xr zforce 1 , 329.Xr zmore 1 , 330.Xr znew 1 , 331.Xr compress 3 332.Rs 333.%A Welch, Terry A. 334.%D June, 1984 335.%T "A Technique for High Performance Data Compression" 336.%J "IEEE Computer" 337.%V 17:6 338.%P pp. 8\-19 339.Re 340.Sh STANDARDS 341The 342.Nm , 343.Nm uncompress , 344and 345.Nm zcat 346utilities are compliant with the 347X/Open System Interfaces option of the 348.St -p1003.1-2008 349specification. 350.Pp 351The 352.Nm 353flags 354.Op Fl 123456789dghlNnOoqrSt , 355.Nm uncompress 356flags 357.Op Fl hlNnoqrt , 358and the 359.Nm zcat 360flags 361.Op Fl fghqr 362are extensions to that specification. 363.Pp 364.St -p1003.1-2008 365specifies a maximum bits limit 366.Pq Fl b 367of 14 to 368.Qq achieve portability to all systems . 369.Sh HISTORY 370The 371.Nm 372command appeared in 373.Bx 4.3 . 374Deflate compression support was added in 375.Ox 2.1 . 376