Browse Source

Add Tar archive functionality

master
Tovi Jaeschke-Rogers 3 years ago
commit
992706d9b7
4 changed files with 277 additions and 0 deletions
  1. +129
    -0
      Archive/Archive.go
  2. +111
    -0
      Archive/Unarchive.go
  3. +24
    -0
      Client/Database/Init.go
  4. +13
    -0
      Client/main.go

+ 129
- 0
Archive/Archive.go View File

@ -0,0 +1,129 @@
package Archive
import (
"archive/tar"
"compress/gzip"
"io"
"os"
"path/filepath"
"strings"
)
func Gzip(source, target string) error {
var (
reader, writer *os.File
archiver *gzip.Writer
filename string
e error
)
reader, e = os.Open(source)
if e != nil {
return e
}
//filename = filepath.Base(source)
//target = filepath.Join(target, fmt.Sprintf("%s.gz", filename))
writer, e = os.Create(target)
if e != nil {
return e
}
defer writer.Close()
archiver = gzip.NewWriter(writer)
archiver.Name = filename
defer archiver.Close()
_, e = io.Copy(archiver, reader)
return e
}
func Tar(source, target string) error {
var (
tarfile, file *os.File
tarball *tar.Writer
info os.FileInfo
header *tar.Header
e error
)
tarfile, e = os.Create(target)
if e != nil {
return e
}
defer tarfile.Close()
tarball = tar.NewWriter(tarfile)
defer tarball.Close()
info, e = os.Stat(source)
if e != nil {
return e
}
var baseDir string
if info.IsDir() {
baseDir = filepath.Base(source)
}
return filepath.Walk(source,
func(path string, info os.FileInfo, e error) error {
if e != nil {
return e
}
header, e = tar.FileInfoHeader(info, info.Name())
if e != nil {
return e
}
if baseDir != "" {
header.Name = filepath.Join(
baseDir,
strings.TrimPrefix(
strings.Join(strings.Split(path, "/")[1:], "/"),
source,
),
)
}
e = tarball.WriteHeader(header)
if e != nil {
return e
}
if info.IsDir() {
return nil
}
file, e = os.Open(path)
if e != nil {
return e
}
defer file.Close()
_, e = io.Copy(tarball, file)
return e
})
}
func TarGzip(source, target string) error {
var (
tarPath string = strings.ReplaceAll(target, ".gz", "")
e error
)
e = Tar(source, tarPath)
if e != nil {
return e
}
e = Gzip(tarPath, target)
if e != nil {
return e
}
return os.Remove(tarPath)
}

+ 111
- 0
Archive/Unarchive.go View File

@ -0,0 +1,111 @@
package Archive
import (
"archive/tar"
"compress/gzip"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
)
func UnGzip(source, target string) error {
var (
reader, writer *os.File
archive *gzip.Reader
e error
)
reader, e = os.Open(source)
if e != nil {
return e
}
defer reader.Close()
archive, e = gzip.NewReader(reader)
if e != nil {
return e
}
defer archive.Close()
target = filepath.Join(target, archive.Name)
writer, e = os.Create(target)
if e != nil {
return e
}
defer writer.Close()
_, e = io.Copy(writer, archive)
return e
}
func Untar(tarball, target string) error {
var (
reader *os.File
tarReader *tar.Reader
header *tar.Header
info fs.FileInfo
file *os.File
path string
e error
)
reader, e = os.Open(tarball)
if e != nil {
return e
}
defer reader.Close()
tarReader = tar.NewReader(reader)
for {
header, e = tarReader.Next()
if e == io.EOF {
break
}
if e != nil {
return e
}
path = "/" + strings.Join(strings.Split(header.Name, "/")[1:], "/")
info = header.FileInfo()
if info.IsDir() {
e = os.MkdirAll(path, info.Mode())
if e != nil {
return e
}
continue
}
file, e = os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, info.Mode())
if e != nil {
return e
}
defer file.Close()
_, e = io.Copy(file, tarReader)
if e != nil {
return e
}
}
return nil
}
func UntarGzip(source, target string) error {
var (
tarPath string = strings.ReplaceAll(source, ".gz", "")
e error
)
e = UnGzip(source, tarPath)
if e != nil {
return e
}
return Untar(tarPath, target)
}

+ 24
- 0
Client/Database/Init.go View File

@ -0,0 +1,24 @@
package Database
import (
"database/sql"
"log"
_ "github.com/mattn/go-sqlite3"
)
var (
DB *sql.DB
)
func init() {
var e error
DB, e = sql.Open("sqlite3", "./foo.db")
if e != nil {
panic(e)
}
}
func InitDB() {
log.Println("Initialising Database...")
}

+ 13
- 0
Client/main.go View File

@ -0,0 +1,13 @@
package main
import (
"fmt"
"PackageManager/Archive"
)
func main() {
//e := Archive.TarGzip("/tmp/test", "/tmp/test.tar.gz")
e := Archive.UntarGzip("/tmp/test.tar.gz", "/tmp/test")
fmt.Println(e)
}

Loading…
Cancel
Save