-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
420 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# 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`](../test/test-archive/main.go) folder. | ||
```go | ||
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`](../test/test-archive-all/main.go) folder. | ||
```go | ||
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) | ||
} | ||
|
||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2020 Nicolas JUHEL | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
* | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
|
||
"github.com/nabbar/golib/archive" | ||
"github.com/nabbar/golib/errors" | ||
"github.com/nabbar/golib/ioutils" | ||
"github.com/nabbar/golib/logger" | ||
) | ||
|
||
// git archive --format=tar --output=git.tar HEAD && gzip git.tar | ||
const fileName = "./git.tar.gz" | ||
|
||
func main() { | ||
var ( | ||
src ioutils.FileProgress | ||
tmp ioutils.FileProgress | ||
out string | ||
err error | ||
) | ||
|
||
defer func() { | ||
if src != nil { | ||
_ = src.Close() | ||
} | ||
if tmp != nil { | ||
_ = tmp.Close() | ||
} | ||
}() | ||
|
||
logger.SetLevel(logger.DebugLevel) | ||
logger.EnableColor() | ||
logger.FileTrace(true) | ||
errors.SetModeReturnError(errors.ErrorReturnCodeErrorTraceFull) | ||
|
||
if src, err = ioutils.NewFileProgressPathOpen(fileName); err != nil { | ||
panic(err) | ||
} | ||
|
||
if tmp, err = ioutils.NewFileProgressTemp(); err != nil { | ||
panic(err) | ||
} else { | ||
out = tmp.FilePath() | ||
_ = tmp.Close() | ||
} | ||
|
||
if err = archive.ExtractAll(src, path.Base(src.FilePath()), out, 0775); err != nil { | ||
panic(err) | ||
} | ||
|
||
if list, e := ioutil.ReadDir(out); e != nil { | ||
panic(e) | ||
} else { | ||
for _, f := range list { | ||
var ( | ||
isDir bool | ||
isLink bool | ||
isFile bool | ||
) | ||
isDir = f.IsDir() | ||
isLink = f.Mode()&os.ModeSymlink == os.ModeSymlink | ||
isFile = !isLink && !isDir | ||
println(fmt.Sprintf("Item '%s' is Dir '%v', is Link '%v', is File '%v'", f.Name(), isDir, isLink, isFile)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.