在Go中,我们如何在没有压缩的情况下将文件添加到zip存档?
对于上下文,我将跟随IBM教程创建一个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 文档确实提供了写入zip存档的一个示例,但所有文件都是压缩的.