MCPcopy Create free account
hub / github.com/MG-RAST/Shock / unZip

Function unZip

shock-server/node/archive/archive.go:154–205  ·  view source on GitHub ↗
(filePath string, unpackDir string)

Source from the content-addressed store, hash-verified

152}
153
154func unZip(filePath string, unpackDir string) (fileList []file.FormFile, err error) {
155 // open file with unzip
156 zipReader, err := zip.OpenReader(filePath)
157 if err != nil {
158 return
159 }
160
161 // extract archive
162 for _, zf := range zipReader.File {
163 // set names
164 path := filepath.Join(unpackDir, zf.Name)
165 baseName := filepath.Base(zf.Name)
166 // skip hidden
167 if strings.HasPrefix(baseName, ".") {
168 continue
169 }
170
171 if zf.FileInfo().IsDir() {
172 // handle directory
173 if merr := os.MkdirAll(path, 0777); merr != nil {
174 logger.Error("err:@node_untar: " + err.Error())
175 }
176 } else {
177 // open output file
178 writer, werr := os.Create(path)
179 if werr != nil {
180 return nil, werr
181 }
182 // open input stream
183 zfh, zerr := zf.Open()
184 if zerr != nil {
185 writer.Close()
186 return nil, zerr
187 }
188 // get md5
189 md5h := md5.New()
190 dst := io.MultiWriter(writer, md5h)
191 // write it
192 _, err = io.Copy(dst, zfh)
193 writer.Close()
194 zfh.Close()
195 if err != nil {
196 return
197 }
198 // add to filelist
199 ffile := file.FormFile{Name: baseName, Path: path, Checksum: make(map[string]string)}
200 ffile.Checksum["md5"] = fmt.Sprintf("%x", md5h.Sum(nil))
201 fileList = append(fileList, ffile)
202 }
203 }
204 return
205}
206
207func ArchiveReader(format string, files []*file.FileInfo) (outReader io.ReadCloser) {
208 pReader, pWriter := io.Pipe()

Callers 1

FilesFromArchiveFunction · 0.85

Calls 3

CreateMethod · 0.65
CloseMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected