[Python] Zipํ์ผ ์์ถ ํด์ (2)(๋น๋ฐ๋ฒํธ ํฌํจ)
2022.09.17 - [๊ฐ๋ฐ๐ป/Python] - [Python] Zipํ์ผ ์์ถ ํด์ (๋น๋ฐ๋ฒํธ ํฌํจ)
[Python] Zipํ์ผ ์์ถ ํด์ (๋น๋ฐ๋ฒํธ ํฌํจ)
Python ๋ชจ๋์ธ zipfile๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ๊ฒ Zip ํ์ผ ์์ถํด์ ์ฝ๋๋ฅผ ์์ฑํ์๋ค. Import ๋๋ ํ ๋ฆฌ ๋ฐ ํ์ผ๋ช ํ์์ ์ํด os, ์์ถ Zip ํ์ผ์ ๋ค๋ฃจ๊ธฐ ์ํด zipfile, ์ ํํ ์ค๋ฅ ์์น ๋ฐ ์์ธ ํ์ ์
bymalware.tistory.com
๊ธฐ์กด Zip ํ์ผ ์์ถํด์ ์ฝ๋๋ฅผ ๋ธ๋ก๊ทธ์ ๊ธฐ๋กํด๋์์ผ๋ ํ์ฌ ์ฝ๋๊ฐ ์ ์์ ์ผ๋ก ์๋๋์ง ์๋ ๊ฒฝ์ฐ๋ ์กด์ฌํ์ฌ ํ์ ๊ธ์ ์์ฑํ๊ฒ ๋์๋ค.
Error
๊ธฐ์กด ์ฝ๋๋ฅผ ์ด์ฉํ์์ ๋ "That compression method is not supported" ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ๋ ์กด์ฌํ๋ค.
ํด๋น ์ค๋ฅ์ ์์ธ์ zipfile ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ํธ๋ก ๋ณดํธ๋ zip ์์ ์๋ํ๊ธฐ ์ํด์๋ ์ํธ๋ฅผ ์ค์ ํ ๋ "Zip ๋ ๊ฑฐ์ ์ํธํ" ์ต์ ์ ์ ํํด์ผ ํ์ง๋ง ์ด๋ฅผ ์ ํํ์ง ์์ ๋ฐ์ํ ์ค๋ฅ๋ก ์ถ์ธกํ๊ณ ์๋ค.
https://stackoverflow.com/questions/62738917/python-zipfile-compression-method-is-not-supported
Python Zipfile compression method is not supported
The times when this worked were when I used ZIPCrypto compression. It's with AES-256 that it fails. How to get around this please? I previously had success using the following Python code to open a
stackoverflow.com
์ด๋ฌํ ์ค๋ฅ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ pyzipper๋ก ๋ณ๊ฒฝํ์ฌ ์๋ก์ด ์ฝ๋๋ฅผ ๊ตฌ์ถํ์๋ค.
Pyzipper - Install
pyzipper ์ค์น๋ ํฐ๋ฏธ๋์ ๋ค์๊ณผ ๊ฐ์ ๋ช ๋ น์ด๋ก ์ฝ๊ฒ ์ค์นํ ์ ์๋ค.
pip install pyzipper
Pyzipper ๊ตฌํ
1๊ฐ์ ํ์ผ์ ๋ํด ์์ถ์ ํ๊ณ API๋ก ๋ง๋ค์ด ์ฌ์ฉํ๋ ๋ฐฉ์์ ์ฌ์ฉํ์ฌ ์ฝ๋ฉํ์๋ค.
์์ถํด์ ๋น๋ฐ๋ฒํธ๋ฅผ ์ง์ ํ ํ with ~ as ๊ตฌ๋ฌธ์ผ๋ก ์์ถํด์ ๋์ ํ์ผ์ ์ด์ด์ค ํ extractall()ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ค์ง์ ์ธ ์์ถํด์ ๊ณผ์ ์ ์ํํ๋ค.
import pyzipper
def unzip_fun(filename):
password = '๋น๋ฐ๋ฒํธ'
with pyzipper.AESZipFile(filename, 'r', compression =pyzipper.ZIP_DEFLATED, encryption = pyzipper.WZ_AES) as extracted_zip:
extracted_zip.extractall(path ='์์ถํด์ ๋ ํ์ผ๋ค์ด ์ํ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก', pwd=str.encode(password))
์์ ์ฝ๋๋ 1๊ฐ์ ํ์ผ์ ๋ํด์๋ง ๋์ํ๊ธฐ ๋๋ฌธ์ for ๋ฌธ์ ํตํด ๋ชจ๋ zip ํ์ผ์ ์ธ์๋ก ๋๊ฒจ ๋ชจ๋ ํ์ผ์ ์์ถ ํด์ ๋ฅผ ์ ์์ ์ผ๋ก ์งํํ ์ ์์๋ค.
basic_dir = '์์ถํด์ ๋์์ด ์กด์ฌํ๋ ํ์ผ'
file_list = os.listdir(basic_dir)
file_name = []
for i in range(len(file_list)):
try:
file_name.append(basic_dir + str(file_list[i]))
unzip.unzip_fun(file_name[i])
except:
print(str(file_name[i])+ 'ํด์ ์คํจ')
์ ์ฒด ์์ค์ฝ๋
์ ์ฒด ์์ค์ฝ๋๋ ๊นํ๋ธ์ ์กด์ฌํ๋ค.
์ค๋ฅ, ์๋ชป๋ ์ ๋๋ ๊ถ๊ธํ ์ ์ด ์์ผ์๋ค๋ฉด ๋๊ธ ๋จ๊ฒจ์ฃผ์ธ์โ