3
在Go中,我们如何在不压缩的情况下将文件添加到zip压缩文件?将文件添加到zip压缩文件而不压缩
对于上下文,我正在跟随IBM tutorial创建一个epub zip文件。它示出了下面的Python代码:
import zipfile, os
def create_archive(path='/path/to/our/epub/directory'):
'''Create the ZIP archive. The mimetype must be the first file in the archive
and it must not be compressed.'''
epub_name = '%s.epub' % os.path.basename(path)
# The EPUB must contain the META-INF and mimetype files at the root, so
# we'll create the archive in the working directory first and move it later
os.chdir(path)
# Open a new zipfile for writing
epub = zipfile.ZipFile(epub_name, 'w')
# Add the mimetype file first and set it to be uncompressed
epub.write(MIMETYPE, compress_type=zipfile.ZIP_STORED)
# For the remaining paths in the EPUB, add all of their files
# using normal ZIP compression
for p in os.listdir('.'):
for f in os.listdir(p):
epub.write(os.path.join(p, f)), compress_type=zipfile.ZIP_DEFLATED)
epub.close()
在这个例子中,文件mimetype
(其仅具有内容application/epub+zip
)不能被压缩。
Go documentation确实提供了写入zip压缩文件的一个示例,但所有文件都被压缩。