Skip to content

Commit 3281512

Browse files
committed
refactor: Update version information in resources.rc file
1 parent 53a3fd9 commit 3281512

File tree

11 files changed

+106
-51
lines changed

11 files changed

+106
-51
lines changed

compressor/compressor.go

+74-21
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ import (
88
"os"
99
"path"
1010
"sync"
11+
"path/filepath"
1112

1213
"file-compressor/utils"
1314
)
1415

15-
// Compress compresses the specified files into a single compressed file.
16+
// Compress compresses the specified files or folders into a single compressed file.
1617
func Compress(filenameStrs []string, outputDir *string, password *string) error {
17-
// Check if there are files to compress
18+
// Check if there are files or folders to compress
1819
if len(filenameStrs) == 0 {
19-
return errors.New("no files to compress")
20+
return errors.New("no files or folders to compress")
2021
}
2122

2223
// Set default output directory if not provided
@@ -34,30 +35,39 @@ func Compress(filenameStrs []string, outputDir *string, password *string) error
3435
// Channel to receive errors from goroutines
3536
errChan := make(chan error, len(filenameStrs))
3637

37-
// Read files and store their content concurrently
38+
// Process each input file or folder
3839
for _, filename := range filenameStrs {
3940
wg.Add(1)
4041
go func(filename string) {
4142
defer wg.Done()
4243

43-
// Check if the file exists
44-
if _, err := os.Stat(filename); os.IsNotExist(err) {
45-
errChan <- fmt.Errorf("file does not exist: %s", filename)
44+
// Check if the file or folder exists
45+
info, err := os.Stat(filename)
46+
if os.IsNotExist(err) {
47+
errChan <- fmt.Errorf("file or folder does not exist: %s", filename)
4648
return
4749
}
4850

49-
// Read file content
50-
content, err := os.ReadFile(filename)
51-
if err != nil {
52-
errChan <- fmt.Errorf("failed to read file %s: %w", filename, err)
53-
return
51+
// Handle directory recursively
52+
if info.IsDir() {
53+
err := compressFolderRecursive(filename, &files)
54+
if err != nil {
55+
errChan <- err
56+
}
57+
} else {
58+
// Read file content
59+
content, err := os.ReadFile(filename)
60+
if err != nil {
61+
errChan <- fmt.Errorf("failed to read file %s: %w", filename, err)
62+
return
63+
}
64+
65+
// Store file information (name and content)
66+
files = append(files, utils.File{
67+
Name: path.Base(filename),
68+
Content: content,
69+
})
5470
}
55-
56-
// Store file information (name and content)
57-
files = append(files, utils.File{
58-
Name: path.Base(filename),
59-
Content: content,
60-
})
6171
}(filename)
6272
}
6373

@@ -95,8 +105,42 @@ func Compress(filenameStrs []string, outputDir *string, password *string) error
95105
return nil
96106
}
97107

108+
// Function to recursively compress a folder and its contents
109+
func compressFolderRecursive(folderPath string, files *[]utils.File) error {
110+
// Traverse the folder contents
111+
err := filepath.Walk(folderPath, func(filePath string, info os.FileInfo, err error) error {
112+
if err != nil {
113+
return err
114+
}
115+
if !info.IsDir() {
116+
// Read file content
117+
content, err := os.ReadFile(filePath)
118+
if err != nil {
119+
return fmt.Errorf("failed to read file %s: %w", filePath, err)
120+
}
121+
122+
filename, err := filepath.Rel(folderPath, filePath)
123+
if err != nil {
124+
return fmt.Errorf("failed to get relative path for file %s: %w", filePath, err)
125+
}
126+
127+
// Store file information (relative path and content)
128+
*files = append(*files, utils.File{
129+
Name: filepath.ToSlash(filename), // Store relative path
130+
Content: content,
131+
})
132+
}
133+
return nil
134+
})
135+
136+
if err != nil {
137+
return fmt.Errorf("error compressing folder %s: %w", folderPath, err)
138+
}
98139

99-
// Decompress decompresses the specified compressed file into individual files.
140+
return nil
141+
}
142+
143+
// Decompress decompresses the specified compressed file into individual files or folders.
100144
func Decompress(filenameStrs []string, outputDir *string, password *string) error {
101145
// Check if there are files to decompress
102146
if len(filenameStrs) == 0 {
@@ -135,13 +179,22 @@ func Decompress(filenameStrs []string, outputDir *string, password *string) erro
135179
// Channel to receive errors from goroutines
136180
errChan := make(chan error, len(files))
137181

138-
// Write decompressed files to the output directory concurrently
182+
// Process each decompressed file
139183
for _, file := range files {
140184
wg.Add(1)
141185
go func(file utils.File) {
142186
defer wg.Done()
143187

144-
err := os.WriteFile(*outputDir+"/"+file.Name, file.Content, 0644)
188+
// Create directories if they don't exist
189+
outputPath := filepath.Join(*outputDir, filepath.Dir(file.Name))
190+
err := os.MkdirAll(outputPath, os.ModePerm)
191+
if err != nil {
192+
errChan <- fmt.Errorf("failed to create directory %s: %w", outputPath, err)
193+
return
194+
}
195+
196+
// Write decompressed file content
197+
err = os.WriteFile(filepath.Join(*outputDir, file.Name), file.Content, 0644)
145198
if err != nil {
146199
errChan <- fmt.Errorf("failed to write decompressed file %s: %w", file.Name, err)
147200
return

resources.rc

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
IDI_ICON1 ICON "icon.ico"
44

55
1 VERSIONINFO
6-
FILEVERSION 0,0,3
7-
PRODUCTVERSION 0,0,3
6+
FILEVERSION 0,0,4
7+
PRODUCTVERSION 0,0,4
88
FILEOS 0x4
99
FILETYPE 0x1
1010
{
@@ -14,12 +14,12 @@ FILETYPE 0x1
1414
{
1515
VALUE "CompanyName", "BrainbirdLab"
1616
VALUE "FileDescription", "File Compress-Encryption program"
17-
VALUE "FileVersion", "0.0.3"
17+
VALUE "FileVersion", "0.0.4"
1818
VALUE "InternalName", "piarch"
1919
VALUE "LegalCopyright", "BrainbirdLab"
2020
VALUE "OriginalFilename", "piarch"
2121
VALUE "ProductName", "piarch"
22-
VALUE "ProductVersion", "0.0.3"
22+
VALUE "ProductVersion", "0.0.4"
2323
VALUE "Author", "Fuad Hasan"
2424
}
2525
}

resources.syso

0 Bytes
Binary file not shown.

test/unzip/compressed.bin

-38.9 KB
Binary file not shown.

test/unzip/files.pcd

-87 Bytes
Binary file not shown.
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/unzip/test/zip/compressed.bin

38.9 KB
Binary file not shown.

test/zip/compressed.bin

51.2 KB
Binary file not shown.

utils/cli.go

+28-26
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,71 @@
1+
//parseCLI.go
2+
13
package utils
24

35
import (
46
"flag"
57
"os"
8+
"path/filepath"
69
"strings"
710
)
811

9-
1012
func ParseCLI() ([]string, *string, *string, *bool) {
11-
//cli arguments
12-
13-
//if -v or --version is passed then skip the other flags
13+
// CLI arguments
1414
version := flag.Bool("v", false, "Print version")
15-
inputFiles := flag.String("i", "", "Input files to be compressed")
15+
inputFiles := flag.String("i", "", "Input files or directory to be compressed")
1616
outputDir := flag.String("o", "", "Output directory for compressed files (Optional)")
1717
password := flag.String("p", "", "Password for encryption (Optional)")
18-
readAllFiles := flag.Bool("a", false, "Read all files in the test directory")
18+
readAllFiles := flag.Bool("a", false, "Read all files in the input directory")
1919
decompressMode := flag.Bool("d", false, "Decompress mode")
2020
flag.Parse()
2121

2222
if *version {
2323
ColorPrint(WHITE, "---------- PI ARCHIVER ----------\n")
2424
ColorPrint(YELLOW, "Version: v0.0.2\n")
25-
//dev info
25+
// dev info
2626
ColorPrint(WHITE, "Developed by: https://github.com/itsfuad/\n")
2727
ColorPrint(WHITE, "---------------------------------")
2828
os.Exit(0)
2929
}
3030

3131
if *inputFiles == "" {
32-
ColorPrint(RED, "No input files provided\n")
32+
ColorPrint(RED, "No input files or directory provided\n")
3333
flag.Usage()
3434
os.Exit(1)
3535
}
3636

37-
// Read all files
3837
var filenameStrs []string
3938

40-
// Split input files by comma and trim spaces and quotes(if any, `'` | `"`)
41-
39+
// Handle reading all files in the input directory
4240
if *readAllFiles {
43-
44-
//filenameStrs is the folder name
45-
dir := inputFiles
46-
//if dir exists
47-
if _, err := os.Stat(*dir); os.IsNotExist(err) {
48-
ColorPrint(RED, "Directory does not exist.\n")
41+
// Check if input is a directory
42+
info, err := os.Stat(*inputFiles)
43+
if os.IsNotExist(err) {
44+
ColorPrint(RED, "Input directory does not exist.\n")
4945
os.Exit(1)
5046
}
5147

52-
//read all filenames in the directory
53-
files, err := os.ReadDir(*dir)
54-
if err != nil {
55-
ColorPrint(RED, err.Error()+"\n")
48+
if !info.IsDir() {
49+
ColorPrint(RED, "Provided input is not a directory.\n")
5650
os.Exit(1)
5751
}
5852

59-
for _, file := range files {
60-
if file.IsDir() {
61-
continue
53+
// Read all files in the directory
54+
err = filepath.Walk(*inputFiles, func(path string, info os.FileInfo, err error) error {
55+
if err != nil {
56+
return err
6257
}
63-
64-
filenameStrs = append(filenameStrs, *dir+"/"+file.Name())
58+
if !info.IsDir() {
59+
filenameStrs = append(filenameStrs, path)
60+
}
61+
return nil
62+
})
63+
if err != nil {
64+
ColorPrint(RED, err.Error())
65+
os.Exit(1)
6566
}
6667
} else {
68+
// Split input files by comma and trim spaces and quotes (if any)
6769
for _, filename := range strings.Split(*inputFiles, ",") {
6870
filenameStrs = append(filenameStrs, strings.Trim(filename, " '\""))
6971
}

0 commit comments

Comments
 (0)