Golang archive-zip.File类(方法)实例源码

下面列出了Golang archive-zip.File 类(方法)源码代码实例,从而了解它的用法。

作者:metal3    项目:vyma   
func parseStyles(f *zip.File) {

	s, err := f.Open()
	if err != nil {
		panic(err)
	}

	x := XMapStyle{}
	content, err := ioutil.ReadAll(s)
	if err != nil {
		panic(err)
	}
	xml.Unmarshal(content, &x)

	for _, s := range x.Styles {

		mdmark := []string{""}
		for _, p := range s.Properties {
			if p.Decoration == "underline" {
				mdmark = append(mdmark, "u")
			}
			if p.Style == "italic" {
				mdmark = append(mdmark, "em")
			}
			if p.Weight == "bold" {
				mdmark = append(mdmark, "strong")
			}

			STYLES[s.Id] = append(STYLES[s.Id], mdmark...)
		}

	}
	defer s.Close()
}

作者:Richardph    项目:nom   
// readWorkbookRelationsFromZipFile is an internal helper function to
// extract a map of relationship ID strings to the name of the
// worksheet.xml file they refer to.  The resulting map can be used to
// reliably derefence the worksheets in the XLSX file.
func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (WorkBookRels, error) {
	var sheetXMLMap WorkBookRels
	var wbRelationships *xlsxWorkbookRels
	var rc io.ReadCloser
	var decoder *xml.Decoder
	var err error

	rc, err = workbookRels.Open()
	if err != nil {
		return nil, err
	}
	decoder = xml.NewDecoder(rc)
	wbRelationships = new(xlsxWorkbookRels)
	err = decoder.Decode(wbRelationships)
	if err != nil {
		return nil, err
	}
	sheetXMLMap = make(WorkBookRels)
	for _, rel := range wbRelationships.Relationships {
		if strings.HasSuffix(rel.Target, ".xml") && rel.Type == "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" {
			_, filename := path.Split(rel.Target)
			sheetXMLMap[rel.Id] = strings.Replace(filename, ".xml", "", 1)
		}
	}
	return sheetXMLMap, nil
}

作者:LuxunForeve    项目:Leftwoo   
// readWorkbookRelationsFromZipFile is an internal helper function to
// extract a map of relationship ID strings to the name of the
// worksheet.xml file they refer to.  The resulting map can be used to
// reliably derefence the worksheets in the XLSX file.
func readWorkbookRelationsFromZipFile(workbookRels *zip.File) (map[string]string, error) {
	var sheetXMLMap map[string]string
	var wbRelationships *xlsxWorkbookRels
	var rc io.ReadCloser
	var decoder *xml.Decoder
	var err error

	rc, err = workbookRels.Open()
	if err != nil {
		return nil, err
	}
	decoder = xml.NewDecoder(rc)
	wbRelationships = new(xlsxWorkbookRels)
	err = decoder.Decode(wbRelationships)
	if err != nil {
		return nil, err
	}
	sheetXMLMap = make(map[string]string)
	for _, rel := range wbRelationships.Relationships {
		if strings.HasSuffix(rel.Target, ".xml") && strings.HasPrefix(rel.Target, "worksheets/") {
			sheetXMLMap[rel.Id] = strings.Replace(rel.Target[len("worksheets/"):], ".xml", "", 1)
		}
	}
	return sheetXMLMap, nil
}

作者:RainYang092    项目:gohttpserve   
func parseIpaIcon(path string) (data []byte, err error) {
	iconPattern := regexp.MustCompile(`(?i)^Payload/[^/]*/icon\.png$`)
	r, err := zip.OpenReader(path)
	if err != nil {
		return
	}
	defer r.Close()

	var zfile *zip.File
	for _, file := range r.File {
		if iconPattern.MatchString(file.Name) {
			zfile = file
			break
		}
	}
	if zfile == nil {
		err = errors.New("icon.png file not found")
		return
	}
	plreader, err := zfile.Open()
	if err != nil {
		return
	}
	defer plreader.Close()
	return ioutil.ReadAll(plreader)
}

作者:remigijus    项目:xls   
// readSheetsFromZipFile is an internal helper function that loops
// over the Worksheets defined in the XSLXWorkbook and loads them into
// Sheet objects stored in the Sheets slice of a xlsx.File struct.
func readSheetsFromZipFile(f *zip.File, file *File) ([]*Sheet, error) {
	var workbook *XLSXWorkbook
	var error error
	var rc io.ReadCloser
	workbook = new(XLSXWorkbook)
	rc, error = f.Open()
	if error != nil {
		return nil, error
	}
	error = xml.Unmarshal(rc, workbook)
	if error != nil {
		return nil, error
	}
	sheets := make([]*Sheet, len(workbook.Sheets.Sheet))
	for i, rawsheet := range workbook.Sheets.Sheet {
		worksheet, error := getWorksheetFromSheet(rawsheet, file.worksheets) //
		if error != nil {
			return nil, error
		}
		sheet := new(Sheet)
		sheet.Rows = readRowsFromSheet(worksheet, file.referenceTable)
		sheets[i] = sheet
	}
	return sheets, nil
}

作者:jingwen    项目:g   
func unzipFile(file *zip.File, to string) (exec string, err error) {
	frc, err := file.Open()
	if err != nil {
		err = fmt.Errorf("Can't open zip entry %s when reading: %s", file.Name, err)
		return
	}
	defer frc.Close()

	dest := filepath.Join(to, filepath.Base(file.Name))
	f, err := os.Create(dest)
	if err != nil {
		return
	}
	defer f.Close()

	copied, err := io.Copy(f, frc)
	if err != nil {
		return
	}

	if uint32(copied) != file.UncompressedSize {
		err = fmt.Errorf("Zip entry %s is corrupted", file.Name)
		return
	}

	exec = f.Name()

	return
}

作者:xia0pin    项目:maldif   
func extractFile(f *zip.File, dest, md5 string) error {
	rc, err := f.Open()
	if err != nil {
		return err
	}
	defer func() {
		if err := rc.Close(); err != nil {
			panic(err)
		}
	}()

	path := filepath.Join(dest, md5+".dex")
	fout, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
	if err != nil {
		return err
	}
	defer func() {
		if err := fout.Close(); err != nil {
			panic(err)
		}
	}()

	_, err = io.Copy(fout, rc)
	if err != nil {
		return err
	}

	return nil
}

作者:Varjelu    项目:archivis   
func (z *unzipper) unzip(f *zip.File) error {
	if f.FileInfo().IsDir() {
		return nil
	}

	fName := filepath.Join(z.dst, f.Name)
	dir, _ := filepath.Split(fName)

	if err := os.MkdirAll(dir, perm); err != nil && os.IsNotExist(err) {
		return err
	}

	r, err := f.Open()
	if err != nil {
		return err
	}
	defer r.Close()

	w, err := os.Create(filepath.Join(z.dst, f.Name))
	if err != nil {
		return err
	}
	defer w.Close()

	if _, err := io.CopyBuffer(w, r, z.buffer); err != nil {
		w.Close()
		return err
	}

	if err := r.Close(); err != nil {
		return err
	}

	return w.Close()
}

作者:ysoht    项目:gopl-e   
func copyFile(dst string, f *zip.File) (err error) {
	path := filepath.Join(dst, f.Name)
	if err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm); err != nil {
		return fmt.Errorf("mkdir failure:%s", err)
	}

	rc, err := f.Open()
	if err != nil {
		return fmt.Errorf("open failure:%s", err)
	}

	defer rc.Close()

	if !f.FileInfo().IsDir() {
		copied, err := os.Create(path)
		if err != nil {
			return err
		}
		defer copied.Close()

		if _, err := io.Copy(copied, rc); err != nil {
			return fmt.Errorf("copy failure:%s", err)
		}
	}

	return nil
}

作者:pierrr    项目:archivefil   
func unarchiveFile(zipFile *zip_impl.File, outFilePath string, progress ProgressFunc) error {
	if zipFile.FileInfo().IsDir() {
		return nil
	}

	if progress != nil {
		progress(zipFile.Name)
	}

	zipFileReader, err := zipFile.Open()
	if err != nil {
		return err
	}
	defer func() {
		_ = zipFileReader.Close()
	}()

	filePath := filepath.Join(outFilePath, filepath.Join(strings.Split(zipFile.Name, "/")...))

	err = os.MkdirAll(filepath.Dir(filePath), os.FileMode(0755))
	if err != nil {
		return err
	}

	file, err := os.Create(filePath)
	if err != nil {
		return err
	}
	defer func() {
		_ = file.Close()
	}()

	_, err = io.Copy(file, zipFileReader)
	return err
}

作者:duluma    项目:webdrive   
// Writes the contents of a single file contained in webdriver.xpi
func (s *Firefox) writeExtensionFile(f *zip.File) (err error) {

	// seperated this into a function so I could use defer, etc.
	var file io.ReadCloser

	if file, err = f.Open(); err == nil {
		defer file.Close()

		targetFileSpec := filepath.Join(s.Extension.TargetPath, f.Name)
		if f.FileInfo().IsDir() {
			err = os.MkdirAll(targetFileSpec, s.DirPermissions)
		} else {

			var targetFile *os.File
			if targetFile, err = os.OpenFile(targetFileSpec,
				os.O_WRONLY|os.O_CREATE,
				s.FilePermissions); err == nil {

				defer targetFile.Close()
				_, err = io.Copy(targetFile, file)

			}

		}

	}

	return err
}

作者:spex    项目:zipf   
// createTempFile creates a temporary file with the contents of the
// zip file. Used to implement io.Seeker interface.
func createTempFile(f *zip.File) (*os.File, error) {
	reader, err := f.Open()
	if err != nil {
		return nil, err
	}
	defer reader.Close()

	tempFile, err := ioutil.TempFile("", "zipfs")
	if err != nil {
		return nil, err
	}

	_, err = io.Copy(tempFile, reader)
	if err != nil {
		tempFile.Close()
		os.Remove(tempFile.Name())
		return nil, err
	}
	_, err = tempFile.Seek(0, os.SEEK_SET)
	if err != nil {
		tempFile.Close()
		os.Remove(tempFile.Name())
		return nil, err
	}

	return tempFile, nil
}

作者:rrudduc    项目:golang-stuf   
func openFile(file *zip.File) (fs.File, error) {
	rc, err := file.Open()
	if err != nil {
		return nil, err
	}
	return &zipfile{file, rc}, nil
}

作者:Joffco    项目:silve   
func extractZipItem(f *zip.File, dest string) error {
	rc, err := f.Open()
	if err != nil {
		return err
	}
	defer rc.Close()

	path := filepath.Join(dest, f.Name)
	if f.FileInfo().IsDir() {
		os.MkdirAll(path, f.Mode())
	} else {
		f, err := os.OpenFile(
			path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
		if err != nil {
			return err
		}
		defer f.Close()

		_, err = io.Copy(f, rc)
		if err != nil {
			return err
		}
	}
	return nil
}

作者:JustinJud    项目:golang-n   
func (p Pkg) copyBin(bin *zip.File, nw io.Reader, binName string, destDir string) error {
	r, err := bin.Open()
	if err != nil {
		return err
	}
	defer r.Close()

	filename := filepath.Join(destDir, binName)
	w, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
	if err != nil {
		return err
	}
	defer w.Close()

	// Copy nw binary
	_, err = io.Copy(w, r)
	if err != nil {
		return err
	}

	// Copy nw
	_, err = io.Copy(w, nw)
	if err != nil {
		return err
	}

	return nil
}

作者:fithisu    项目:ipromotion-remote-rendere   
func cloneZipItem(f *zip.File, dest string) error {
	// Create full directory path
	path := filepath.Join(dest, f.Name)
	fmt.Println("Creating", path)
	err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm)
	if err != nil {
		return err
	}

	// Clone if item is a file
	rc, err := f.Open()
	if err != nil {
		return err
	}
	if !f.FileInfo().IsDir() {
		// Use os.Create() since Zip don't store file permissions.
		fileCopy, err := os.Create(path)
		if err != nil {
			return err
		}
		_, err = io.Copy(fileCopy, rc)
		fileCopy.Close()
		if err != nil {
			return err
		}
	}
	rc.Close()
	return nil
}

作者:Gallardo1    项目:ebookcatalo   
func createFile(filename string, zipFile *zip.File, flag int) error {
	if filename == "" {
		return nil
	}
	var writer *os.File
	var err error
	if flag == 0 {
		writer, err = os.Create("files/books/" + filename)
	} else {
		i++
		writer, err = os.Create("files/books/pictures/" + strconv.Itoa(i) + filename)
	}
	if err != nil {
		return err
	}
	defer writer.Close()
	reader, err := zipFile.Open()
	if err != nil {
		return err
	}
	defer reader.Close()
	if _, err = io.Copy(writer, reader); err != nil {
		return err
	}
	return nil
}

作者:LyudvaDmitr    项目:ebookcatalo   
func findAndRead(file string, r *zip.ReadCloser) ([]byte, error) {
	var cont *zip.File
	//Searching for file
	for _, f := range r.File {
		if f.FileHeader.Name == file {
			cont = f
			break
		}
	}
	if cont == nil {
		return nil, errors.New(file + " not found")
	}
	//Opening and reading file
	reader, err := cont.Open()
	if err != nil {
		return nil, err
	}
	data, err := ioutil.ReadAll(reader)
	if err != nil {
		return nil, err
	}
	if reader.Close() != nil {
		return nil, err
	}
	return data, nil
}

作者:rflorian    项目:gandal   
func CopyZipFile(f *zip.File, d, p string) error {
	if p != "" {
		dirname := path.Dir(p)
		if dirname != "." {
			err := os.MkdirAll(path.Join(d, dirname), 0755)
			if err != nil {
				return err
			}
		}
		rc, err := f.Open()
		defer rc.Close()
		if err != nil {
			return err
		}
		path := path.Join(d, p)
		stat, err := os.Stat(path)
		if err != nil || !stat.IsDir() {
			file, err := fs.Filesystem().OpenFile(path, os.O_WRONLY|os.O_CREATE, 0755)
			if err != nil {
				return err
			}
			defer file.Close()
			_, err = io.Copy(file, rc)
			if err != nil {
				return err
			}
		}
	}
	return nil
}

作者:julien-    项目:epubg   
func TestOpenFileId(t *testing.T) {
	f, _ := Open(bookPath)
	defer f.Close()

	html, err := f.OpenFileId(fileId)
	if err != nil {
		t.Errorf("OpenFileId(%v) return an error: %v", fileId, err)
		return
	}
	defer html.Close()

	zipFile, _ := zip.OpenReader(bookPath)
	defer zipFile.Close()
	var file *zip.File
	for _, file = range zipFile.Reader.File {
		if file.Name == htmlPath {
			break
		}
	}
	zipHTML, _ := file.Open()

	buff1, err := ioutil.ReadAll(html)
	if err != nil {
		t.Errorf("Error reading the opened file: %v", err)
		return
	}
	buff2, _ := ioutil.ReadAll(zipHTML)
	if !bytes.Equal(buff1, buff2) {
		t.Errorf("The files on zip and OpenFile are not equal")
		return
	}
}


问题


面经


文章

微信
公众号

扫码关注公众号