Python tar.gz format compression, decompression

Source: Internet
Author: User
Tags gz file

Compression

Code

ImportTarfileImportOSdeftar (fname): t= Tarfile.open (fname +". tar.gz","W:gz")     forRoot, dir, filesinchOs.walk (fname):Printroot, dir, files forFileinchFiles:fullpath=os.path.join (root, file) T.add (FullPath) t.close ()if __name__=="__main__": Tar ("del")

Results

del [' Del2 '] [' del2.txt ']del\del2 [] [' Del3.txt ']

Extract

Import Tarfile Import OS def Untar (fname, dirs):     = Tarfile.open (fname)    =if__name__"__main__ " :    Untar ("del.tar.gz"". ")

Tar and tar.gz file differences

The tar file simply packs the files and the file size does not change; tar.gz is compressing the files.

Compression principle

The binary code of the file is compressed, the adjacent 0,1 code is reduced, for example, 000000, you can turn it into 6 0 60, to reduce the file space.

Since the information processed by the computer is represented in the form of a binary number, the compression software is the The same string in binary information is marked with a special characterTo achieve the purpose of compression. To help you understand file compression, imagine a picture of blue sky and white clouds in your mind. For thousands of monotonous, repetitive blue pixels, instead of a long string of colors that defines "blue, blue, blue ...", it's better to tell the computer: "Storing 1117 blue dots from this location" is simple and saves storage space. This is an example of a very simple image compression. In fact, all computer files are in the final analysis of "1" and "0" in the form of storage, and the blue image point, as long as the reasonable mathematical formula, the size of the file can be greatly compressed to achieve "data lossless dense" effect. In general, compression can be divided into two kinds of lossy and lossless compression. It's a good idea to ignore the individual data if it's not going to make too much difference, which is lossy compression. Lossy compression is widely used in animation, sound, and image files, typical of which is the DVD file format mpeg, the music file format mp3, and the image file format jpg. But in more cases the compressed data must be accurate, people have designed lossless compression format, such as common zip, rar and so on. Compression software (compression software) is naturally the use of compression principle of compression data tools, compressed after the resulting file is called a compressed package (archive), the volume is only one of the original and even smaller. Of course, the compressed package is already a different file format, if you want to use the data, the first need to use compression software to restore the data, this process is called decompression. Common compression software has WinZip, WinRAR and so on.

Python tar.gz format compression, decompression

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.