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

Lines changed: 74 additions & 21 deletions
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

Lines changed: 4 additions & 4 deletions
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.

0 commit comments

Comments
 (0)