dpi96151 2014-07-16 20:59
浏览 105
已采纳

Golang存档/ zip产生损坏的zip文件

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条回答 默认 最新

  • dongling2038 2014-07-16 23:54
    关注

    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()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?