Repository navigation
Expand file tree
/
Copy pathzipCompressor.sh
More file actions
186 lines (157 loc) · 4.52 KB
/
Copy pathzipCompressor.sh
File metadata and controls
186 lines (157 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/bash
# Script to compress recursively files and directories in .zip format
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Help
show_help() {
echo -e "${BLUE}=== ZIP COMPRESSOR ===${NC}"
echo ""
echo "Usage: $0 [OPTIONS] <source_directory> [destination_file.zip]"
echo ""
echo "Options:"
echo " -h, --help Show this help"
echo " -v, --verbose Verbose mode (show processed files)"
echo " -p, --password Protect with password"
echo ""
echo "Examples:"
echo " $0 /home/user/documents"
echo " $0 /home/user/documents my_backup.zip"
echo " $0 -v -p 123456 /home/user/documents backup_complete.zip"
echo ""
}
# Check dependencies
check_dependencies() {
if ! command -v zip &> /dev/null; then
echo -e "${RED}Error: The 'zip' command is not installed.${NC}"
echo "To install on Ubuntu/Debian: sudo apt-get install zip"
echo "To install on CentOS/RHEL: sudo yum install zip"
exit 1
fi
}
# Function to compress
compress_directory() {
local directory="$1"
local zip_file="$2"
# Check if the directory exists
if [[ ! -d "$directory" ]]; then
echo -e "${RED}Error: The directory '$directory' does not exist.${NC}"
exit 1
fi
# Build zip command
local cmd_zip="zip"
# Add recursive mode
cmd_zip="$cmd_zip -r"
# Add verbose mode if enabled
if [[ "$VERBOSE" == "true" ]]; then
cmd_zip="$cmd_zip -v"
fi
# Add password if specified
if [[ "$USE_PASSWORD" == "true" ]]; then
cmd_zip="$cmd_zip -P $PASSWORD"
fi
# Add destination file and source directory
cmd_zip="$cmd_zip \"$zip_file\" \"$directory\""
echo -e "${BLUE}Starting compression...${NC}"
echo -e "${YELLOW}Source directory: $directory${NC}"
echo -e "${YELLOW}Destination file: $zip_file${NC}"
echo ""
# Execute command
eval "$cmd_zip"
# Check result
if [[ $? -eq 0 ]]; then
echo ""
echo -e "${GREEN}✓ Compression completed successfully!${NC}"
# Show information about the created file
if [[ -f "$zip_file" ]]; then
local size=$(du -h "$zip_file" 2>/dev/null | awk '{print $1}' || stat -c%s "$zip_file" 2>/dev/null | numfmt --to=iec || echo "N/A")
echo -e "${GREEN}File created: $zip_file ($size)${NC}"
fi
else
echo ""
echo -e "${RED}✗ Error during compression.${NC}"
exit 1
fi
}
# Function to request password
request_password() {
echo -n "Enter the password for the ZIP file: "
read -s PASSWORD
echo ""
if [[ -z "$PASSWORD" ]]; then
echo -e "${RED}Error: The password cannot be empty.${NC}"
exit 1
fi
}
# Default variables
VERBOSE="false"
USE_PASSWORD="false"
PASSWORD=""
# Process arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-v|--verbose)
VERBOSE="true"
shift
;;
-p|--password)
USE_PASSWORD="true"
shift
;;
-*)
echo -e "${RED}Error: Unknown option: $1${NC}"
show_help
exit 1
;;
*)
break
;;
esac
done
# Check minimum arguments
if [[ $# -lt 1 ]]; then
echo -e "${RED}Error: At least one directory must be specified.${NC}"
echo ""
show_help
exit 1
fi
# Check dependencies
check_dependencies
# Get source directory
SOURCE_DIRECTORY="$1"
# Get destination file (if not specified, use directory name)
if [[ -n "$2" ]]; then
DESTINATION_FILE="$2"
else
BASE_NAME=$(basename "$SOURCE_DIRECTORY")
DESTINATION_FILE="${BASE_NAME}_$(date +%Y%m%d_%H%M%S).zip"
fi
# Ensure extension .zip
if [[ ! "$DESTINATION_FILE" =~ \.zip$ ]]; then
DESTINATION_FILE="${DESTINATION_FILE}.zip"
fi
# Request password if option is enabled
if [[ "$USE_PASSWORD" == "true" ]]; then
request_password
fi
# Check if destination file already exists
if [[ -f "$DESTINATION_FILE" ]]; then
echo -e "${YELLOW}Warning: The file '$DESTINATION_FILE' already exists.${NC}"
echo -n "Do you want to overwrite it? (y/N): "
read -r response
if [[ ! "$response" =~ ^[yY]$ ]]; then
echo "Operation cancelled."
exit 0
fi
fi
# Execute compression
compress_directory "$SOURCE_DIRECTORY" "$DESTINATION_FILE"
echo ""
echo -e "${GREEN}Process completed!${NC}"