-
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.
Merge pull request #3 from essentialkaos/develop
Version 1.1.0
- Loading branch information
Showing
29 changed files
with
879 additions
and
47 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,70 @@ | ||
// Package bz2 provides methods for unpacking bz2 files | ||
package bz2 | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"bufio" | ||
"compress/bzip2" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// Unpacks file to given directory | ||
func Unpack(file, dir string) error { | ||
switch { | ||
case file == "": | ||
return fmt.Errorf("Path to input file can not be empty") | ||
case dir == "": | ||
return fmt.Errorf("Path to output file can not be empty") | ||
} | ||
|
||
fd, err := os.OpenFile(file, os.O_RDONLY, 0) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
defer fd.Close() | ||
|
||
return Read( | ||
bufio.NewReader(fd), | ||
filepath.Join( | ||
filepath.Clean(dir), | ||
strings.TrimSuffix(filepath.Base(file), ".bz2"), | ||
), | ||
) | ||
} | ||
|
||
// Read reads compressed data using given reader and unpacks it to | ||
// the given directory | ||
func Read(r io.Reader, output string) error { | ||
switch { | ||
case r == nil: | ||
return fmt.Errorf("Reader can not be nil") | ||
case output == "": | ||
return fmt.Errorf("Path to output file can not be empty") | ||
} | ||
|
||
fd, err := os.OpenFile(output, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0640) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
bw := bufio.NewWriter(fd) | ||
_, err = io.Copy(bw, bzip2.NewReader(r)) | ||
|
||
bw.Flush() | ||
fd.Close() | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package bz2 | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/essentialkaos/ek/v12/fsutil" | ||
"github.com/essentialkaos/ek/v12/hash" | ||
|
||
. "github.com/essentialkaos/check" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
type BZ2Suite struct { | ||
Dir string | ||
} | ||
|
||
var _ = Suite(&BZ2Suite{}) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func (s *BZ2Suite) SetUpSuite(c *C) { | ||
s.Dir = c.MkDir() | ||
} | ||
|
||
func (s *BZ2Suite) TestUnpack(c *C) { | ||
err := Unpack("../.testdata/data.txt.bz2", s.Dir) | ||
|
||
c.Assert(err, IsNil) | ||
|
||
c.Assert(fsutil.IsExist(s.Dir+"/data.txt"), Equals, true) | ||
c.Assert(fsutil.GetMode(s.Dir+"/data.txt"), Equals, os.FileMode(0640)) | ||
|
||
c.Assert(hash.FileHash(s.Dir+"/data.txt"), Equals, "918c03a211adc19a466c9db22efa575efb6c488fd41c70e57b1ec0920f1a1d8c") | ||
} | ||
|
||
func (s *BZ2Suite) TestErrors(c *C) { | ||
err := Unpack("", "/_unknown") | ||
c.Assert(err, NotNil) | ||
|
||
err = Unpack("../.testdata/data.txt.bz2", "") | ||
c.Assert(err, NotNil) | ||
|
||
err = Unpack("/_unknown", s.Dir) | ||
c.Assert(err, NotNil) | ||
|
||
err = Read(nil, "/_unknown") | ||
c.Assert(err, NotNil) | ||
|
||
err = Read(strings.NewReader(""), "") | ||
c.Assert(err, NotNil) | ||
|
||
err = Read(strings.NewReader(""), "/_unknown") | ||
c.Assert(err, NotNil) | ||
} |
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,77 @@ | ||
// Package gz provides methods for unpacking gz files | ||
package gz | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/klauspost/compress/gzip" | ||
) | ||
|
||
// Unpacks file to given directory | ||
func Unpack(file, dir string) error { | ||
switch { | ||
case file == "": | ||
return fmt.Errorf("Path to input file can not be empty") | ||
case dir == "": | ||
return fmt.Errorf("Path to output file can not be empty") | ||
} | ||
|
||
fd, err := os.OpenFile(file, os.O_RDONLY, 0) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
defer fd.Close() | ||
|
||
return Read( | ||
bufio.NewReader(fd), | ||
filepath.Join( | ||
filepath.Clean(dir), | ||
strings.TrimSuffix(filepath.Base(file), ".gz"), | ||
), | ||
) | ||
} | ||
|
||
// Read reads compressed data using given reader and unpacks it to | ||
// the given directory | ||
func Read(r io.Reader, output string) error { | ||
switch { | ||
case r == nil: | ||
return fmt.Errorf("Reader can not be nil") | ||
case output == "": | ||
return fmt.Errorf("Path to output file can not be empty") | ||
} | ||
|
||
fd, err := os.OpenFile(output, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0640) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
gr, err := gzip.NewReader(r) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
bw := bufio.NewWriter(fd) | ||
_, err = io.Copy(bw, gr) | ||
|
||
bw.Flush() | ||
fd.Close() | ||
|
||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package gz | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
// // | ||
// Copyright (c) 2023 ESSENTIAL KAOS // | ||
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> // | ||
// // | ||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/essentialkaos/ek/v12/fsutil" | ||
"github.com/essentialkaos/ek/v12/hash" | ||
|
||
. "github.com/essentialkaos/check" | ||
) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func Test(t *testing.T) { TestingT(t) } | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
type GZSuite struct { | ||
Dir string | ||
} | ||
|
||
var _ = Suite(&GZSuite{}) | ||
|
||
// ////////////////////////////////////////////////////////////////////////////////// // | ||
|
||
func (s *GZSuite) SetUpSuite(c *C) { | ||
s.Dir = c.MkDir() | ||
} | ||
|
||
func (s *GZSuite) TestUnpack(c *C) { | ||
err := Unpack("../.testdata/data.txt.gz", s.Dir) | ||
|
||
c.Assert(err, IsNil) | ||
|
||
c.Assert(fsutil.IsExist(s.Dir+"/data.txt"), Equals, true) | ||
c.Assert(fsutil.GetMode(s.Dir+"/data.txt"), Equals, os.FileMode(0640)) | ||
|
||
c.Assert(hash.FileHash(s.Dir+"/data.txt"), Equals, "918c03a211adc19a466c9db22efa575efb6c488fd41c70e57b1ec0920f1a1d8c") | ||
} | ||
|
||
func (s *GZSuite) TestErrors(c *C) { | ||
err := Unpack("", "/_unknown") | ||
c.Assert(err, NotNil) | ||
|
||
err = Unpack("../.testdata/data.txt.gz", "") | ||
c.Assert(err, NotNil) | ||
|
||
err = Unpack("/_unknown", s.Dir) | ||
c.Assert(err, NotNil) | ||
|
||
err = Read(nil, "/_unknown") | ||
c.Assert(err, NotNil) | ||
|
||
err = Read(strings.NewReader(""), "") | ||
c.Assert(err, NotNil) | ||
|
||
err = Read(strings.NewReader(""), "/_unknown") | ||
c.Assert(err, NotNil) | ||
|
||
err = Read(strings.NewReader(""), s.Dir+"/test") | ||
c.Assert(err, NotNil) | ||
} |
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.