将文件添加到ZIP存档中,但不进行压缩。

huangapple go评论91阅读模式
英文:

Add file to zip archive without compression

问题

在Go语言中,我们如何将文件添加到zip归档文件中而不进行压缩?

以下是一个示例代码,展示了如何在Go中创建一个不进行压缩的zip归档文件:

package main

import (
	"archive/zip"
	"io"
	"os"
)

func main() {
	// 创建一个新的zip文件
	zipFile, err := os.Create("example.zip")
	if err != nil {
		panic(err)
	}
	defer zipFile.Close()

	// 创建一个zip.Writer
	zipWriter := zip.NewWriter(zipFile)
	defer zipWriter.Close()

	// 添加文件到zip归档文件中
	file, err := os.Open("file.txt")
	if err != nil {
		panic(err)
	}
	defer file.Close()

	// 创建一个zip文件中的文件
	zipFileInArchive, err := zipWriter.Create("file.txt")
	if err != nil {
		panic(err)
	}

	// 将文件内容复制到zip文件中的文件中
	_, err = io.Copy(zipFileInArchive, file)
	if err != nil {
		panic(err)
	}
}

在这个示例中,我们使用archive/zip包来创建一个zip归档文件,并将文件添加到其中。通过使用zipWriter.Create方法创建一个zip文件中的文件,并将文件内容复制到该文件中,我们可以实现将文件添加到zip归档文件中而不进行压缩。

请注意,这只是一个简单的示例,你可能需要根据你的具体需求进行适当的修改。

英文:

In Go, how do we add a file to a zip archive without compression?

For context, I'm following along with an IBM tutorial to create an epub zip file. It shows the following Python code:

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()

In this example, the file mimetype (which has content application/epub+zip only) must not be compressed.

The Go documentation does provide one example of writing to a zip archive, but all files are compressed.

答案1

得分: 7

有两种方法可以将文件添加到zip.Writer中:Create方法和CreateHeader方法。Create方法只允许您指定文件名,而CreateHeader方法提供了更多的灵活性,包括设置压缩方法的能力。

例如:

w, err := zipwriter.CreateHeader(&zip.FileHeader{
    Name:   filename,
    Method: zip.Store,
})

现在,您可以像Go文档中的示例代码一样向w写入数据,它将以无压缩的方式存储在zip文件中。

英文:

There are two methods to add a file to file to a zip.Writer: the Create method and the CreateHeader. While Create only lets you specify a filename, the CreateHeader method gives more flexibility including the ability to set the compression method.

For example:

w, err := zipwriter.CreateHeader(&zip.FileHeader{
    Name:   filename,
    Method: zip.Store,
})

You can now write data to w the same as in the example code from the Go documentation and it will be stored in the zip file without compression.

huangapple
  • 本文由 发表于 2014年5月18日 07:26:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/23717101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定