Skip to content

Commit 60c490a

Browse files
committed
update zip file
1 parent 44236f4 commit 60c490a

File tree

3 files changed

+148
-14
lines changed

3 files changed

+148
-14
lines changed

Diff for: src/components/function.go

+132-13
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
"os"
1111
"path/filepath"
1212
"regexp"
13+
"runtime"
1314
"sort"
1415
"strconv"
1516
"strings"
1617
"time"
17-
"runtime"
1818

1919
"github.com/barasher/go-exiftool"
2020
"github.com/charmbracelet/bubbles/progress"
@@ -46,14 +46,14 @@ func getWellKnownDirectories() []directory {
4646
{location: userdirs.PublicShare, name: " PublicShare"},
4747
}
4848

49-
if runtime.GOOS == "darwin" {
50-
wellKnownDirectories[1].location = HomeDir+"/Downloads/"
51-
wellKnownDirectories[2].location = HomeDir+"/Documents/"
52-
wellKnownDirectories[3].location = HomeDir+"/Pictures/"
53-
wellKnownDirectories[4].location = HomeDir+"/Movies/"
54-
wellKnownDirectories[5].location = HomeDir+"/Music/"
55-
wellKnownDirectories[7].location = HomeDir+"/Public/"
56-
}
49+
if runtime.GOOS == "darwin" {
50+
wellKnownDirectories[1].location = HomeDir + "/Downloads/"
51+
wellKnownDirectories[2].location = HomeDir + "/Documents/"
52+
wellKnownDirectories[3].location = HomeDir + "/Pictures/"
53+
wellKnownDirectories[4].location = HomeDir + "/Movies/"
54+
wellKnownDirectories[5].location = HomeDir + "/Music/"
55+
wellKnownDirectories[7].location = HomeDir + "/Public/"
56+
}
5757

5858
for _, dir := range wellKnownDirectories {
5959
if _, err := os.Stat(dir.location); !os.IsNotExist(err) {
@@ -546,16 +546,16 @@ func unzip(src, dest string) error {
546546
panic(err)
547547
}
548548
}()
549-
totalFile := len(r.File)
549+
totalFiles := len(r.File)
550550
// progessbar
551551
prog := progress.New(progress.WithScaledGradient(theme.ProcessBarGradient[0], theme.ProcessBarGradient[1]))
552552
prog.PercentageStyle = textStyle
553553
// channel message
554554
p := process{
555-
name: "test",
555+
name: "unzip file",
556556
progress: prog,
557557
state: inOperation,
558-
total: totalFile,
558+
total: totalFiles,
559559
done: 0,
560560
}
561561
if _, err := os.Stat(filepath.Join(dest, filepath.Base(src))); os.IsExist(err) {
@@ -639,8 +639,127 @@ func unzip(src, dest string) error {
639639
}
640640
}
641641

642-
p.total = totalFile
642+
p.total = totalFiles
643+
p.state = successful
644+
channel <- channelMessage{
645+
messageId: id,
646+
processNewState: p,
647+
}
648+
649+
return nil
650+
}
651+
652+
func zipSource(source, target string) error {
653+
id := shortuuid.New()
654+
prog := progress.New(progress.WithScaledGradient(theme.ProcessBarGradient[0], theme.ProcessBarGradient[1]))
655+
prog.PercentageStyle = textStyle
656+
657+
totalFiles, err := countFiles(source)
658+
659+
if err != nil {
660+
outPutLog("Zip file count files error: ", err)
661+
}
662+
663+
p := process{
664+
name: "zip files",
665+
progress: prog,
666+
state: inOperation,
667+
total: totalFiles,
668+
done: 0,
669+
}
670+
671+
_, err = os.Stat(target)
672+
if os.IsExist(err) {
673+
p.name = "󰗄 File already exist"
674+
channel <- channelMessage{
675+
messageId: id,
676+
processNewState: p,
677+
}
678+
return nil
679+
}
680+
681+
// 1. Create a ZIP file and zip.Writer
682+
f, err := os.Create(target)
683+
if err != nil {
684+
return err
685+
}
686+
defer f.Close()
687+
688+
writer := zip.NewWriter(f)
689+
defer writer.Close()
690+
691+
// 2. Go through all the files of the source
692+
err = filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
693+
p.name = filepath.Base(path)
694+
if len(channel) < 5 {
695+
channel <- channelMessage{
696+
messageId: id,
697+
processNewState: p,
698+
}
699+
}
700+
701+
if err != nil {
702+
return err
703+
}
704+
705+
// 3. Create a local file header
706+
header, err := zip.FileInfoHeader(info)
707+
if err != nil {
708+
return err
709+
}
710+
711+
// set compression
712+
header.Method = zip.Deflate
713+
714+
// 4. Set relative path of a file as the header name
715+
header.Name, err = filepath.Rel(filepath.Dir(source), path)
716+
if err != nil {
717+
return err
718+
}
719+
if info.IsDir() {
720+
header.Name += "/"
721+
}
722+
723+
// 5. Create writer for the file header and save content of the file
724+
headerWriter, err := writer.CreateHeader(header)
725+
if err != nil {
726+
return err
727+
}
728+
729+
if info.IsDir() {
730+
return nil
731+
}
732+
733+
f, err := os.Open(path)
734+
if err != nil {
735+
return err
736+
}
737+
defer f.Close()
738+
739+
_, err = io.Copy(headerWriter, f)
740+
if err != nil {
741+
return err
742+
}
743+
p.done++
744+
if len(channel) < 5 {
745+
channel <- channelMessage{
746+
messageId: id,
747+
processNewState: p,
748+
}
749+
}
750+
return nil
751+
})
752+
753+
if err != nil {
754+
outPutLog("Error while zip file:", err)
755+
p.state = failure
756+
channel <- channelMessage{
757+
messageId: id,
758+
processNewState: p,
759+
}
760+
}
643761
p.state = successful
762+
p.done = totalFiles
644763
channel <- channelMessage{
645764
messageId: id,
646765
processNewState: p,

Diff for: src/components/globalController.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"path"
77
"path/filepath"
8+
"strings"
89
"time"
910

1011
"github.com/charmbracelet/bubbles/progress"
@@ -451,4 +452,14 @@ func extractFile(m model) model {
451452
unzip(panel.element[panel.cursor].location, filepath.Dir(panel.element[panel.cursor].location))
452453
m.fileModel.filePanels[m.filePanelFocusIndex] = panel
453454
return m
454-
}
455+
}
456+
457+
func compressFile(m model) model {
458+
panel := m.fileModel.filePanels[m.filePanelFocusIndex]
459+
fileName := filepath.Base(panel.element[panel.cursor].location)
460+
461+
zipName := strings.TrimSuffix(fileName, filepath.Ext(fileName)) + ".zip"
462+
zipSource(panel.element[panel.cursor].location, filepath.Join(filepath.Dir(panel.element[panel.cursor].location), zipName))
463+
m.fileModel.filePanels[m.filePanelFocusIndex] = panel
464+
return m
465+
}

Diff for: src/components/model.go

+4
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,10 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
227227
go func() {
228228
m = extractFile(m)
229229
}()
230+
case Config.CompressFile[0], Config.CompressFile[1]:
231+
go func() {
232+
m = compressFile(m)
233+
}()
230234
default:
231235
// check if it's the select mode
232236
if m.fileModel.filePanels[m.filePanelFocusIndex].focusType == focus && m.fileModel.filePanels[m.filePanelFocusIndex].panelMode == selectMode {

0 commit comments

Comments
 (0)