github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/Unknwon/cae/zip/read.go (about) 1 // Copyright 2013 Unknown 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package zip 16 17 import ( 18 "archive/zip" 19 "os" 20 "strings" 21 ) 22 23 // OpenFile is the generalized open call; most users will use Open 24 // instead. It opens the named zip file with specified flag 25 // (O_RDONLY etc.) if applicable. If successful, 26 // methods on the returned ZipArchive can be used for I/O. 27 // If there is an error, it will be of type *PathError. 28 func (z *ZipArchive) Open(name string, flag int, perm os.FileMode) error { 29 // Create a new archive if it's specified and not exist. 30 if flag&os.O_CREATE != 0 { 31 f, err := os.Create(name) 32 if err != nil { 33 return err 34 } 35 zw := zip.NewWriter(f) 36 if err = zw.Close(); err != nil { 37 return err 38 } 39 } 40 41 rc, err := zip.OpenReader(name) 42 if err != nil { 43 return err 44 } 45 46 z.ReadCloser = rc 47 z.FileName = name 48 z.Comment = rc.Comment 49 z.NumFiles = len(rc.File) 50 z.Flag = flag 51 z.Permission = perm 52 z.isHasChanged = false 53 54 z.files = make([]*File, z.NumFiles) 55 for i, f := range rc.File { 56 z.files[i] = &File{} 57 z.files[i].FileHeader, err = zip.FileInfoHeader(f.FileInfo()) 58 if err != nil { 59 return err 60 } 61 z.files[i].Name = strings.Replace(f.Name, "\\", "/", -1) 62 if f.FileInfo().IsDir() && !strings.HasSuffix(z.files[i].Name, "/") { 63 z.files[i].Name += "/" 64 } 65 } 66 return nil 67 }