本文整理汇总了Golang中archive/zip.File类的典型用法代码### 示例。如果您正苦于以下问题:Golang File类的具体用法?Golang File怎么用?Golang File使用的例子?那么恭喜您, 这里精选的类代码### 示例或许可以为您提供帮助。

在下文中一共展示了File类的20个代码### 示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码### 示例。

示例1: parseStyles

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()
}

开发者ID:metal3d,项目名称:vymad,代码行数:34,代码来源:xmind.go

示例2: readWorkbookRelationsFromZipFile

// 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
}

开发者ID:Richardphp,项目名称:noms,代码行数:30,代码来源:lib.go

示例3: readWorkbookRelationsFromZipFile

// 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
}

开发者ID:LuxunForever,项目名称:Leftwood,代码行数:29,代码来源:lib.go

示例4: parseIpaIcon

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)
}

开发者ID:RainYang0925,项目名称:gohttpserver,代码行数:26,代码来源:ipa.go

示例5: readSheetsFromZipFile

// 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
}

开发者ID:remigijusj,项目名称:xlsx,代码行数:28,代码来源:lib.go

示例6: unzipFile

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
}

开发者ID:jingweno,项目名称:gh,代码行数:29,代码来源:updater.go

示例7: extractFile

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
}

开发者ID:xia0pin9,项目名称:maldiff,代码行数:29,代码来源:prepare.go

示例8: unzip

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()
}

开发者ID:Varjelus,项目名称:archivist,代码行数:35,代码来源:unzipper.go

示例9: copyFile

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
}

开发者ID:ysohta,项目名称:gopl-ex,代码行数:27,代码来源:extractor.go

示例10: unarchiveFile

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
}

开发者ID:pierrre,项目名称:archivefile,代码行数:35,代码来源:zip.go

示例11: writeExtensionFile

// 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
}

开发者ID:dulumao,项目名称:webdriver,代码行数:30,代码来源:firefox_extension.go

示例12: createTempFile

// 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
}

开发者ID:spexp,项目名称:zipfs,代码行数:29,代码来源:file_system.go

示例13: openFile

func openFile(file *zip.File) (fs.File, error) {
    rc, err := file.Open()
    if err != nil {
        return nil, err
    }
    return &zipfile{file, rc}, nil
}

开发者ID:rrudduck,项目名称:golang-stuff,代码行数:7,代码来源:zipfs.go

示例14: extractZipItem

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
}

开发者ID:Joffcom,项目名称:silver,代码行数:25,代码来源:updater.go

示例15: copyBin

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
}

开发者ID:JustinJudd,项目名称:golang-nw,代码行数:28,代码来源:pkg.go

示例16: cloneZipItem

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
}

开发者ID:fithisux,项目名称:ipromotion-remote-renderer,代码行数:29,代码来源:promonode.go

示例17: createFile

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
}

开发者ID:Gallardo13,项目名称:ebookcatalog,代码行数:26,代码来源:ebookcatalog.go

示例18: findAndRead

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
}

开发者ID:LyudvaDmitry,项目名称:ebookcatalog,代码行数:26,代码来源:ebookcatalog.go

示例19: CopyZipFile

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
}

开发者ID:rfloriano,项目名称:gandalf,代码行数:30,代码来源:multipartzip.go

示例20: TestOpenFileId

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
    }
}

开发者ID:julien-c,项目名称:epubgo,代码行数:32,代码来源:epub_test.go

最后编辑: goerone  文档更新时间: 2021-08-23 19:14   作者:goerone