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

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

示例1: zipFile

// Deal with file
func zipFile(srcFile string, recPath string, zw *zip.Writer, fi os.FileInfo) {
    if fi.IsDir() {
        // Create zip header
        fh := new(zip.FileHeader)
        fh.Name = recPath + "/"
        fh.UncompressedSize = 0

        _, err := zw.CreateHeader(fh)
        handleError(err)
    } else {
        // Create zip header
        fh := new(zip.FileHeader)
        fh.Name = recPath
        fh.UncompressedSize = uint32(fi.Size())
        fw, err := zw.CreateHeader(fh)
        handleError(err)

        // Read file data
        buf := make([]byte, fi.Size())
        f, err := os.Open(srcFile)
        handleError(err)
        _, err = f.Read(buf)
        handleError(err)

        // Write file data to zip
        _, err = fw.Write(buf)
        handleError(err)
    }
}

开发者ID:seacoastboy,项目名称:go-compresser,代码行数:30,代码来源:go-zip.go

示例2: array2XlsxWriteSharedStrings

func array2XlsxWriteSharedStrings(zw *zip.Writer, data [][]string) (err error) {
    siList := []xlsxSharedStringSi{{""}}
    for _, row := range data {
        for _, v1 := range row {
            if v1 == "" { //ignore blank cell can save space
                continue
            }
            siList = append(siList, xlsxSharedStringSi{v1})
        }
    }
    sst := xlsxSharedStringSst{
        Xmlns:  xmlNs,
        Count:  len(siList),
        SiList: siList,
    }
    thisW, err := zw.Create(sharedStringsFileName)
    _, err = thisW.Write([]byte(xml.Header))
    if err != nil {
        return
    }
    encoder := xml.NewEncoder(thisW)
    err = encoder.Encode(sst)
    if err != nil {
        return
    }
    return
}

开发者ID:keysonZZZ,项目名称:kmg,代码行数:27,代码来源:XlsxWriter.go

示例3: AddContainer

func AddContainer(zippy *zip.Writer) {
    container, err := zippy.Create(path.Join("META-INF", "container.xml"))
    if err != nil {
        log.Fatal(err)
    }
    WriteToFile(container, "<?xml version=\"1.0\"?><container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\"><rootfiles><rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"/></rootfiles></container>")
}

开发者ID:smazga,项目名称:GetFARMSEpub,代码行数:7,代码来源:GetFARMSEpub.go

示例4: AddMimetype

func AddMimetype(zippy *zip.Writer) {
    mimetype, err := zippy.Create("mimetype")
    if err != nil {
        log.Fatal(err)
    }
    WriteToFile(mimetype, "application/epub+zip")
}

开发者ID:smazga,项目名称:GetFARMSEpub,代码行数:7,代码来源:GetFARMSEpub.go

示例5: writeFile2Zip

func writeFile2Zip(zw *zip.Writer, path string, name string) error {
    file, err := os.Open(path + name)
    if err != nil {
        return err
    }
    defer file.Close()

    info, err := file.Stat()
    if err != nil {
        return err
    }

    header, err := zip.FileInfoHeader(info)
    if err != nil {
        return err
    }
    header.Name = name

    writer, err := zw.CreateHeader(header)
    if err != nil {
        return err
    }

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

开发者ID:jsli,项目名称:gtbox,代码行数:26,代码来源:zip.go

示例6: addKeysToArchive

func addKeysToArchive(zipWriter *zip.Writer, newKeyStore *trustmanager.KeyFileStore) error {
    for _, relKeyPath := range newKeyStore.ListFiles() {
        fullKeyPath := filepath.Join(newKeyStore.BaseDir(), relKeyPath)

        fi, err := os.Lstat(fullKeyPath)
        if err != nil {
            return err
        }

        infoHeader, err := zip.FileInfoHeader(fi)
        if err != nil {
            return err
        }

        infoHeader.Name = relKeyPath

        zipFileEntryWriter, err := zipWriter.CreateHeader(infoHeader)
        if err != nil {
            return err
        }

        fileContents, err := ioutil.ReadFile(fullKeyPath)
        if err != nil {
            return err
        }

        if _, err = zipFileEntryWriter.Write(fileContents); err != nil {
            return err
        }
    }

    return nil
}

开发者ID:DaveDaCoda,项目名称:docker,代码行数:33,代码来源:import_export.go

示例7: addKeysToArchive

func addKeysToArchive(zipWriter *zip.Writer, newKeyStore *trustmanager.KeyFileStore, subDir string) error {
    // List all files but no symlinks
    for _, relKeyPath := range newKeyStore.ListFiles(false) {
        fullKeyPath := filepath.Join(newKeyStore.BaseDir(), relKeyPath)

        fi, err := os.Stat(fullKeyPath)
        if err != nil {
            return err
        }

        infoHeader, err := zip.FileInfoHeader(fi)
        if err != nil {
            return err
        }

        infoHeader.Name = filepath.Join(subDir, relKeyPath)
        zipFileEntryWriter, err := zipWriter.CreateHeader(infoHeader)
        if err != nil {
            return err
        }

        fileContents, err := ioutil.ReadFile(fullKeyPath)
        if err != nil {
            return err
        }
        if _, err = zipFileEntryWriter.Write(fileContents); err != nil {
            return err
        }
    }

    return nil
}

开发者ID:ChanderG,项目名称:docker,代码行数:32,代码来源:import_export.go

示例8: Write

// Write the File to io.Writer as xlsx
func (f *File) Write(writer io.Writer) (err error) {
    var parts map[string]string
    var zipWriter *zip.Writer

    parts, err = f.MarshallParts()
    if err != nil {
        return
    }

    zipWriter = zip.NewWriter(writer)

    for partName, part := range parts {
        var writer io.Writer
        writer, err = zipWriter.Create(partName)
        if err != nil {
            return
        }
        _, err = writer.Write([]byte(part))
        if err != nil {
            return
        }
    }

    err = zipWriter.Close()

    return
}

开发者ID:lasithasenanayake,项目名称:v6engine-deps,代码行数:28,代码来源:file.go

示例9: foliazipdir

func foliazipdir(q *Context, zf *zip.Writer, fdir, subdir string) (ok bool) {
    dir := filepath.Join(fdir, subdir)
    files, err := ioutil.ReadDir(dir)
    if foliaErr(q, err) {
        return false
    }
    for _, file := range files {
        fname := path.Join(subdir, file.Name()) // in zip alleen forward slashes toegestaan
        if file.IsDir() {
            if !foliazipdir(q, zf, fdir, fname) {
                return false
            }
        } else {
            data, err := ioutil.ReadFile(filepath.Join(fdir, fname))
            if foliaErr(q, err) {
                return false
            }
            fh, err := zip.FileInfoHeader(file)
            if foliaErr(q, err) {
                return false
            }
            fh.Name = fname
            f, err := zf.CreateHeader(fh)
            if foliaErr(q, err) {
                return false
            }
            _, err = f.Write(data)
            if foliaErr(q, err) {
                return false
            }
        }
    }
    return true
}

开发者ID:rug-compling,项目名称:paqu,代码行数:34,代码来源:foliatool.go

示例10: addFileToZip

func addFileToZip(w *zip.Writer, path string, newname string) error {
    f, err := os.Open(path)
    if err != nil {
        return err
    }
    info, err := f.Stat()
    if err != nil {
        return err
    }
    header, err := zip.FileInfoHeader(info)
    if err != nil {
        return err
    }
    if newname != "" {
        header.Name = newname
    }
    wr, err := w.CreateHeader(header)
    if err != nil {
        return err
    }
    fileBytes, err := ioutil.ReadFile(path)
    if err != nil {
        return err
    }
    _, err = wr.Write(fileBytes)
    if err != nil {
        return err
    }
    return nil
}

开发者ID:maxmcd,项目名称:gitbao,代码行数:30,代码来源:builder.go

示例11: addFileToZip

func addFileToZip(zipWriter *zip.Writer, fi os.FileInfo, srcPath string) error {
    fh, err := zip.FileInfoHeader(fi)
    if err != nil {
        return err
    }
    fh.Method = zip.Deflate
    destFile, err := zipWriter.CreateHeader(fh)
    if err != nil {
        return err
    }

    srcFile, err := os.Open(srcPath)
    if err != nil {
        return err
    }
    defer srcFile.Close()

    ba, err := ioutil.ReadAll(srcFile)
    if err != nil {
        return err
    }

    //_, err = io.Copy(destFile, srcFile)
    _, err = destFile.Write(ba)
    return err
}

开发者ID:jquag,项目名称:kempt,代码行数:26,代码来源:util.go

示例12: addFileToZIP

func addFileToZIP(zw *zip.Writer, item ArchiveItem) (err error) {
    binfo, err := os.Stat(item.FileSystemPath)
    if err != nil {
        return
    }
    header, err := zip.FileInfoHeader(binfo)
    if err != nil {
        return
    }
    header.Method = zip.Deflate
    //always use forward slashes even on Windows
    header.Name = strings.Replace(item.ArchivePath, "\\", "/", -1)
    w, err := zw.CreateHeader(header)
    if err != nil {
        zw.Close()
        return
    }
    bf, err := os.Open(item.FileSystemPath)
    if err != nil {
        return
    }
    defer bf.Close()
    _, err = io.Copy(w, bf)
    return
}

开发者ID:kc87654321,项目名称:goxc,代码行数:25,代码来源:zip.go

示例13: addKeysToArchive

func addKeysToArchive(zipWriter *zip.Writer, newKeyStore *trustmanager.KeyFileStore, tempBaseDir string) error {
    // List all files but no symlinks
    for _, fullKeyPath := range newKeyStore.ListFiles(false) {
        relKeyPath := strings.TrimPrefix(fullKeyPath, tempBaseDir)
        relKeyPath = strings.TrimPrefix(relKeyPath, string(filepath.Separator))

        fi, err := os.Stat(fullKeyPath)
        if err != nil {
            return err
        }

        infoHeader, err := zip.FileInfoHeader(fi)
        if err != nil {
            return err
        }

        infoHeader.Name = relKeyPath
        zipFileEntryWriter, err := zipWriter.CreateHeader(infoHeader)
        if err != nil {
            return err
        }

        fileContents, err := ioutil.ReadFile(fullKeyPath)
        if err != nil {
            return err
        }
        if _, err = zipFileEntryWriter.Write(fileContents); err != nil {
            return err
        }
    }

    return nil
}

开发者ID:RichardScothern,项目名称:notary,代码行数:33,代码来源:import_export.go

示例14: addZipEntry

func addZipEntry(zw *zip.Writer, f filetesting.Entry) {
    h := &zip.FileHeader{
        Name: f.GetPath(),
        // Don't bother compressing - the contents are so small that
        // it will just slow things down for no particular benefit.
        Method: zip.Store,
    }
    contents := ""
    switch f := f.(type) {
    case filetesting.Dir:
        h.SetMode(os.ModeDir | 0755)
    case filetesting.File:
        h.SetMode(f.Perm)
        contents = f.Data
    case filetesting.Symlink:
        h.SetMode(os.ModeSymlink | 0777)
        contents = f.Link
    }
    w, err := zw.CreateHeader(h)
    if err != nil {
        panic(err)
    }
    if contents != "" {
        if _, err := w.Write([]byte(contents)); err != nil {
            panic(err)
        }
    }
}

开发者ID:jrwren,项目名称:charmrepo,代码行数:28,代码来源:testcharm.go

示例15: writeFileToZip

// 逐个文件,写入zip包
// zipper *zip.Writer:要写入的zip包的zip.Writer
// includeFileName string:本次写入的文件的全路径+名字
func writeFileToZip(zipper *zip.Writer, includeFileName string) error {
    //打开待写入文件
    includeFile, err := os.Open(includeFileName)
    if err != nil {
        return err
    }
    defer includeFile.Close()

    //获取文件描述
    includeFileInfo, err := includeFile.Stat()
    if err != nil {
        return err
    }

    //zip.FileInfoHeader
    zipFileHeader, err := zip.FileInfoHeader(includeFileInfo)
    if err != nil {
        return err
    }

    //修改文件描述的Header,截断路径,只保留文件名
    //否则,解压的时候,可能按压缩进来时候的文件路径来解压,解压回到原来的位置,而不是当前目录
    zipFileHeader.Name = path.Base(includeFileName)

    //用zip.FileInfoHeader,创建zip包内的一个项,并获得io.Writer,准备写入文件
    zipFileWriter, err := zipper.CreateHeader(zipFileHeader)
    if err != nil {
        return err
    }

    //写入本次的文件
    _, err = io.Copy(zipFileWriter, includeFile)
    return err
}

开发者ID:TYuenCN,项目名称:TYGo,代码行数:37,代码来源:compress.go

示例16: addIndexToZip

// addIndexToZip generates an index.html file for the given Overlay and adds
// it to the provided zip file.
func addIndexToZip(c appengine.Context, z *zip.Writer, oKey *datastore.Key, o *Overlay) error {
    w, err := z.Create(fmt.Sprintf("%s/index.html", oKey.Encode()))
    if err != nil {
        return err
    }
    return zipTemplate.Execute(w, o)
}

开发者ID:TheAustrianPro,项目名称:overlay-tiler,代码行数:9,代码来源:task.go

示例17: addZip

func addZip(filename, buildDir string, zipWriter *zip.Writer) error {
    zipfile, err := os.Open(filename)
    if err != nil {
        return err
    }
    defer zipfile.Close()

    info, err := zipfile.Stat()
    if err != nil {
        return err
    }

    header, err := zip.FileInfoHeader(info)
    if err != nil {
        return err
    }

    r := regexp.MustCompile(buildDir + "/")
    header.Name = r.ReplaceAllString(filename, "")
    writer, err := zipWriter.CreateHeader(header)
    if err != nil {
        return err
    }

    _, err = io.Copy(writer, zipfile)
    return nil
}

开发者ID:gophergala2016,项目名称:sisho,代码行数:27,代码来源:epubify.go

示例18: addHandlerToZip

func addHandlerToZip(w *zip.Writer, bf baofile.Baofile) error {
    path := "lambda/handler_example.js"
    f, err := os.Open(path)
    if err != nil {
        return err
    }
    info, err := f.Stat()
    if err != nil {
        return err
    }
    header, err := zip.FileInfoHeader(info)
    if err != nil {
        return err
    }
    wr, err := w.CreateHeader(header)
    if err != nil {
        return err
    }
    fileBytes, err := ioutil.ReadFile(path)
    if err != nil {
        return err
    }

    handlerTemplate := string(fileBytes)
    t := template.Must(template.New("handler").Parse(handlerTemplate))
    err = t.Execute(wr, bf)
    if err != nil {
        return err
    }
    return nil
}

开发者ID:maxmcd,项目名称:gitbao,代码行数:31,代码来源:builder.go

示例19: zipFile

// Deal with files
func zipFile(srcFile string, recPath string, tw *zip.Writer, fi os.FileInfo) {
    if fi.IsDir() {
        //      fmt.Println("??")
        // Create tar header
        /*
           fh, err := zip.FileInfoHeader(fi)
           if err != nil {
               panic(err)
           }
           fh.Name = recPath // + "/"
           err = tw.WriteHeader(hdr)
           tw.Create(recPath)
        */
    } else {
        // File reader
        fr, err := os.Open(srcFile)
        if err != nil {
            panic(err)
        }
        defer fr.Close()

        // Write hander
        w, err2 := tw.Create(recPath)
        if err2 != nil {
            panic(err)
        }
        // Write file data
        _, err = io.Copy(w, fr)
        if err != nil {
            panic(err)
        }
    }
}

开发者ID:ClaudeXin,项目名称:leanote,代码行数:34,代码来源:zip.go

示例20: ToZip

// Writes this tree to the given zip file, returning an error on failure.
func (t Stream) ToZip(w *zip.Writer) error {
    for blob := range t {

        hdr, err := zip.FileInfoHeader(blob)
        if err != nil {
            return err
        }

        writer, err := w.CreateHeader(hdr)
        if err != nil {
            return err
        }

        if blob.Contents() != nil {
            _, err := io.Copy(writer, blob.Contents())
            if err != nil {
                return err
            }
        }

        if blob.Error() != nil {
            return blob.Error()
        }
    }

    w.Close()

    return nil
}

开发者ID:fatlotus,项目名称:anvil,代码行数:30,代码来源:zip_files.go

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