2018-06-11 15:57:19 +02:00
2018-06-11 15:58:11 +02:00
2018-09-14 19:55:37 +07:00
2015-10-27 04:12:51 -05:00
2018-02-23 12:14:14 +07:00
2015-10-27 04:12:51 -05:00
2015-10-27 04:12:51 -05:00
2015-12-02 00:58:01 -06:00

ByteVibe ZIP

这是为 ZipVeil 独立维护的 Go ZIP/AES 库,源自 github.com/yeka/zip,基于上游提交 03d6312748a9。保留原始许可证、提交历史、测试和测试数据。

本 fork 的修改

  • 新增 Go 模块路径 gitea.bytevibe.dev/gary/zip,使用 Go 1.26,提供固定的依赖与校验文件。
  • 增加 (*Writer).RegisterCompressor,让每个 writer 独立设置压缩器。支持对 Deflate 设置级别,无需修改全局配置;普通 ZIP 和 WinZip AES 都使用这一配置。
  • 默认 Deflate 级别保持为 5;同一个 writer 应在添加文件前完成配置,且不支持并发写入同一个 writer。

独立配置 Deflate

zipw := zip.NewWriter(output)
zipw.RegisterCompressor(zip.Deflate, func(w io.Writer) (io.WriteCloser, error) {
    return flate.NewWriter(w, 9)
})

上面使用 compress/flate、io 和 gitea.bytevibe.dev/gary/zip。完整的加密/解密示例见下文。

安装与测试

go get gitea.bytevibe.dev/gary/zip@master
go test ./...
go test -race ./...

若仓库设为私有,需要配置 Gitea 访问凭据及 Go 的 GOPRIVATE=gitea.bytevibe.dev。不将凭据写入模块路径或代码。上游保留为 upstream remote 持续获取更新;本 fork 的提交推送到 Gitea 的 origin。调用方应固定到发布标签或提交对应的 Go 伪版本,避免依赖随 master 更新而变化。

上游说明

This fork add support for Standard Zip Encryption.

The work is based on https://github.com/alexmullins/zip

Available encryption:

zip.StandardEncryption
zip.AES128Encryption
zip.AES192Encryption
zip.AES256Encryption

Warning

Zip Standard Encryption isn't actually secure. Unless you have to work with it, please use AES encryption instead.

Example Encrypt Zip

package main

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

	"gitea.bytevibe.dev/gary/zip"
)

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

Example Decrypt Zip

package main

import (
	"fmt"
	"io/ioutil"
	"log"

	"gitea.bytevibe.dev/gary/zip"
)

func main() {
	r, err := zip.OpenReader("encrypted.zip")
	if err != nil {
		log.Fatal(err)
	}
	defer r.Close()

	for _, f := range r.File {
		if f.IsEncrypted() {
			f.SetPassword("12345")
		}

		r, err := f.Open()
		if err != nil {
			log.Fatal(err)
		}

		buf, err := ioutil.ReadAll(r)
		if err != nil {
			log.Fatal(err)
		}
		defer r.Close()

		fmt.Printf("Size of %v: %v byte(s)\n", f.Name, len(buf))
	}
}
S
Description
No description provided
Readme MIT
133 KiB
0 Stars 1 Watchers 0 Forks
Languages
Go 100%