Skip to content

Commit

Permalink
Restore previous gunzip logic to preserve original filename
Browse files Browse the repository at this point in the history
  • Loading branch information
frodenas committed Jan 4, 2019
1 parent b518cc2 commit 1523853
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions in/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package in

import (
"bufio"
"fmt"
"compress/gzip"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -77,22 +76,33 @@ func unpackZip(sourcePath, destinationDir string) error {
}

func unpackGzip(sourcePath string) (string, error) {
cmd := exec.Command("gunzip", sourcePath)
err := cmd.Run()
reader, err := os.Open(sourcePath)
if err != nil {
return "", err
}
defer reader.Close()

destinationDir := filepath.Dir(sourcePath)
fileInfos, err := ioutil.ReadDir(destinationDir)
archive, err := gzip.NewReader(reader)
if err != nil {
return "", fmt.Errorf("failed to read dir: %s", err)
return "", err
}
defer archive.Close()

var destinationPath string
if archive.Name != "" {
destinationPath = filepath.Join(filepath.Dir(sourcePath), archive.Name)
} else {
destinationPath = sourcePath + ".uncompressed"
}
if len(fileInfos) != 1 {
return "", fmt.Errorf("%d files found after gunzip; expected 1", len(fileInfos))

writer, err := os.Create(destinationPath)
if err != nil {
return "", err
}
defer writer.Close()

return filepath.Join(destinationDir, fileInfos[0].Name()), err
_, err = io.Copy(writer, archive)
return destinationPath, err
}

func unpackTar(sourcePath, destinationDir string) error {
Expand Down

0 comments on commit 1523853

Please sign in to comment.