jumpserver/apps/common/utils/file.py

31 lines
910 B
Python
Raw Normal View History

2021-12-03 07:50:04 +00:00
import os
import csv
2022-05-19 16:10:22 +00:00
2021-12-03 07:50:04 +00:00
import pyzipper
2022-05-19 16:10:22 +00:00
import requests
2021-12-03 07:50:04 +00:00
def create_csv_file(filename, headers, rows, ):
with open(filename, 'w', encoding='utf-8-sig')as f:
w = csv.writer(f)
w.writerow(headers)
w.writerows(rows)
2022-01-10 11:02:18 +00:00
def encrypt_and_compress_zip_file(filename, secret_password, encrypted_filenames):
2021-12-03 07:50:04 +00:00
with pyzipper.AESZipFile(
filename, 'w', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES
) as zf:
zf.setpassword(secret_password)
2022-01-10 11:02:18 +00:00
for encrypted_filename in encrypted_filenames:
with open(encrypted_filename, 'rb') as f:
zf.writestr(os.path.basename(encrypted_filename), f.read())
2022-05-19 16:10:22 +00:00
def download_file(src, path):
with requests.get(src, stream=True) as r:
r.raise_for_status()
with open(path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)