Golang archive/zip生成的zip文件损坏。

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

Golang archive/zip producing corrupt zip files

问题

我用Go语言编写了一个小工具来压缩文件夹。在许多情况下,它似乎工作正常,但偶尔会生成一个在解压缩应用程序中打开时被识别为损坏的zip文件(它们似乎都对此抱怨)。

以下是代码:

const (
    singleFileByteLimit = 107374182400 // 1 GB
    chunkSize           = 1024         // 1 KB
)

// ZipFolder将给定的文件夹压缩为指定名称的zip文件
func ZipFolder(srcFolder string, destFile string) error {
    z := &zipper{
        srcFolder: srcFolder,
        destFile:  destFile,
    }
    return z.zipFolder()
}

// 我们需要一个内部结构体,因为filepath WalkFunc
// 不允许自定义参数。所以我们在这里保存它们,以便
// 它可以访问它们
type zipper struct {
    srcFolder string
    destFile  string
    writer    *zip.Writer
}

// 内部函数用于压缩文件夹
func (z *zipper) zipFolder() error {
    // 创建zip文件
    zipFile, err := os.Create(z.destFile)
    if err != nil {
        return err
    }
    defer zipFile.Close()

    // 创建zip写入器
    z.writer = zip.NewWriter(zipFile)

    // 遍历源文件夹
    err = filepath.Walk(z.srcFolder, z.zipFile)
    if err != nil {
        return nil
    }

    // 关闭zip文件
    err = z.writer.Close()
    if err != nil {
        return err
    }
    return nil
}

// 内部函数用于压缩文件,由filepath.Walk在每个文件上调用
func (z *zipper) zipFile(path string, f os.FileInfo, err error) error {
    // 只压缩文件(目录由其中的文件创建)
    // TODO:允许在没有文件的情况下创建文件夹
    if !f.IsDir() && f.Size() > 0 {
        // 打开文件
        file, err := os.Open(path)
        if err != nil {
            return err
        }
        defer file.Close()

        // 在zip中创建新文件
        fileName := strings.TrimPrefix(path, z.srcFolder+"/")
        w, err := z.writer.Create(fileName)
        if err != nil {
            return err
        }

        // 将文件内容复制到zip写入器中
        err = copyContents(file, w)
        if err != nil {
            return err
        }
    }

    return nil
}

func copyContents(r io.Reader, w io.Writer) error {
    var size int64
    for {
        b := make([]byte, chunkSize)

        // 为了避免zip炸弹,我们限制文件大小
        size += chunkSize
        if size > singleFileByteLimit {
            return errors.New("文件太大,请联系我们寻求帮助")
        }

        // 将块读入内存
        length, err := r.Read(b)
        if err == io.EOF {
            break
        } else if err != nil {
            return err
        }
        // 将块写入zip文件
        _, err = w.Write(b[:length])
        if err != nil {
            return err
        }
    }
    return nil
}

希望这可以帮助到你。

英文:

I wrote a small utility in Go to zip a folder. It seems to work in many cases, but every now and then it produces a zip file that is coming up as corrupt when I open it in an unzip app (they all seem to complain about it).

Here is the code:

const (
singleFileByteLimit = 107374182400 // 1 GB
chunkSize           = 1024         // 1 KB
)
// ZipFolder zips the given folder to the a zip file
// with the given name
func ZipFolder(srcFolder string, destFile string) error {
z := &zipper{
srcFolder: srcFolder,
destFile:  destFile,
}
return z.zipFolder()
}
// We need a struct internally because the filepath WalkFunc
// doesn't allow custom params. So we save them here so it can
// access them
type zipper struct {
srcFolder string
destFile  string
writer    *zip.Writer
}
// internal function to zip a folder
func (z *zipper) zipFolder() error {
// create zip file
zipFile, err := os.Create(z.destFile)
if err != nil {
return err
}
defer zipFile.Close()
// create zip writer
z.writer = zip.NewWriter(zipFile)
// traverse the source folder
err = filepath.Walk(z.srcFolder, z.zipFile)
if err != nil {
return nil
}
// close the zip file
err = z.writer.Close()
if err != nil {
return err
}
return nil
}
// internal function to zip a file, called by filepath.Walk on each file
func (z *zipper) zipFile(path string, f os.FileInfo, err error) error {
// only zip files (directories are created by the files inside of them)
// TODO allow creating folder when no files are inside
if !f.IsDir() && f.Size() > 0 {
// open file
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// create new file in zip
fileName := strings.TrimPrefix(path, z.srcFolder+"/")
w, err := z.writer.Create(fileName)
if err != nil {
return err
}
// copy contents of the file to the zip writer
err = copyContents(file, w)
if err != nil {
return err
}
}
return nil
}
func copyContents(r io.Reader, w io.Writer) error {
var size int64
for {
b := make([]byte, chunkSize)
// we limit the size to avoid zip bombs
size += chunkSize
if size > singleFileByteLimit {
return errors.New("file too large, please contact us for assitance")
}
// read chunk into memory
length, err := r.Read(b)
if err == io.EOF {
break
} else if err != nil {
return err
}
// write chunk to zip file
_, err = w.Write(b[:length])
if err != nil {
return err
}
}
return nil
}

答案1

得分: 3

阅读了你的代码后,我修复了一些看起来不正确的地方。请尝试以下代码:

const (
    singleFileByteLimit = 107374182400 // 1 GB
    chunkSize           = 4096         // 4 KB
)

func copyContents(r io.Reader, w io.Writer) error {
    var size int64
    b := make([]byte, chunkSize)
    for {
        // 为了避免压缩炸弹,我们限制文件大小
        size += chunkSize
        if size > singleFileByteLimit {
            return errors.New("文件太大,请联系我们寻求帮助")
        }
        // 将块读入内存
        length, err := r.Read(b[:cap(b)])
        if err != nil {
            if err != io.EOF {
                return err
            }
            if length == 0 {
                break
            }
        }
        // 将块写入 zip 文件
        _, err = w.Write(b[:length])
        if err != nil {
            return err
        }
    }
    return nil
}

// 我们需要一个内部结构体,因为 filepath.WalkFunc 不允许自定义参数。所以我们在这里保存它们以便访问
type zipper struct {
    srcFolder string
    destFile  string
    writer    *zip.Writer
}

// 内部函数,用于在每个文件上调用 filepath.Walk 以进行压缩
func (z *zipper) zipFile(path string, f os.FileInfo, err error) error {
    if err != nil {
        return err
    }
    // 只压缩文件(目录由其中的文件创建)
    // TODO:允许在没有文件的情况下创建文件夹
    if !f.Mode().IsRegular() || f.Size() == 0 {
        return nil
    }
    // 打开文件
    file, err := os.Open(path)
    if err != nil {
        return err
    }
    defer file.Close()
    // 在 zip 中创建新文件
    fileName := strings.TrimPrefix(path, z.srcFolder+"/")
    w, err := z.writer.Create(fileName)
    if err != nil {
        return err
    }
    // 将文件内容复制到 zip writer
    err = copyContents(file, w)
    if err != nil {
        return err
    }
    return nil
}

// 内部函数,用于压缩文件夹
func (z *zipper) zipFolder() error {
    // 创建 zip 文件
    zipFile, err := os.Create(z.destFile)
    if err != nil {
        return err
    }
    defer zipFile.Close()
    // 创建 zip writer
    z.writer = zip.NewWriter(zipFile)
    err = filepath.Walk(z.srcFolder, z.zipFile)
    if err != nil {
        return nil
    }
    // 关闭 zip 文件
    err = z.writer.Close()
    if err != nil {
        return err
    }
    return nil
}

// ZipFolder 将给定的文件夹压缩为指定名称的 zip 文件
func ZipFolder(srcFolder string, destFile string) error {
    z := &zipper{
        srcFolder: srcFolder,
        destFile:  destFile,
    }
    return z.zipFolder()
}

这段代码实现了将指定文件夹压缩为 zip 文件的功能。你可以调用 ZipFolder 函数,并传入源文件夹路径和目标 zip 文件路径来实现压缩。

英文:

Reading through your code, I fixed things that didn't look right. Try the following:

const (
singleFileByteLimit = 107374182400 // 1 GB
chunkSize           = 4096         // 4 KB
)
func copyContents(r io.Reader, w io.Writer) error {
var size int64
b := make([]byte, chunkSize)
for {
// we limit the size to avoid zip bombs
size += chunkSize
if size > singleFileByteLimit {
return errors.New("file too large, please contact us for assistance")
}
// read chunk into memory
length, err := r.Read(b[:cap(b)])
if err != nil {
if err != io.EOF {
return err
}
if length == 0 {
break
}
}
// write chunk to zip file
_, err = w.Write(b[:length])
if err != nil {
return err
}
}
return nil
}
// We need a struct internally because the filepath WalkFunc
// doesn't allow custom params. So we save them here so it can
// access them
type zipper struct {
srcFolder string
destFile  string
writer    *zip.Writer
}
// internal function to zip a file, called by filepath.Walk on each file
func (z *zipper) zipFile(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
// only zip files (directories are created by the files inside of them)
// TODO allow creating folder when no files are inside
if !f.Mode().IsRegular() || f.Size() == 0 {
return nil
}
// open file
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// create new file in zip
fileName := strings.TrimPrefix(path, z.srcFolder+"/")
w, err := z.writer.Create(fileName)
if err != nil {
return err
}
// copy contents of the file to the zip writer
err = copyContents(file, w)
if err != nil {
return err
}
return nil
}
// internal function to zip a folder
func (z *zipper) zipFolder() error {
// create zip file
zipFile, err := os.Create(z.destFile)
if err != nil {
return err
}
defer zipFile.Close()
// create zip writer
z.writer = zip.NewWriter(zipFile)
err = filepath.Walk(z.srcFolder, z.zipFile)
if err != nil {
return nil
}
// close the zip file
err = z.writer.Close()
if err != nil {
return err
}
return nil
}
// ZipFolder zips the given folder to the a zip file
// with the given name
func ZipFolder(srcFolder string, destFile string) error {
z := &zipper{
srcFolder: srcFolder,
destFile:  destFile,
}
return z.zipFolder()
}

huangapple
  • 本文由 发表于 2014年7月17日 04:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/24790635.html
匿名

发表评论

匿名网友

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

确定