Files
golib/archive
Nicolas JUHEL 97e70d063f Package Archives:
- fix security arbitrary path
- fix linter

Package AWS:
- implement resolver v2

Package Cobra:
- fix linter

Package Config/component:
- fix linter

Package Context/Config:
- Add function to set context

Package Database/KV...:
- Fix error
- Fix collision pointer
- Fix models
- Fix circular dependencies
- Add function Delete on driver, table and item
- Add function new on drvier to prevent collision data when create item on table get / walk

Package Duration:
- Add type Duration based on time.Duration to allow transform duration to string instead of int64 nanosecond
- Add function to parse in json, yaml, toml, text, cbor
- Add function to allow convert type into mapstructure (spf13 viper, cobra...)

Package File/Perm:
- Add type Perm based on os.FileMode to allow marshall / unmashall it into octal form instead of string representation (-rwxrwxrwx)
- Add function to marshall / unmarshall in json, yaml, toml, text, cbor
- Add function to allow convert type into mapstructure (spf13 viper, cobra...)

Package File/progress:
- Fix linter

Package HTTPServer :
- Fix linter
- Fix security by adding a default value if not set on config

Package ioutils:
- Fix Linter

Package LDAP:
- Add Clone function

Package logger/hookfile:
- Fix linter

Package nats:
- Fix linter

Package Network/Protocol:
- Fix bug with quote / Dbl Quote on unmarshall

Package Password:
- Replace password with crypto rand instead of math rand

Package Size:
- Fix potential overflow
- Add function to format value into Int32, Int, Uint32, Uint, Float32
- Add function to parse Float64 into type Size

Package Socket:
- change config uint32 to golib Size, time.Duration to golib Duration
- add TLS managment to server TCP, discard for UDP & Unix file Local Domain
- add function Info Server to print information of server when listen is starting
2023-11-21 20:21:37 +01:00
..
2023-11-21 20:21:37 +01:00
2023-11-21 20:21:37 +01:00
2023-11-21 20:21:37 +01:00
2023-11-21 20:21:37 +01:00
2023-10-03 12:03:36 +02:00
2020-09-22 10:09:32 +02:00

Package Archive

This package will try to do all uncompress/unarchive and extract one file or all file. This package will expose 2 functions :

  • ExtractFile : for one file extracted
  • ExtractAll : to extract all file

Example of implementation

Example of one file extracted

To use ExtractFile function, you will need this parameters :

  • src : is the source file into a ioutils.FileProgress struct to expose an os.File pointer with interface io.WriteTo, io.ReadFrom, io.ReaderAt and progress capabilities
  • dst : is the source file into a ioutils.FileProgress struct to expose an os.File pointer with interface io.WriteTo, io.ReadFrom, io.ReaderAt and progress capabilities
  • filenameContain : is a string to search in the file name to find it and extract it. This string will be search into the strings.Contains function
  • filenameRegex : is a regex pattern string to search in the file name any match and extract it. This string will be search into the regexp.MatchString function

You can implement this function as it. This example is available in test/test-archive folder.

 import (
	"io"
	"io/ioutil"

	"github.com/nabbar/golib/archive"
	"github.com/nabbar/golib/ioutils"
)

const fileName = "fullpath to my archive file"

func main() {
	var (
		src ioutils.FileProgress
		dst ioutils.FileProgress
		err errors.Error
	)

	// register closing function in output function callback
  defer func() {
		if src != nil {
			_ = src.Close()
		}
		if dst != nil {
			_ = dst.Close()
		}
	}()

	// open archive with a ioutils NewFileProgress function
  if src, err = ioutils.NewFileProgressPathOpen(fileName); err != nil {
		panic(err)
	}

	// open a destination with a ioutils NewFileProgress function, as a temporary file
	if dst, err = ioutils.NewFileProgressTemp(); err != nil {
		panic(err)
	}

  // call the extract file function
	if err = archive.ExtractFile(tmp, rio, "path/to/my/file/into/archive", "archive name regex"); err != nil {
		panic(err)
	}
  
}

Example of all files extracted

To use ExtractAll function, you will need this parameters :

  • src : is the source file into a ioutils.FileProgress struct to expose an os.File pointer with interface io.WriteTo, io.ReadFrom, io.ReaderAt and progress capabilities
  • originalName : is a string to define the originalName of the archive. This params is used to create a unique file created into the outputPath if the archive is not an archive or just compressed with a not catalogued compress type like gzip or bzip2.
  • outputPath : is a string to precise the destination output directory (full path). All extracted file will be extracted with this directory as base of path.
  • defaultDirPerm : is a os.FileMode to precise the permission of directory. This parameters is usefull if the output directory is not existing.

You can implement this function as it. This example is available in test/test-archive-all folder.

 import (
	"io"
	"io/ioutil"

	"github.com/nabbar/golib/archive"
	"github.com/nabbar/golib/ioutils"
)

const fileName = "fullpath to my archive file"

func main() {
	var (
		src ioutils.FileProgress
		tmp ioutils.FileProgress
		out string
		err error
	)
 
  // open archive with a ioutils NewFileProgress function 
	if src, err = ioutils.NewFileProgressPathOpen(fileName); err != nil {
		panic(err)
	}

  // create an new temporary file to use his name as output path
	if tmp, err = ioutils.NewFileProgressTemp(); err != nil {
		panic(err)
	} else {
    // get the filename of the temporary file
		out = tmp.FilePath()
    
    // close the temporary file will call the delete temporary file
		_ = tmp.Close()
	}
  
  if err = archive.ExtractAll(src, path.Base(src.FilePath()), out, 0775); err != nil {
		panic(err)
	}
  
}