Turn http://m.blog.csdn.net/blog/wice110956/26597179#
Here we discuss the following five types of compressed files extracted using Python:
. gz. Tar. tgz. zip. rar
Brief introduction
GZ: That is, gzip, usually only one file can be compressed. Combined with tar, it can be packaged first and then compressed.
Packaging tools under the Tar:linux system, packaged only, not compressed
Tgz: That is tar.gz. The files that are packed first with tar and then compressed with GZ
Zip: Unlike gzip, while using a similar algorithm, you can package and compress multiple files, but compress the files separately, with a compression rate lower than tar.
RAR: Packaging Compressed files, originally used for DOS, based on the Windows operating system. The compression rate is higher than zip, but it is slow and the speed of random access is slow.
For various comparisons between zip and rar, it can be seen that:
Http://www.comicer.com/stronghorse/water/software/ziprar.htm
Gz
Because GZ generally compresses only one file, all work with other packaging tools. For example, you can pack the tar into Xxx.tar first, and then compress to XXX.tar.gz
To extract gz, in fact, is to read a single file, the Python method is as follows:
Import Gzipimport osdef un_gz (file_name): "" " ungz Zip file" "" F_name = File_name.replace (". Gz", "") # Get the name of the file and remove G_file = gzip. Gzipfile (file_name) #创建gzip对象 Open (f_name, "w+"). Write (G_file.read ()) #gzip对象用read () when opened, writes to open () The file that was created. g_file.close () #关闭gzip对象 <span style= "font-family:arial, Helvetica, Sans-serif; "> </span>
Tar
XXX.tar.gz decompression to get Xxx.tar, but also further decompression out.
* Note: tgz and tar.gz are the same format, the old version dos extension up to three characters, so with tgz.
Since there are multiple files here, we first read all the filenames and then unzip them, as follows:
Import Tarfiledef Un_tar (file_name): untar zip file "" " tar = Tarfile.open (file_name) names = Tar.getnames ( if Os.path.isdir (file_name + "_files"): pass else: os.mkdir (file_name + "_files") #由于解压后是许多文件 , pre-established folder with the same name as name in Names: tar.extract (name, file_name + "_files/") Tar.close ()
* Note: The tgz file is the same decompression method as the tar file.
Zip
Similar to tar, read more than one file name and unzip it as follows:
Import Zipfiledef Un_zip (file_name): "" " Unzip zip file" "" zip_file = ZipFile. ZipFile (file_name) if Os.path.isdir (file_name + "_files"): pass else: os.mkdir (file_name + "_ Files ") for names in Zip_file.namelist (): zip_file.extract (names,file_name +" _files/") Zip_ File.close ()
rar
Because RAR is usually used under window, additional Python package rarfile is required.
Available Address: Http://sourceforge.net/projects/rarfile.berlios/files/rarfile-2.4.tar.gz/download
Unzip to the/scripts/directory of the Python installation directory, open the command line in the current window,
Enter Python setup.py install
Installation is complete.
Import Rarfileimport osdef Un_rar (file_name): "" " unrar Zip file" "" rar = rarfile. Rarfile (file_name) if Os.path.isdir (file_name + "_files"): pass else: os.mkdir (file_name + "_files") ) Os.chdir (file_name + "_files"): rar.extractall () rar.close ()
Python Unzip the zip package