Skip to content

Commit 21a4279

Browse files
authored
Merge pull request #114 from kana800/add-solutions
added solution convert_folder_to_zip
2 parents 06ae88e + ffe2787 commit 21a4279

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed

Python/folder_zip/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Converts Folder to a Zip
2+
3+
## GUI Version
4+
5+
<p align="center">
6+
<img width="568" height="516" src="pic1.gif"></img>
7+
</p>
8+
9+
10+
## CMD Version
11+
12+
### USAGE
13+
14+
```python
15+
convert.py -h
16+
```
17+
18+
or
19+
20+
21+
```python
22+
convert.py --help
23+
```
24+
25+
26+
```python
27+
convert.py <location_of_dir> <place_to_be_saved>
28+
```
29+
30+
31+
**TO RUN THE GUI VERSION CHECK THE REQUIREMENT TXT**

Python/folder_zip/convert.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from shutil import make_archive
2+
import argparse
3+
from os import path
4+
from sys import exit
5+
6+
#argparse setup
7+
parser = argparse.ArgumentParser()
8+
parser.add_argument("directory",help="path of the directory/file that needs to be zipped")
9+
parser.add_argument("zip_location",help="path of the zip file you need to store")
10+
args = parser.parse_args()
11+
12+
#declaring global variables for directory and zip_location
13+
zip_name = args.zip_location
14+
directory_name= args.directory
15+
16+
17+
def path_checker(_path):
18+
'''
19+
checking if the paths exists
20+
returns True or False
21+
'''
22+
return path.exists(_path)
23+
24+
def make_zip():
25+
global zip_name,directory_name
26+
try:
27+
make_archive(zip_name,'zip',directory_name)
28+
print(f"zip file generated path: {zip_name}")
29+
return;
30+
except:
31+
print(f"Error!")
32+
return;
33+
34+
if __name__ == "__main__":
35+
if path_checker(directory_name):
36+
make_zip()
37+
else:
38+
print("invalid path")
39+
exit()

Python/folder_zip/gui.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# basic modules needed
2+
import zipfile
3+
import argparse
4+
from os import path
5+
import sys
6+
# import PyQt5 modules
7+
from PyQt5 import QtWidgets as qtw
8+
from PyQt5 import QtCore as qtc
9+
from PyQt5 import QtGui as qtg
10+
11+
filename_list = []
12+
filename = ""
13+
14+
class MainWindow(qtw.QWidget):
15+
def __init__(self,*args,**kwargs):
16+
super().__init__()
17+
self.setLayout(qtw.QVBoxLayout())
18+
19+
layout1 = qtw.QHBoxLayout()
20+
label1 = qtw.QLabel("Directory: ")
21+
self.enter1 = qtw.QLineEdit()
22+
button1 = qtw.QPushButton("F")
23+
24+
button1.clicked.connect(self.openfile)
25+
26+
layout1.addWidget(label1)
27+
layout1.addWidget(self.enter1)
28+
layout1.addWidget(button1)
29+
30+
layout2 = qtw.QHBoxLayout()
31+
label2 = qtw.QLabel("Save location: ")
32+
self.enter2 = qtw.QLineEdit()
33+
button2 = qtw.QPushButton("F")
34+
35+
button2.clicked.connect(self.savefile)
36+
37+
layout2.addWidget(label2)
38+
layout2.addWidget(self.enter2)
39+
layout2.addWidget(button2)
40+
41+
convert = qtw.QPushButton("convert")
42+
convert.clicked.connect(self.convert)
43+
44+
self.layout().addLayout(layout1)
45+
self.layout().addLayout(layout2)
46+
self.layout().addWidget(convert)
47+
self.layout().addWidget(qtw.QPushButton("close",clicked=self.close))
48+
self.show()
49+
50+
def openfile(self):
51+
global filename_list
52+
filename_list,other = qtw.QFileDialog.getOpenFileUrls()
53+
text=""
54+
for i in filename_list:
55+
text += f"{i.path()},"
56+
57+
self.enter1.setText(text)
58+
59+
def savefile(self):
60+
global filename
61+
filename,other = qtw.QFileDialog.getSaveFileName()
62+
self.enter2.setText(filename)
63+
64+
def convert(self):
65+
global filename_list,filename
66+
if filename:
67+
with zipfile.ZipFile(filename,'w') as myzip:
68+
for urls in filename_list:
69+
myzip.write(urls.path())
70+
qtw.QMessageBox.information(self,"Success!","Successfully converted")
71+
self.enter1.clear()
72+
self.enter2.clear()
73+
else:
74+
qtw.QMessageBox.critical(self,"Error","No File Included")
75+
76+
if __name__ == "__main__":
77+
app = qtw.QApplication(sys.argv)
78+
win = MainWindow()
79+
sys.exit(app.exec_())

Python/folder_zip/pic1.gif

103 KB
Loading

Python/folder_zip/requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Package Version
2+
----------------- ---------
3+
PyQt5 5.15.0
4+
PyQt5-sip 12.8.0

0 commit comments

Comments
 (0)