Skip to content

Commit

Permalink
chore: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ansidev committed May 20, 2023
0 parents commit fefe84b
Show file tree
Hide file tree
Showing 13 changed files with 984 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# These are supported funding model platforms

github: [ansidev]
patreon: ansidev
# open_collective: # Replace with a single Open Collective username
# ko_fi: # Replace with a single Ko-fi username
# tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
# community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
# liberapay: # Replace with a single Liberapay username
# issuehunt: # Replace with a single IssueHunt username
# otechie: # Replace with a single Otechie username
# lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom:
- https://paypal.me/ansidev
- https://buymeacoffee.com/ansidev
- https://me.momo.vn/ansidev
38 changes: 38 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Go CI
on:
push:
tags:
- v*
branches:
- main
pull_request:
permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read
jobs:
golangci:
strategy:
matrix:
os: [macos-latest, windows-latest, ubuntu-latest]
go-version: ['1.18', '1.19', '1.20']
name: lint
runs-on: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Go environment
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}

- name: Run golangci-lint
uses: golangci/golangci-lint-action@v3
with:
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version.
version: v1.52
args: --timeout=3m

- name: Test
run: go test -v ./...
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# IDE and code editor directories
.vscode/
.idea/
.fleet/

bin/
661 changes: 661 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
prepare:
@echo "Installing golangci-lint"
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
# @echo "Install Husky"
# @go install github.com/go-courier/husky/cmd/husky@latest && husky init

dependency:
@go get -v ./...

test: dependency
@go test ./...

coverage: dependency
@go test -cover ./...

.PHONY: prepare dependency test coverage
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# tar-xz

## Introduction

A simple Go module for decompressing a tar.xz file.

## Example

```go
package main

import (
tarXz "github.com/ansidev/tar-xz"
)

func main() {
err := tarXz.Decompress("archive.tar.xz", "/path/to/output/dir")
if err != nil {
log.Fatal(err)
}
}
```

## Contact

Le Minh Tri [@ansidev](https://ansidev.xyz/about).

## License

This source code is available under the [AGPL-3.0 LICENSE](/LICENSE).
65 changes: 65 additions & 0 deletions decompress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package main

import (
"archive/tar"
"bytes"
"io"
"os"
"path/filepath"

"github.com/xi2/xz"
)

func Decompress(filePath string, destDir string) error {
f, err := os.ReadFile(filePath)
if err != nil {
return err
}

// Create an xz Reader to read the compressed data
xr, err := xz.NewReader(bytes.NewReader(f), 0)
if err != nil {
return err
}

// Open and iterate through the files in the archive.
tr := tar.NewReader(xr)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}

if err != nil {
return err
}

destFilePath := filepath.Join(destDir, hdr.Name)
destFileParentDir := filepath.Dir(destFilePath)

// Create the parent directory path if it does not exist
err = os.MkdirAll(destFileParentDir, 0755)
if err != nil {
return err
}

if hdr.Typeflag == tar.TypeDir {
continue
}

if hdr.Typeflag == tar.TypeReg {
destFile, err := os.Create(destFilePath)
if err != nil {
return err
}

defer destFile.Close()

if _, err := io.Copy(destFile, tr); err != nil {
return err
}
}
}

return nil
}
108 changes: 108 additions & 0 deletions decompress_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"crypto/sha256"
"fmt"
"os"
"path/filepath"
"testing"
)

const testDataDir = "testdata"
const testFileHash string = "dc8897816b30ddee02e83f4d285916731d7940d8b6fc03aab4e71185649034fd"

func getSHA256Checksum(filePath string) (string, error) {
buf, err := os.ReadFile(filePath)
if err != nil {
return "", err
}

return fmt.Sprintf("%x", sha256.Sum256(buf)), nil
}

func removeDir(t *testing.T, dirPath string) {
err := os.RemoveAll(dirPath)
if err != nil {
t.Errorf("Failed to remove the directory: %v", err)
}
}

func compareFileHash(t *testing.T, actualDirPath string, expectedDirPath string, verifyPaths []string) {
for _, path := range verifyPaths {
actualFilePath := filepath.Join(actualDirPath, path)
expectedFilePath := filepath.Join(expectedDirPath, path)

actualFileHash, err := getSHA256Checksum(actualFilePath)
if err != nil {
t.Errorf("Failed to get SHA256 checksum of the actual file: %v", err)
}

expectedFileHash, err := getSHA256Checksum(expectedFilePath)
if err != nil {
t.Errorf("Failed to get SHA256 checksum of the expected file: %v", err)
}

if actualFileHash != expectedFileHash {
t.Errorf("Mismatched checksum. Got: %v, want: %v", actualFileHash, expectedFileHash)
}
}
}

func testGetSHA256Checksum(t *testing.T) {
sum, err := getSHA256Checksum(filepath.Join(testDataDir, "sample.tar.xz"))
if err != nil {
t.Errorf("Internal function getSHA256Checksum() error: %v", err)
}

if sum != testFileHash {
t.Errorf("Unmatched test file checksum. Got: %v, want: %v", sum, testFileHash)
}
}

func TestDecompress(t *testing.T) {
type args struct {
filePath string
destDir string
verifyPaths []string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Decompress sample.tar.xz to ./sample_test",
args: args{
filePath: filepath.Join(testDataDir, "sample.tar.xz"),
destDir: filepath.Join(testDataDir, "sample_test"),
verifyPaths: []string{
filepath.Join("sample", "MIT_LICENSE"),
filepath.Join("sample", "MIT_LICENSE.pdf"),
},
},
wantErr: false,
},
}

// Test getSHA256Checksum first
testGetSHA256Checksum(t)

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Remove the directory if exists before running the test case
if _, err := os.Stat(tt.args.destDir); !os.IsNotExist(err) {
removeDir(t, tt.args.destDir)
}
err := Decompress(tt.args.filePath, tt.args.destDir)

// Remove the directory after running the test case
defer removeDir(t, tt.args.destDir)

if (err != nil) != tt.wantErr {
t.Errorf("Decompress() error = %v, wantErr %v", err, tt.wantErr)
} else {
compareFileHash(t, testDataDir, tt.args.destDir, tt.args.verifyPaths)
}
})
}
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/ansidev/tar-xz

go 1.19

require github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 h1:nIPpBwaJSVYIxUFsDv3M8ofmx9yWTog9BfvIu0q41lo=
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMxjDjgmT5uz5wzYJKVo23qUhYTos=
Binary file added testdata/sample.tar.xz
Binary file not shown.
21 changes: 21 additions & 0 deletions testdata/sample/MIT_LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) [year] [fullname]

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.
Binary file added testdata/sample/MIT_LICENSE.pdf
Binary file not shown.

0 comments on commit fefe84b

Please sign in to comment.