Skip to content

Commit

Permalink
make superfile can extract almost any compressed file
Browse files Browse the repository at this point in the history
  • Loading branch information
yorukot committed May 8, 2024
1 parent 80ef0f1 commit e57cb78
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 106 deletions.
14 changes: 14 additions & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,23 @@ require (
)

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/bodgit/plumbing v1.3.0 // indirect
github.com/bodgit/sevenzip v1.4.0 // indirect
github.com/bodgit/windows v1.0.1 // indirect
github.com/connesc/cipherio v0.2.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/kdomanski/iso9660 v0.3.3 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/nwaples/rardecode v1.1.3 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect
golift.io/xtractr v0.2.2 // indirect
)

require (
Expand Down
263 changes: 263 additions & 0 deletions src/go.sum

Large diffs are not rendered by default.

126 changes: 48 additions & 78 deletions src/internal/file_operations_extract.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package internal

import (
"archive/tar"
"archive/zip"
"compress/gzip"
"fmt"
"io"
"os"
Expand All @@ -12,8 +10,55 @@ import (

"github.com/charmbracelet/bubbles/progress"
"github.com/lithammer/shortuuid"
"golift.io/xtractr"
)

func extractCompressFile(src, dest string) error {
id := shortuuid.New()

prog := progress.New(generateGradientColor())
prog.PercentageStyle = footerStyle

p := process{
name: "󰛫 unzip file",
progress: prog,
state: inOperation,
total: 1,
done: 0,
}

channel <- channelMessage{
messageId: id,
processNewState: p,
}

x := &xtractr.XFile{
FilePath: src,
OutputDir: dest,
}

_, _, _, err := xtractr.ExtractFile(x)

if err != nil {
p.state = successful
channel <- channelMessage{
messageId: id,
processNewState: p,
}
return err
}

p.state = successful
p.done = 1

channel <- channelMessage{
messageId: id,
processNewState: p,
}

return nil
}

// Extract zip file
func unzip(src, dest string) error {
id := shortuuid.New()
Expand All @@ -32,7 +77,7 @@ func unzip(src, dest string) error {
prog.PercentageStyle = footerStyle
// channel message
p := process{
name: "unzip file",
name: "󰛫 unzip file",
progress: prog,
state: inOperation,
total: totalFiles,
Expand Down Expand Up @@ -115,80 +160,5 @@ func unzip(src, dest string) error {
processNewState: p,
}

return nil
}

// Extract gzip file
func ungzip(input, output string) error {
var err error
input, err = filepath.Abs(input)
if err != nil {
return err
}
output, err = filepath.Abs(output)
if err != nil {
return err
}

inputFile, err := os.Open(input)
if err != nil {
return err
}
defer inputFile.Close()

gzReader, err := gzip.NewReader(inputFile)
if err != nil {
return err
}
defer gzReader.Close()

err = os.MkdirAll(output, 0755)
if err != nil {
return err
}

tarReader := tar.NewReader(gzReader)
for {
header, err := tarReader.Next()

if err == io.EOF {
break
}
if err != nil {
return err
}

if !filepath.IsAbs(header.Name) {
return fmt.Errorf("unsanitized archive entry with relative path")
}
targetPath := filepath.Join(output, header.Name)

fileInfo := header.FileInfo()
if fileInfo.IsDir() {
err = os.MkdirAll(targetPath, fileInfo.Mode())
if err != nil {
return err
}
continue
}

targetFile, err := os.Create(targetPath)
if err != nil {
return err
}

_, err = io.Copy(targetFile, tarReader)
if err != nil {
targetFile.Close()
return err
}

err = targetFile.Close()
if err != nil {
return err
}

}

return nil
}
14 changes: 5 additions & 9 deletions src/internal/handle_file_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ func cutMultipleItem(m model) model {
return m
}


// Paste all clipboard items
func pasteItem(m model) model {
id := shortuuid.New()
Expand All @@ -519,7 +518,6 @@ func pasteItem(m model) model {
prog := progress.New(generateGradientColor())
prog.PercentageStyle = footerStyle


prefixIcon := "󰆏 "
if m.copyItems.cut {
prefixIcon = "󰆐 "
Expand Down Expand Up @@ -606,16 +604,14 @@ func extractFile(m model) model {
os.MkdirAll(outputDir, 0755)
err = unzip(panel.element[panel.cursor].location, outputDir)
if err != nil {
outPutLog(err)
outPutLog("Error extract file,", err)
}
case ".tar", ".gz":
default:
os.MkdirAll(outputDir, 0755)
err = ungzip(panel.element[panel.cursor].location, outputDir)
err = extractCompressFile(panel.element[panel.cursor].location, outputDir)
if err != nil {
outPutLog(err)
outPutLog("Error extract file,", err)
}
default:
return m
}
m.fileModel.filePanels[m.filePanelFocusIndex] = panel
return m
Expand Down Expand Up @@ -665,4 +661,4 @@ func openDirectoryWithEditor(m model) tea.Cmd {
return tea.ExecProcess(c, func(err error) tea.Msg {
return editorFinishedMsg{err}
})
}
}
10 changes: 0 additions & 10 deletions src/internal/handle_panel_movement.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,6 @@ func searchBarFocus(m model) model {
return m
}

func commandLine(m model) model {
if m.commandLine.open {
m.commandLine.open = false
}

m.commandLine.open = true

return m
}

// ======================================== File panel controller ========================================

// Control file panel list up
Expand Down
3 changes: 0 additions & 3 deletions src/internal/key_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,6 @@ func normalAndBrowserModeKey(msg string, m model) model {
m = panelItemRename(m)
case hotkeys.SearchBar[0], hotkeys.SearchBar[1]:
m = searchBarFocus(m)

case hotkeys.CommandLine[0], hotkeys.CommandLine[1]:
m = commandLine(m)
}
return m
}
Expand Down
6 changes: 0 additions & 6 deletions src/internal/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ type model struct {
warnModal warnModal
helpMenu helpMenuModal
fileMetaData fileMetadata
commandLine commandLineModal
firstTextInput bool
toggleDotFile bool
editorMode bool
Expand All @@ -93,11 +92,6 @@ type model struct {
}

// Modal
type commandLineModal struct {
open bool
commandLine textinput.Model
}

type helpMenuModal struct {
height int
width int
Expand Down

0 comments on commit e57cb78

Please sign in to comment.