GitHub - GKoSon/zip: 使用GO对文件执行压缩/加密

需求:一个文件 加密 压缩 可以返回来

1---新建文件夹
C:\Users\Koson.Gong\Desktop\pi_ota_tool

2---文件夹内启动VSCODE 新建文件main.go

package main
 import "fmt"func main() {
     fmt.Println("HELLO WORLD")
 }

3--使用开源方案
3.1

    "github.com/alexmullins/zip"
  它的格式是github.com+作者+仓库名称
此后是可以使用仓库名称-包名的函数方法的  
注意 不能直接使用 zip_test.ExampleWrite_Encrypy()
因为它不是zip这个包里面的

需要自己写

3.2  alexmullins/zip 全github搜索的时候发现一个新的
那就用新的吧

https://github.com/looklzj/zip

3.3编写代码 完成copy func ExampleWriter_Encrypt() {

main.go:10:2: no required module provides package github.com/looklzj/zip: go.mod file not found in current directory or any parent directory; see 'go help modules'
开始操作
  go mod init github.com/GKoSon/zip
 go mod tidygo env -w GO111MODULE=on
 go env -w GOPROXY=https://goproxy.cn,direct

在run代码

package main

import (
	"bytes"
	"io"
	"log"
	"os"

	"github.com/looklzj/zip"
)

func main() {
	contents := []byte("Hello Koson")

	// write a password zip
	raw := new(bytes.Buffer)
	zipw := zip.NewWriter(raw)
	w, err := zipw.Encrypt("hellogo.txt", "golang")
	if err != nil {
		log.Fatal(err)
	}
	_, err = io.Copy(w, bytes.NewReader(contents))
	if err != nil {
		log.Fatal(err)
	}
	zipw.Close()

	// read the password zip
	zipr, err := zip.NewReader(bytes.NewReader(raw.Bytes()), int64(raw.Len()))
	if err != nil {
		log.Fatal(err)
	}
	for _, z := range zipr.File {
		z.SetPassword("golang")
		rr, err := z.Open()
		if err != nil {
			log.Fatal(err)
		}
		_, err = io.Copy(os.Stdout, rr)
		if err != nil {
			log.Fatal(err)
		}
		rr.Close()
	}
}

 

============上面没有出现文件  回到第一个作者吧 run 可以

package main

import (
	"bytes"
	"io"
	"log"
	"os"

	"github.com/looklzj/zip"
)

func main() {
	//contents := []byte("Hello World")
	contents, err := os.ReadFile("./rpi_gw")
	if err != nil {
		log.Fatalln(err)
	}
	fzip, err := os.Create(`./rpi_gw.zip`)
	if err != nil {
		log.Fatalln(err)
	}
	zipw := zip.NewWriter(fzip)
	defer zipw.Close()
	w, err := zipw.Encrypt(`rpi_gw`, `golang`)
	if err != nil {
		log.Fatal(err)
	}
	_, err = io.Copy(w, bytes.NewReader(contents))
	if err != nil {
		log.Fatal(err)
	}
	zipw.Flush()
}

可以把文件 压缩 360解压可以一样

go打包到docker并设置文件映射 go 打包_开发语言

测试exe 文本 都可以到ZIP

但是文件没有压缩

解压压流程

package main

import (
	"fmt"
	"io"
	"log"
	"os"

	"github.com/looklzj/zip"
)

func main() {

	r, err := zip.OpenReader("./rpi_gw.zip")
	if err != nil {
		log.Fatal(err)
	}
	defer r.Close()
	err = os.Mkdir("./new4", 0777)
	if err != nil {
		log.Fatal(err)
	}

	of, err := os.Create("./new4/rpi_gw")
	defer of.Close()

	for _, f := range r.File {
		fmt.Printf("Contents of %s:\n", f.Name)
		f.SetPassword("golang")
		rr, err := f.Open()
		if err != nil {
			log.Fatal(err)
		}
		//_, err = io.Copy(os.Stdout, rr)
		_, err = io.Copy(of, rr)

		if err != nil {
			log.Fatal(err)
		}
		rr.Close()

		fmt.Println()
	}

}

整理

package main

import (
	"fmt"
	"io"
	"os"
	"strings"

	"github.com/looklzj/zip"
)

var ZIP_PATH string = "./rpi_gw.zip"

func must(action string, err error) {
	if err != nil {
		panic("failed to " + action + ": " + err.Error())
	}
}
func main() {
	r, err := zip.OpenReader(ZIP_PATH)
	must("zip.OpenReader", err)
	defer r.Close()
	NEW_DIR_PATH := ZIP_PATH[0:strings.Index(ZIP_PATH, ".zip")]
	fmt.Printf("NEW_DIR_PATH = %s\n", NEW_DIR_PATH)
again:
	err = os.Mkdir(NEW_DIR_PATH, 0777)
	if err != nil {
		must("os.RemoveAll", os.RemoveAll("./rpi_gw"))
		goto again
	}

	for _, f := range r.File {
		fmt.Printf("f.Name = %s\n", f.Name)
		NEW_FILE_PATH := NEW_DIR_PATH + "/" + f.Name
		fmt.Printf("NEW_FILE_PATH = %s\n", NEW_FILE_PATH)

		of, err := os.Create(NEW_FILE_PATH)
		must("os.Create", err)
		defer of.Close()
		f.SetPassword("golang")
		rr, err := f.Open()
		must("io.Open", err)
		//_, err = io.Copy(os.Stdout, rr)
		_, err = io.Copy(of, rr)

		must("io.Copy", err)
		rr.Close()
	}

}
package main

import (
	"bytes"
	"io"
	"os"

	"github.com/looklzj/zip"
)

var FILE_NAME string = "rpi_gw"

func must(action string, err error) {
	if err != nil {
		panic("failed to " + action + ": " + err.Error())
	}
}
func main() {
	//contents := []byte("Hello World")
	FILE_PATH := "./" + FILE_NAME
	contents, err := os.ReadFile(FILE_PATH)//打开文件
	must("os.ReadFile", err)

	ZIP_PATH := FILE_PATH + ".zip"
	fzip, err := os.Create(ZIP_PATH)//建立ZIP文件
	must("os.Create", err)

	zipw := zip.NewWriter(fzip)
	defer zipw.Close()
	w, err := zipw.Encrypt(FILE_NAME, "KOSON")//ZIP里面的文件名称是FILE_NAME 密码是KOSON
	must("zipw.Encrypt", err)

	_, err = io.Copy(w, bytes.NewReader(contents))//把文件A写道文件B
	must("io.Copy", err)
	zipw.Flush()
}

++++++++++++已经完成  现在MD5++++++++++

package main

import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io"
	"os"

	"github.com/looklzj/zip"
)

var FILE_NAME string = "rpi_gw"

func must(action string, err error) {
	if err != nil {
		panic("failed to " + action + ": " + err.Error())
	}
}

// 获取文件的md5码
func getFileMd5(filename string) string {
	// 文件全路径名
	path := fmt.Sprintf("./%s", filename)
	pFile, err := os.Open(path)
	if err != nil {
		fmt.Errorf("打开文件失败,filename=%v, err=%v", filename, err)
		return ""
	}
	defer pFile.Close()
	md5h := md5.New()
	io.Copy(md5h, pFile)

	return hex.EncodeToString(md5h.Sum(nil))
}

func main() {
	//contents := []byte("Hello World")
	FILE_PATH := "./" + FILE_NAME
	contents, err := os.ReadFile(FILE_PATH) //打开文件
	must("os.ReadFile", err)

	ZIP_PATH := FILE_PATH + ".zip"
	fzip, err := os.Create(ZIP_PATH) //建立ZIP文件
	must("os.Create", err)

	zipw := zip.NewWriter(fzip)
	defer zipw.Close()
	w, err := zipw.Encrypt(FILE_NAME, "KOSON") //ZIP里面的文件名称是FILE_NAME 密码是KOSON
	must("zipw.Encrypt", err)

	_, err = io.Copy(w, bytes.NewReader(contents)) //把文件A写道文件B
	must("io.Copy", err)
	zipw.Flush()

	FILE_MD5 := FILE_NAME + "." + "md5"
	w, err = zipw.Encrypt(FILE_MD5, "KOSON")
	must("zipw.Encrypt", err)
	md5, _ := hex.DecodeString(getFileMd5(FILE_PATH))
	_, err = io.Copy(w, bytes.NewReader(md5))
	must("io.Copy", err)
	zipw.Flush()
}

写的时候 加入MD5 

解压的时候 不需要修改!!!本来就是循环的!!!

package main

import (
	"fmt"
	"io"
	"os"
	"strings"

	"github.com/looklzj/zip"
)

var ZIP_PATH string = "./rpi_gw.zip"
var PASSWD string = "KOSON"

func must(action string, err error) {
	if err != nil {
		panic("failed to " + action + ": " + err.Error())
	}
}
func main() {
	r, err := zip.OpenReader(ZIP_PATH)
	must("zip.OpenReader", err)
	defer r.Close()
	NEW_DIR_PATH := ZIP_PATH[0:strings.Index(ZIP_PATH, ".zip")]
	fmt.Printf("NEW_DIR_PATH = %s\n", NEW_DIR_PATH)
again:
	err = os.Mkdir(NEW_DIR_PATH, 0777)
	if err != nil {
		must("os.RemoveAll", os.RemoveAll("./rpi_gw"))
		goto again
	}

	for _, f := range r.File {
		fmt.Printf("f.Name = %s\n", f.Name)
		NEW_FILE_PATH := NEW_DIR_PATH + "/" + f.Name
		fmt.Printf("NEW_FILE_PATH = %s\n", NEW_FILE_PATH)

		of, err := os.Create(NEW_FILE_PATH)
		must("os.Create", err)
		defer of.Close()
		f.SetPassword(PASSWD)
		rr, err := f.Open()
		must("io.Open", err)
		//_, err = io.Copy(os.Stdout, rr)
		_, err = io.Copy(of, rr)

		must("io.Copy", err)
		rr.Close()
	}

}

合在一起

package main

import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"fmt"
	"io"
	"os"
	"strings"

	"github.com/looklzj/zip"
)

//COMMON
var PASSWD string = "KOSON"

//ZIP-->FILE
var ZIP_PATH string = "./rpi_gw.zip"

//FILE->ZIP
var FILE_NAME string = "rpi_gw"

func must(action string, err error) {
	if err != nil {
		panic("failed to " + action + ": " + err.Error())
	}
}
func Out() {
	r, err := zip.OpenReader(ZIP_PATH)
	must("zip.OpenReader", err)
	defer r.Close()
	NEW_DIR_PATH := ZIP_PATH[0:strings.Index(ZIP_PATH, ".zip")]
	fmt.Printf("NEW_DIR_PATH = %s\n", NEW_DIR_PATH)

again:
	err = os.Mkdir(NEW_DIR_PATH, 0777)
	if err != nil {
		must("os.RemoveAll", os.RemoveAll(ZIP_PATH))
		goto again
	}

	for _, f := range r.File {
		fmt.Printf("f.Name = %s\n", f.Name)
		NEW_FILE_PATH := NEW_DIR_PATH + "/" + f.Name
		fmt.Printf("NEW_FILE_PATH = %s\n", NEW_FILE_PATH)

		of, err := os.Create(NEW_FILE_PATH)
		must("os.Create", err)

		f.SetPassword(PASSWD)
		rr, err := f.Open()
		must("io.Open", err)
		//_, err = io.Copy(os.Stdout, rr)
		_, err = io.Copy(of, rr)

		must("io.Copy", err)
		rr.Close()
		of.Close()
	}
}
func checkFileMd5() bool {
	FILE_PATH := "./" + FILE_NAME + "/" + FILE_NAME
	MD5_FILE_PATH := "./" + FILE_NAME + "/" + FILE_NAME + ".md5"
	//fmt.Println(FILE_PATH)
	//fmt.Println(MD5_FILE_PATH)
	pFile, err := os.Open(FILE_PATH)
	if err != nil {
		fmt.Printf("打开文件失败,filename=%v, err=%v", FILE_PATH, err)
		return false
	}
	defer pFile.Close()
	md5h := md5.New()
	io.Copy(md5h, pFile)
	mymd5 := hex.EncodeToString(md5h.Sum(nil))

	youmd5, err := os.ReadFile(MD5_FILE_PATH)
	if err != nil {
		fmt.Printf("打开文件失败,filename=%v, err=%v", MD5_FILE_PATH, err)
		return false
	}
	fmt.Println(hex.EncodeToString(youmd5))
	fmt.Println(mymd5)
	return mymd5 == hex.EncodeToString(youmd5)
}

// 获取文件的md5码
func getFileMd5(filename string) string {
	// 文件全路径名
	path := fmt.Sprintf("./%s", filename)
	pFile, err := os.Open(path)
	if err != nil {
		fmt.Printf("打开文件失败,filename=%v, err=%v", filename, err)
		return ""
	}
	defer pFile.Close()
	md5h := md5.New()
	io.Copy(md5h, pFile)

	return hex.EncodeToString(md5h.Sum(nil))
}

func In() {
	//contents := []byte("Hello World")
	FILE_PATH := "./" + FILE_NAME
	contents, err := os.ReadFile(FILE_PATH) //打开文件
	must("os.ReadFile", err)

	ZIP_PATH := FILE_PATH + ".zip"
	fzip, err := os.Create(ZIP_PATH) //建立ZIP文件
	must("os.Create", err)

	zipw := zip.NewWriter(fzip)
	defer zipw.Close()
	w, err := zipw.Encrypt(FILE_NAME, PASSWD) //ZIP里面的文件名称是FILE_NAME 密码是KOSON
	must("zipw.Encrypt", err)

	_, err = io.Copy(w, bytes.NewReader(contents)) //把文件A写道文件B
	must("io.Copy", err)
	zipw.Flush()

	FILE_MD5 := FILE_NAME + "." + "md5"
	w, err = zipw.Encrypt(FILE_MD5, PASSWD)
	must("zipw.Encrypt", err)
	md5, _ := hex.DecodeString(getFileMd5(FILE_PATH))
	_, err = io.Copy(w, bytes.NewReader(md5))
	must("io.Copy", err)
	zipw.Flush()
}

func main() {
	Out()
	fmt.Println(checkFileMd5())
}

建立文件夹ZIP

go打包到docker并设置文件映射 go 打包_开发语言_02

剪切进去

修改main

go打包到docker并设置文件映射 go 打包_go打包到docker并设置文件映射_03

go打包到docker并设置文件映射 go 打包_github_04

为什么我的是/zip/zip?

它是github的url!!!!就是这个仓库 里面的一个文件夹ZIP 这里面的一个包ZIP  而文件的名可以是X.go 但是内部必须是zip包

++++++++++++++++github已经开源以后+++++++++

新项目 不需要clone我的仓库

只需要自己本地写main

就复制我的main的2句话即可 

然后mod  tidy 自动拉下来 就可以编译ok了

+++++++++++++++++++++++++++++++

文件夹怎么操作?


package main

import (
	"archive/zip"
	"io"
	"log"
	"os"
	"path/filepath"
)

func zipDir(dir, zipFile string) {

	fz, err := os.Create(zipFile)
	if err != nil {
		log.Fatalf("Create zip file failed: %s\n", err.Error())
	}
	defer fz.Close()

	w := zip.NewWriter(fz)
	defer w.Close()

	filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
		if !info.IsDir() {
			fDest, err := w.Create(path[len(dir)+1:])
			if err != nil {
				log.Printf("Create failed: %s\n", err.Error())
				return nil
			}
			fSrc, err := os.Open(path)
			if err != nil {
				log.Printf("Open failed: %s\n", err.Error())
				return nil
			}
			defer fSrc.Close()
			_, err = io.Copy(fDest, fSrc)
			if err != nil {
				log.Printf("Copy failed: %s\n", err.Error())
				return nil
			}
		}
		return nil
	})
}

func unzipDir(zipFile, dir string) {

	r, err := zip.OpenReader(zipFile)
	if err != nil {
		log.Fatalf("Open zip file failed: %s\n", err.Error())
	}
	defer r.Close()

	for _, f := range r.File {
		func() {
			path := dir + string(filepath.Separator) + f.Name
			os.MkdirAll(filepath.Dir(path), 0755)
			fDest, err := os.Create(path)
			if err != nil {
				log.Printf("Create failed: %s\n", err.Error())
				return
			}
			defer fDest.Close()

			fSrc, err := f.Open()
			if err != nil {
				log.Printf("Open failed: %s\n", err.Error())
				return
			}
			defer fSrc.Close()

			_, err = io.Copy(fDest, fSrc)
			if err != nil {
				log.Printf("Copy failed: %s\n", err.Error())
				return
			}
		}()
	}
}

func main() {
	zipDir("C:\\Users\\Koson.Gong\\Desktop\\XX\\rpi_gw", "D:\\adb\\test.zip")
	unzipDir("D:\\adb\\test.zip", "D:\\adb")
}

把我这个文件夹

C:\Users\Koson.Gong\Desktop\XX\rpi_gw

压缩到 D:\adb里面 名字是test.zip

都可解压回来 的