Golang archive/zip使用管道生成损坏的文件

Golang archive/zip使用管道生成损坏的文件,go,zip,Go,Zip,我试图从MinIO文件存储实例中提取文件,压缩文件,然后将压缩文件推回到MinIO。我已经让它大部分工作正常,但由于某种原因,存储中的zip文件已损坏,例如文件testzip。zip显示它是一个zip文件,但解压缩不起作用: Archive: somerandomname.zip End-of-central-directory signature not found. Either this file is not a zipfile, or it constitutes one

我试图从MinIO文件存储实例中提取文件,压缩文件,然后将压缩文件推回到MinIO。我已经让它大部分工作正常,但由于某种原因,存储中的zip文件已损坏,例如
文件testzip。zip
显示它是一个zip文件,但解压缩不起作用:

Archive:  somerandomname.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
unzip:  cannot find zipfile directory in one of somerandomname.zip or
    somerandomname.zip.zip, and cannot find somerandomname.zip.ZIP, period.
这是代码

func testThis() error {
pipeReader, pipeWriter := io.Pipe()

zipWriter := zip.NewWriter(pipeWriter)
wg := sync.WaitGroup{}
wg.Add(2)

go func() {

    defer wg.Done()
    defer zipWriter.Close()
    defer pipeWriter.Close()

    header := &zip.FileHeader{
        Name:         "testzip.zip",
        Method:       zip.Deflate,
        ModifiedTime: uint16(time.Now().UnixNano()),
        ModifiedDate: uint16(time.Now().UnixNano()),
    }

    fw, err := zipWriter.CreateHeader(header)


    if err != nil {
        fmt.Println("err")
    }

    object, err := minioClient.GetObject("test", "somekey", minio.GetObjectOptions{})
    if err != nil {
        fmt.Println("err", err)
    }

    if _, err := io.Copy(fw, object); err != nil {
        fmt.Println(err)
    }

}()

go func() {
    defer wg.Done()

    if _, err := io.Copy(os.Stdout, pipeReader); err != nil {
        fmt.Println("err", err)
    }

    _, err := minioClient.PutObject("test", "testzip.zip", pipeReader, -1, minio.PutObjectOptions{})
    if err != nil {
        fmt.Println(err)
    }
}()

wg.Wait()

return nil
}

如果您需要任何帮助,请在关闭
pipeWriter
之前关闭
zipWriter
,我们将不胜感激。您的两个
defer()
调用是按该顺序进行的,但延迟调用的发生顺序相反,因此您要在关闭
zipWriter
之前关闭
pipeWriter
。因此,预告片永远也出不去。检查关闭函数中的错误可能是明智的,即使您所能做的只是记录它们:这会捕获到错误,因为预告片写入会失败。如果您将zip文件复制到stdout,则以后无法将其上载到minio(该文件将为空)。使用或。在关闭
pipeWriter
之前,需要先关闭
zipWriter
。您的两个
defer()
调用是按该顺序进行的,但延迟调用的发生顺序相反,因此您要在关闭
zipWriter
之前关闭
pipeWriter
。因此,预告片永远也出不去。检查关闭函数中的错误可能是明智的,即使您所能做的只是记录它们:这会捕获到错误,因为预告片写入会失败。如果您将zip文件复制到stdout,则以后无法将其上载到minio(该文件将为空)。改用or。