Skip to content

Commit

Permalink
Merge pull request #3 from essentialkaos/develop
Browse files Browse the repository at this point in the history
Version 1.1.0
  • Loading branch information
andyone authored Jan 5, 2023
2 parents 40dccba + caeb018 commit 551752a
Show file tree
Hide file tree
Showing 29 changed files with 879 additions and 47 deletions.
Binary file added .testdata/data.tar.gz
Binary file not shown.
Binary file added .testdata/data.txt.bz2
Binary file not shown.
Binary file added .testdata/data.txt.gz
Binary file not shown.
Binary file added .testdata/data.txt.xz
Binary file not shown.
Binary file added .testdata/data.txt.zst
Binary file not shown.
Binary file added .testdata/data.zip
Binary file not shown.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ vendor: mod-vendor ## Make vendored copy of dependencies

test: ## Run tests
ifdef COVERAGE_FILE ## Save coverage data into file (String)
go test $(VERBOSE_FLAG) -covermode=count -coverprofile=$(COVERAGE_FILE) ./tar ./tbz ./tgz ./txz ./tzst
go test $(VERBOSE_FLAG) -covermode=count -coverprofile=$(COVERAGE_FILE) ./. ./bz2 ./gz ./tar ./tbz2 ./tgz ./txz ./tzst ./xz ./zip ./zst
else
go test $(VERBOSE_FLAG) -covermode=count ./tar ./tbz ./tgz ./txz ./tzst
go test $(VERBOSE_FLAG) -covermode=count ./. ./bz2 ./gz ./tar ./tbz2 ./tgz ./txz ./tzst ./xz ./zip ./zst
endif

mod-init:
Expand Down
70 changes: 70 additions & 0 deletions bz2/bz2.go
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
}
68 changes: 68 additions & 0 deletions bz2/bz2_test.go
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)
}
77 changes: 77 additions & 0 deletions gz/gz.go
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
}
71 changes: 71 additions & 0 deletions gz/gz_test.go
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)
}
50 changes: 43 additions & 7 deletions npck.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package npck provides methods for unpacking various types of archives
package npck

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -8,19 +9,54 @@ package npck
// ////////////////////////////////////////////////////////////////////////////////// //

import (
"fmt"
"path/filepath"
"strings"

"github.com/essentialkaos/npck/bz2"
"github.com/essentialkaos/npck/gz"
"github.com/essentialkaos/npck/tar"
"github.com/essentialkaos/npck/tbz"
"github.com/essentialkaos/npck/tbz2"
"github.com/essentialkaos/npck/tgz"
"github.com/essentialkaos/npck/txz"
"github.com/essentialkaos/npck/tzst"
"github.com/essentialkaos/npck/xz"
"github.com/essentialkaos/npck/zip"
"github.com/essentialkaos/npck/zst"
)

// ////////////////////////////////////////////////////////////////////////////////// //

func stub() {
tar.Unpack("", "")
tbz.Unpack("", "")
tgz.Unpack("", "")
txz.Unpack("", "")
tzst.Unpack("", "")
// Unpack unpacks given file
func Unpack(file, dir string) error {
ext := filepath.Ext(file)

if strings.Contains(file, ".tar.") {
ext = ".tar" + ext
}

switch ext {
case ".tgz", ".tar.gz":
return tgz.Unpack(file, dir)
case ".tbz2", ".tar.bz2":
return tbz2.Unpack(file, dir)
case ".txz", ".tar.xz":
return txz.Unpack(file, dir)
case ".tzst", ".tar.zst":
return tzst.Unpack(file, dir)
case ".zip":
return zip.Unpack(file, dir)
case ".tar":
return tar.Unpack(file, dir)
case ".gz":
return gz.Unpack(file, dir)
case ".bz2":
return bz2.Unpack(file, dir)
case ".xz":
return xz.Unpack(file, dir)
case ".zst":
return zst.Unpack(file, dir)
}

return fmt.Errorf("Unknown or unsupported archive type")
}
Loading

0 comments on commit 551752a

Please sign in to comment.