无需压缩即可将文件添加到zip存档

 rlu1941950 发布于 2023-01-14 15:58
  • php
  • 在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存档的一个示例,但所有文件都是压缩的.

    撰写答案
    今天,你开发时遇到什么问题呢?
    立即提问
    热门标签
    PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
    Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有