dozip

package
v0.3.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 16, 2022 License: MIT Imports: 6 Imported by: 0

README

Dozip 搞定解压缩

多文件压缩

func FilesToZip(destZip string, srcFiles ...string) error 

目录压缩

func DirToZip(destZip string, srcDir string) error

解压缩

func Unzip(srcZip, destDir string, opts *UnZipOptions) error 

单元测试

package dozip

import "testing"

func TestDirToZip(t *testing.T) {
	type args struct {
		destZip string
		srcDir  string
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{name: "多文件压缩", args: args{destZip: "./destzip.zip", srcDir: "srcFile"},wantErr: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := DirToZip(tt.args.destZip, tt.args.srcDir); (err != nil) != tt.wantErr {
				t.Errorf("DirToZip() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func TestFilesToZip(t *testing.T) {
	type args struct {
		destZip  string
		srcFiles []string
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		{name: "多文件压缩", args: args{destZip: "./destzip2.zip", srcFiles: []string{"./srcFile/x.abc","dozip.go"}},wantErr: false},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := FilesToZip(tt.args.destZip, tt.args.srcFiles...); (err != nil) != tt.wantErr {
				t.Errorf("FilesToZip() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

func TestUnzip(t *testing.T) {
	type args struct {
		srcZip  string
		destDir string
		opts    *UnZipOptions
	}
	tests := []struct {
		name    string
		args    args
		wantErr bool
	}{
		//解压目录
		{name: "解压", args: args{srcZip: "./destzip2.zip", destDir: "./dest", opts: &UnZipOptions{}}},
		//不解压目录
		{name: "解压", args: args{srcZip: "./destzip2.zip", destDir: "./dest", opts: &UnZipOptions{NotUnzipDir: true}}},
		//限制最大解压文件
		{name: "解压", args: args{srcZip: "./destzip2.zip", destDir: "./dest", opts: &UnZipOptions{MaxFileSize: 3500}}},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if err := Unzip(tt.args.srcZip, tt.args.destDir, tt.args.opts); (err != nil) != tt.wantErr {
				t.Errorf("Unzip() error = %v, wantErr %v", err, tt.wantErr)
			}
		})
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DirToZip

func DirToZip(destZip string, srcDir string) error

压缩目录

func FilesToZip

func FilesToZip(destZip string, srcFiles ...string) error

压缩一个或多个文件

func Unzip

func Unzip(srcZip, destDir string, opts *UnZipOptions) error

Types

type UnZipOptions

type UnZipOptions struct {
	//不解压目录
	NotUnzipDir bool
	//可以解压的最大文件限制(byte)
	//当超过这个限制,该文件将被跳过解压
	MaxFileSize int64
}

解压

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL