-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f054083
Showing
4 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# Temporary files | ||
*.tmp | ||
*.log | ||
*.bak | ||
*.swp | ||
|
||
# Compressed files | ||
*.zip | ||
*.7z | ||
*.tar.gz | ||
*.rar | ||
|
||
# Temporary folders | ||
temp/ | ||
tmp/ | ||
*.tmp/ | ||
|
||
# Output files | ||
*.out | ||
*.o | ||
*.a | ||
*.so | ||
|
||
# Virtual environment | ||
venv/ | ||
.env/ | ||
|
||
# System files | ||
.DS_Store | ||
Thumbs.db | ||
|
||
# IDE files | ||
.idea/ | ||
.vscode/ | ||
|
||
# pyinstaller | ||
dist/ | ||
build/ | ||
*.spec | ||
.exe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import os | ||
import shutil | ||
import tempfile | ||
import subprocess | ||
from getpass import getpass | ||
import argparse | ||
|
||
def check_executable(executable): | ||
result = subprocess.run(["where", executable], capture_output=True, text=True) | ||
if result.returncode != 0: | ||
raise Exception(f"Error: {executable} is not installed or not in the PATH.") | ||
|
||
def cleanup(temp_folder): | ||
if os.path.exists(temp_folder): | ||
shutil.rmtree(temp_folder) | ||
print(f"Cleanup successful. Temporary folder {temp_folder} has been removed.") | ||
|
||
def open_explorer(path): | ||
try: | ||
if os.name == 'nt': # Windows | ||
subprocess.run(["explorer", path]) | ||
elif os.name == 'posix': # macOS or Linux | ||
if sys.platform == 'darwin': # macOS | ||
subprocess.run(["open", path]) | ||
else: # Linux | ||
subprocess.run(["xdg-open", path]) | ||
else: | ||
raise Exception("Unsupported OS.") | ||
except Exception as e: | ||
print(f"Error: Failed to open the folder. Please open it manually at {path}. Exception: {e}") | ||
|
||
def main(): | ||
parser = argparse.ArgumentParser(description="Edit a compressed file.") | ||
parser.add_argument("zip_file", help="The zip file to edit.") | ||
parser.add_argument("-o", "--output", help="The name of the output file. If not specified, the output filename will be the same as the input file with '_edited' appended.", default=None) | ||
|
||
args = parser.parse_args() | ||
zip_file = args.zip_file | ||
|
||
if not os.path.exists(zip_file): | ||
print(f"Error: The file {zip_file} does not exist.") | ||
return | ||
|
||
password = getpass("Enter the password for decryption and encryption (leave blank if none): ") | ||
|
||
new_zip_file = args.output if args.output else f"{os.path.splitext(zip_file)[0]}_edited{os.path.splitext(zip_file)[1]}" | ||
|
||
try: | ||
temp_folder = tempfile.mkdtemp() | ||
print(f"Working in temporary folder: {temp_folder}") | ||
|
||
check_executable("7z") | ||
|
||
unzip_result = subprocess.run(["7z", "x", f"-p{password}", zip_file, f"-o{temp_folder}"]) | ||
if unzip_result.returncode != 0: | ||
raise Exception("Error: Extraction failed. Please check the password and try again.") | ||
|
||
print("Please edit the files in the temporary folder.") | ||
open_explorer(temp_folder) | ||
|
||
# Wait for the user to finish editing the files | ||
# Meanwhile, the user have the following options: | ||
# 1. Enter wq to save and exit | ||
# 2. Enter q to exit without saving | ||
# 3. Enter p to reset the password | ||
while True: | ||
print("Enter 'wq' to save and exit, 'q' to exit without saving, 'p' to reset the password") | ||
user_input = input(":").strip().lower() | ||
if user_input == 'wq': | ||
print(f"Compressing the updated files into {new_zip_file}...") | ||
zip_command = ["7z", "a", new_zip_file, f"{temp_folder}/*"] | ||
if password: | ||
zip_command.insert(2, f"-p{password}") | ||
zip_result = subprocess.run(zip_command) | ||
if zip_result.returncode != 0: | ||
raise Exception("Error: Zipping failed.") | ||
print(f"Process completed successfully. The updated file is {new_zip_file}.") | ||
break | ||
elif user_input == 'q': | ||
print("Exiting without saving.") | ||
break | ||
elif user_input == 'p': | ||
password_new = getpass("Enter the new password (leave blank to remove the password): ") | ||
# enter the new password again to confirm | ||
password_confirm = getpass("Enter the new password again: ") | ||
if password_new == password_confirm: | ||
password = password_new | ||
print("Password has been reset.") | ||
else: | ||
print("Passwords do not match. Please try again.") | ||
else: | ||
print("Invalid input. Please try again.") | ||
|
||
except Exception as e: | ||
print(e) | ||
|
||
finally: | ||
cleanup(temp_folder) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2025 sun123zxy | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# 7z-edit | ||
|
||
## Description | ||
|
||
A simple command line tool to ease the cumbersome process of editing the contents of a (possibly encrypted) compressed file. Theoretically, any compressed file format supported by 7-Zip is supported. | ||
|
||
## Requirements | ||
|
||
- 7-Zip installed and added to the system PATH | ||
- (Optional) Python 3.x if you wish to run the script directly | ||
|
||
## Usage | ||
|
||
```sh | ||
7z-edit <zip_file> [-o <output_file>] | ||
``` | ||
|
||
- `zip_file`: The path to the zip file you want to edit. | ||
- `-o, --output`: (Optional) The name of the output file. If not specified, the output filename will be the same as the input file with `_edited` appended. | ||
|
||
## Instructions | ||
|
||
1. Run the script with the zip file you want to edit. | ||
2. Enter the password for decryption and encryption when prompted (leave blank if none). | ||
3. The script will extract the contents to a temporary folder. | ||
4. Edit the files in the temporary folder. | ||
5. Enter `wq` to save and exit, `q` to exit without saving, or `p` to reset the password. | ||
6. If `wq` is entered, the script will compress the updated files into the specified output file. | ||
|
||
## Notes | ||
|
||
- Ensure that 7-Zip is installed and added to the system PATH. | ||
- The temporary folder will be cleaned up automatically after the process is completed. | ||
- Both the script and this README are written in collaboration with Github Copilot. Be aware of potential errors or inaccuracies. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License. |