README
¶
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 aioutils.FileProgress
struct to expose anos.File
pointer with interfaceio.WriteTo
,io.ReadFrom
,io.ReaderAt
and progress capabilitiesdst
: is the source file into aioutils.FileProgress
struct to expose anos.File
pointer with interfaceio.WriteTo
,io.ReadFrom
,io.ReaderAt
and progress capabilitiesfilenameContain
: is astring
to search in the file name to find it and extract it. This string will be search into thestrings.Contains
functionfilenameRegex
: is a regex patternstring
to search in the file name any match and extract it. This string will be search into theregexp.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 aioutils.FileProgress
struct to expose anos.File
pointer with interfaceio.WriteTo
,io.ReadFrom
,io.ReaderAt
and progress capabilitiesoriginalName
: is astring
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 astring
to precise the destination output directory (full path). All extracted file will be extracted with this directory as base of path.defaultDirPerm
: is aos.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)
}
}
Documentation
¶
Index ¶
- func DetectArchive(r io.ReadCloser) (arcarc.Algorithm, arctps.Reader, io.ReadCloser, error)
- func DetectCompression(r io.Reader) (arccmp.Algorithm, io.ReadCloser, error)
- func ExtractAll(r io.ReadCloser, archiveName, destination string) error
- func NopWriteCloser(w io.Writer) io.WriteCloser
- func ParseArchive(s string) arcarc.Algorithm
- func ParseCompression(s string) arccmp.Algorithm
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DetectArchive ¶ added in v1.15.0
func DetectArchive(r io.ReadCloser) (arcarc.Algorithm, arctps.Reader, io.ReadCloser, error)
func DetectCompression ¶ added in v1.15.0
func ExtractAll ¶ added in v1.3.0
func ExtractAll(r io.ReadCloser, archiveName, destination string) error
func NopWriteCloser ¶ added in v1.15.0
func NopWriteCloser(w io.Writer) io.WriteCloser
func ParseArchive ¶ added in v1.15.0
func ParseCompression ¶ added in v1.15.0
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.