Skip to content

Commit

Permalink
feat: 合并gz和tgz
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Oct 27, 2024
1 parent e3ad36e commit 530ed71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
13 changes: 5 additions & 8 deletions pkg/io/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ type FormatArchive string

const (
Zip FormatArchive = "zip"
Gz FormatArchive = "gz"
Bz2 FormatArchive = "bz2"
Tar FormatArchive = "tar"
TarGz FormatArchive = "tar.gz"
TGz FormatArchive = "tgz"
Xz FormatArchive = "xz"
SevenZip FormatArchive = "7z"
)
Expand Down Expand Up @@ -49,7 +48,7 @@ func Compress(dir string, src []string, dst string) error {
switch format {
case Zip:
cmd = exec.Command("zip", append([]string{"-qr", "-o", dst}, src...)...)
case Gz, TarGz:
case TGz:
cmd = exec.Command("tar", append([]string{"-czf", dst}, src...)...)
case Bz2:
cmd = exec.Command("tar", append([]string{"-cjf", dst}, src...)...)
Expand Down Expand Up @@ -86,7 +85,7 @@ func UnCompress(src string, dst string) error {
switch format {
case Zip:
cmd = exec.Command("unzip", "-qo", src, "-d", dst)
case Gz, TarGz:
case TGz:
cmd = exec.Command("tar", "-xzf", src, "-C", dst)
case Bz2:
cmd = exec.Command("tar", "-xjf", src, "-C", dst)
Expand All @@ -108,14 +107,12 @@ func formatArchiveByPath(path string) (FormatArchive, error) {
switch filepath.Ext(path) {
case ".zip":
return Zip, nil
case ".gz":
return Gz, nil
case ".bz2":
return Bz2, nil
case ".tar":
return Tar, nil
case ".tar.gz", ".tgz":
return TarGz, nil
case ".gz", ".tar.gz", ".tgz":
return TGz, nil
case ".xz":
return Xz, nil
case ".7z":
Expand Down
26 changes: 12 additions & 14 deletions pkg/io/io_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io

import (
"github.com/go-rat/utils/debug"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -64,12 +63,12 @@ func (s *IOTestSuite) TestCompress() {
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.zip"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.gz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.bz2"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.tar"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.gz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.tar.gz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "compress_test.tgz"))
Expand All @@ -93,12 +92,12 @@ func (s *IOTestSuite) TestUnCompress() {
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.zip"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.gz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.bz2"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.tar"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.gz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.tar.gz"))
s.NoError(err)
err = Compress(abs, src, filepath.Join(abs, "uncompress_test.tgz"))
Expand All @@ -116,15 +115,6 @@ func (s *IOTestSuite) TestUnCompress() {
data, err = Read("testdata/uncompressed_zip/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.gz"), filepath.Join(abs, "uncompressed_gz"))
s.NoError(err)
debug.Dump(ReadDir("testdata/uncompressed_gz"))
data, err = Read("testdata/uncompressed_gz/uncompress_test1.txt")
s.NoError(err)
s.Equal("File 1", data)
data, err = Read("testdata/uncompressed_gz/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.bz2"), filepath.Join(abs, "uncompressed_bz2"))
s.NoError(err)
data, err = Read("testdata/uncompressed_bz2/uncompress_test1.txt")
Expand All @@ -141,6 +131,14 @@ func (s *IOTestSuite) TestUnCompress() {
data, err = Read("testdata/uncompressed_tar/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.gz"), filepath.Join(abs, "uncompressed_gz"))
s.NoError(err)
data, err = Read("testdata/uncompressed_gz/uncompress_test1.txt")
s.NoError(err)
s.Equal("File 1", data)
data, err = Read("testdata/uncompressed_gz/uncompress_test2.txt")
s.NoError(err)
s.Equal("File 2", data)
err = UnCompress(filepath.Join(abs, "uncompress_test.tar.gz"), filepath.Join(abs, "uncompressed_tar_gz"))
s.NoError(err)
data, err = Read("testdata/uncompressed_tar_gz/uncompress_test1.txt")
Expand Down

0 comments on commit 530ed71

Please sign in to comment.