如何在Python中压缩大文件?

我遇到的问题是存储文件的名称.存储的文件未使用原始/未压缩文件名命名.而是使用存档名称(附加的“.gz”扩展名)命名存储的文件.

预期结果:
file.txt.gz {存档名称}
…. file.txt {存储文件名}

实际结果:
file.txt.gz {存档名称}
…. file.txt.gz {存储文件名}

阅读gzip文档(https://docs.python.org/2.7/library/gzip.html)示例代码:

import gzip
import shutil
with open('file.txt', 'rb') as f_in, gzip.open('file.txt.gz', 'wb') as f_out:
    shutil.copyfileobj(f_in, f_out)

如何获取存档以存储名为“file.txt”而不是“file.txt.gz”的文件?

解决方法:

你必须使用gzip.GzipFile();简写gzip.open()不会做你想要的.

the doc号:

When fileobj is not None, the filename argument is only used to be included in the gzip file header, which may include the original filename of the uncompressed file. 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.

试试这个:

import gzip
import shutil
with open('file.txt', 'rb') as f_in:
    with open('file.txt.gz', 'wb') as f_out:
        with gzip.GzipFile('file.txt', 'wb', fileobj=f_out) as f_out:
            shutil.copyfileobj(f_in, f_out)
上一篇:是否有任何当前的C/C++库充满了众所周知的压缩算法?


下一篇:c# – 在客户端上使用lz-string.js编码的.NET服务器上压缩/解压缩字符串