golang 压缩原apk文件夹 安装不了 但是用其他压缩软件压缩后能安装?求解决我想golang批量打包apk

a610569731 · · 1760 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

package main import ( "archive/zip" "errors" "fmt" "io" "os" "path/filepath" "strings" ) func SubString(str string, begin, length int) (substr string) { // 将字符串的转换成[]rune rs := []rune(str) lth := len(rs) // 简单的越界判断 if begin < 0 { begin = 0 } if begin >= lth { begin = lth } end := begin + length if end > lth { end = lth } // 返回子串 return string(rs[begin:end]) } const ( singleFileByteLimit = 107374182400 // 1 GB chunkSize = 4096 // 4 KB ) func copyContents(r io.Reader, w io.Writer) error { var size int64 b := make([]byte, chunkSize) for { // we limit the size to avoid zip bombs size += chunkSize if size > singleFileByteLimit { return errors.New("file too large, please contact us for assistance") } // read chunk into memory length, err := r.Read(b[:cap(b)]) if err != nil { if err != io.EOF { return err } if length == 0 { break } } // write chunk to zip file _, err = w.Write(b[:length]) if err != nil { return err } } return nil } // We need a struct internally because the filepath WalkFunc // doesn't allow custom params. So we save them here so it can // access them type zipper struct { srcFolder string destFile string writer *zip.Writer } // internal function to zip a file, called by filepath.Walk on each file func (z *zipper) zipFile(path string, f os.FileInfo, err error) error { if err != nil { return err } // only zip files (directories are created by the files inside of them) // TODO allow creating folder when no files are inside if !f.Mode().IsRegular() || f.Size() == 0 { return nil } // open file file, err := os.Open(path) if err != nil { return err } defer file.Close() // create new file in zip fileName := strings.TrimPrefix(path, z.srcFolder+"/") result := SubString(fileName, len(z.srcFolder)-1, len(fileName)-len(z.srcFolder)+1) fmt.Println("fileName:", result) fmt.Println(path) w, err := z.writer.Create(result) if err != nil { return err } // copy contents of the file to the zip writer err = copyContents(file, w) if err != nil { return err } return nil } // internal function to zip a folder func (z *zipper) zipFolder() error { // create zip file zipFile, err := os.Create(z.destFile) if err != nil { return err } defer zipFile.Close() // create zip writer z.writer = zip.NewWriter(zipFile) err = filepath.Walk(z.srcFolder, z.zipFile) if err != nil { return nil } // close the zip file err = z.writer.Close() if err != nil { return err } return nil } // ZipFolder zips the given folder to the a zip file // with the given name func ZipFolder(srcFolder string, destFile string) error { z := &zipper{ srcFolder: srcFolder, destFile: destFile, } return z.zipFolder() } func main() { // file write ZipFolder("d://test//src", "d://test//src2.apk") fmt.Println("zip ok") }

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1760 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传