From e902d0a1560cba5eac92a1e5ebe435f6f823e783 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Wed, 25 Nov 2020 15:50:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86zipfiles.go?= =?UTF-8?q?=E4=B8=ADZipFiles=E5=87=BD=E6=95=B0=E5=8F=AF=E8=83=BD=E4=BC=9A?= =?UTF-8?q?=E9=80=A0=E6=88=90=E7=9A=84goroutine=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/utils/zipfiles.go | 63 ++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/server/utils/zipfiles.go b/server/utils/zipfiles.go index 1242cbfb..665d6b0f 100644 --- a/server/utils/zipfiles.go +++ b/server/utils/zipfiles.go @@ -20,38 +20,43 @@ func ZipFiles(filename string, files []string, oldform, newform string) error { // 把files添加到zip中 for _, file := range files { - - zipfile, err := os.Open(file) - if err != nil { - return err - } - defer zipfile.Close() - - // 获取file的基础信息 - info, err := zipfile.Stat() - if err != nil { - return err - } - - header, err := zip.FileInfoHeader(info) + err = func(file string) error { + zipFile, err := os.Open(file) + if err != nil { + return err + } + defer zipFile.Close() + + // 获取file的基础信息 + info, err := zipFile.Stat() + if err != nil { + return err + } + + header, err := zip.FileInfoHeader(info) + if err != nil { + return err + } + + // 使用上面的FileInforHeader() 就可以把文件保存的路径替换成我们自己想要的了,如下面 + header.Name = strings.Replace(file, oldform, newform, -1) + + // 优化压缩 + // 更多参考see http://golang.org/pkg/archive/zip/#pkg-constants + header.Method = zip.Deflate + + writer, err := zipWriter.CreateHeader(header) + if err != nil { + return err + } + if _, err = io.Copy(writer, zipFile); err != nil { + return err + } + return nil + }(file) if err != nil { return err } - - // 使用上面的FileInforHeader() 就可以把文件保存的路径替换成我们自己想要的了,如下面 - header.Name = strings.Replace(file, oldform, newform, -1) - - // 优化压缩 - // 更多参考see http://golang.org/pkg/archive/zip/#pkg-constants - header.Method = zip.Deflate - - writer, err := zipWriter.CreateHeader(header) - if err != nil { - return err - } - if _, err = io.Copy(writer, zipfile); err != nil { - return err - } } return nil } From e1dd5eb58e3113ba73fc6f4c6896fc2b946a2465 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Wed, 25 Nov 2020 16:24:20 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=9F=BA=E7=A1=80?= =?UTF-8?q?=E5=BA=93=E7=9A=84=E4=B8=80=E4=BA=9B=E9=94=99=E8=AF=AF=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/exa_simple_uploader.go | 9 ++++++--- server/utils/breakpoint_continue.go | 10 +++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/server/service/exa_simple_uploader.go b/server/service/exa_simple_uploader.go index 25777c3d..0d4e71df 100644 --- a/server/service/exa_simple_uploader.go +++ b/server/service/exa_simple_uploader.go @@ -36,7 +36,12 @@ func MergeFileMd5(md5 string, fileName string) (err error) { rd, err := ioutil.ReadDir(dir) _ = os.MkdirAll(finishDir, os.ModePerm) //创建目标文件 - fd, _ := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) + fd, err := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) + if err != nil { + return + } + //关闭文件 + defer fd.Close() //将切片文件按照顺序写入 for k := range rd { content, _ := ioutil.ReadFile(dir + "/" + fileName + strconv.Itoa(k+1)) @@ -45,8 +50,6 @@ func MergeFileMd5(md5 string, fileName string) (err error) { _ = os.Remove(finishDir + fileName) } } - //关闭文件 - defer fd.Close() if err != nil { return err diff --git a/server/utils/breakpoint_continue.go b/server/utils/breakpoint_continue.go index 05fb99cb..47921399 100644 --- a/server/utils/breakpoint_continue.go +++ b/server/utils/breakpoint_continue.go @@ -37,7 +37,6 @@ func CheckMd5(content []byte, chunkMd5 string) (CanUpload bool) { func makeFileContent(content []byte, fileName string, FileDir string, contentNumber int) (error, string) { path := FileDir + fileName + "_" + strconv.Itoa(contentNumber) f, err := os.Create(path) - defer f.Close() if err != nil { return err, path } else { @@ -46,6 +45,7 @@ func makeFileContent(content []byte, fileName string, FileDir string, contentNum return err, path } } + defer f.Close() return nil, path } @@ -55,7 +55,11 @@ func MakeFile(fileName string, FileMd5 string) (error, string) { return err, finishDir + fileName } _ = os.MkdirAll(finishDir, os.ModePerm) - fd, _ := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) + fd, err := os.OpenFile(finishDir+fileName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644) + if err != nil { + return err, finishDir + fileName + } + defer fd.Close() for k := range rd { content, _ := ioutil.ReadFile(breakpointDir + FileMd5 + "/" + fileName + "_" + strconv.Itoa(k)) _, err = fd.Write(content) @@ -64,7 +68,7 @@ func MakeFile(fileName string, FileMd5 string) (error, string) { return err, finishDir + fileName } } - defer fd.Close() + return nil, finishDir + fileName }