How do you gzip a file
Related Articles. Table of Contents. Save Article. Improve Article. Like Article. Next ZIP command in Linux with examples. Recommended Articles. This is useful when the compressed file name was truncated or when the time stamp was not preserved after a file transfer. Compressed files can be restored to their original form using gzip -d or gunzip or zcat. If the original name saved in the compressed file is not suitable for its file system, a new name is constructed from the original one to make it legal.
Z respectively. When compressing, gzip uses the. The detection of the input format is automatic. When using the first two formats, gunzip checks a 32 bit CRC. For pack, gunzip checks the uncompressed length. The standard compress format was not designed to allow consistency checks. However gunzip is sometimes able to detect a bad. Z file. If you get an error when uncompressing a.
Z file, do not assume that the. Z file is correct simply because the standard uncompress does not complain. This generally means that the standard uncompress does not check its input, and happily generates garbage output. Files created by zip can be uncompressed by gzip only if they have a single member compressed with the 'deflation' method.
This feature is only intended to help conversion of tar. To extract zip files with several members, use unzip instead of gunzip. On some systems, zcat may be installed as gzcat to preserve the original link to compress. The amount of compression obtained depends on the size of the input and the distribution of common substrings. Compression is generally much better than that achieved by LZW as used in compress , Huffman coding as used in pack , or adaptive Huffman coding compact.
Compression is always performed, even if the compressed file is slightly larger than the original. The worst case expansion is a few bytes for the gzip file header, plus 5 bytes every 32K block, or an expansion ratio of 0. Note that the actual number of used disk blocks almost never increases. To compress a file called mydocument. If the file name contains spaces, surround it by quotes:.
Some files compress better than others. Other file types such as JPEG images and MP3 audio files do not compress at all well and the file may actually increase in size after running the gzip command against it. JPEG images and MP3 audio files are already compressed and therefore the gzip command simply adds to it rather than compressing it.
The gzip command only attempts to compress regular files and folders. Use the following command to decompress a gzip archive:. To decompress the mydocument. Sometimes a file cannot be compressed. To force the gzip command to do its stuff simply run the following command:. By default when you compress a file using the gzip command you end up with a new file with the extension. To compress the file and keep the original file must specify the -k flag:.
I you run the following command you would end up with a file called mydocument. The whole point of compressing files is about saving disk space or to reduce the size of a file prior to sending it over a network.
The gzip command provides the kind of statistics you require when checking for compression performance. To get the list of statistics run the following command:. The information returned by the above command is as follows:. Compress every file in a folder and its subfolders by using the following command:. This process doesn't create one file called foldername. Instead, it traverses the directory structure and compresses each file in that folder structure. It defaults to the filename of fileobj , if discernible; otherwise, it defaults to the empty string, and in this case the original filename is not included in the header.
The mode argument can be any of 'r' , 'rb' , 'a' , 'ab' , 'w' , 'wb' , 'x' , or 'xb' , depending on whether the file will be read or written. The default is the mode of fileobj if discernible; otherwise, the default is 'rb'. In future Python releases the mode of fileobj will not be used.
It is better to always specify mode for writing. Note that the file is always opened in binary mode. To open a compressed file in text mode, use open or wrap your GzipFile with an io.
The compresslevel argument is an integer from 0 to 9 controlling the level of compression; 1 is fastest and produces the least compression, and 9 is slowest and produces the most compression. The default is 9.
The mtime argument is an optional numeric timestamp to be written to the last modification time field in the stream when compressing.
It should only be provided in compression mode. If omitted or None , the current time is used. See the mtime attribute for more details. This also allows you to pass an io. BytesIO object opened for writing as fileobj , and retrieve the resulting memory buffer using the io.
GzipFile supports the io. BufferedIOBase interface, including iteration and the with statement.
0コメント