This code provides a simple command-line utility written in C...

September 3, 2025 at 03:15 PM

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/stat.h> // Funzione per comprimere un file o una cartella void comprimiBackup() { // Ottengo il nome della cartella o file char fonte[128], nomeFile[128], comando[512]; printf("File/Cartella da comprimere: "); fgets(fonte, sizeof(fonte), stdin); fonte[strcspn(fonte, "\n")] = 0; // Ottengo il nome da assegnare all'archivio printf("Nome archivio: "); fgets(nomeFile, sizeof(nomeFile), stdin); nomeFile[strcspn(nomeFile, "\n")] = 0; // Creo il commando da eseguire con i input dell'utente snprintf(comando, sizeof(comando), "tar -czf %s %s", nomeFile, fonte); system(comando); printf("Compressione completata.\n\n"); } // Funzione per la decompressione di un archivio void ripristinaBackup() { //Ottengo Il nome del archivio da decomprimere char nomeFile[128], dest[128], comando[512]; printf("Archivio tar da ripristinare: "); fgets(nomeFile, sizeof(nomeFile), stdin); nomeFile[strcspn(nomeFile, "\n")] = 0; // Creo il commando da eseguire con i input dell'utente snprintf(comando, sizeof(comando), "tar -xzf %s", nomeFile); system(comando); printf("Ripristino completato.\n\n"); } int main() { int scelta; // Menu per la gestire le scelte dell'utente do { printf("Compressione Cartelle e File\n"); printf("1) Comprimi file/cartella\n"); printf("2) Ripristina backup\n"); printf("3) Esci\n"); printf("Scelta: "); scanf("%d", &scelta); getchar(); switch(scelta) { case 1: comprimiBackup(); break; case 2: ripristinaBackup(); break; case 3: printf("Chiusura.\n"); break; default: printf("Opzione non valida.\n"); } } while (scelta != 3); return 0; }

This code provides a simple command-line utility written in C to compress and decompress files or directories using the tar utility. Here is a detailed explanation of what it does:

comprimiBackup (Compress Backup)

This function compresses a file or directory into a .tar.gz archive:

  1. It prompts the user to input the name (path) of the file or directory to compress, storing it in the fonte variable.
  2. It prompts the user for the name of the resulting archive, storing it in the nomeFile variable.
  3. It creates a tar command string using snprintf in the format: tar -czf <archive_name> <source_path>.
  4. The system function is called to execute the command, compressing the file/directory into a .tar.gz archive file.
  5. It prints a message signaling the completion of the compression.

ripristinaBackup (Restore Backup)

This function extracts (decompresses) a .tar.gz archive:

  1. It prompts the user for the name of the .tar.gz archive to extract, storing it in nomeFile.
  2. It creates a tar command string using snprintf in the format: tar -xzf <archive_name>.
  3. The system function executes the command, extracting the contents of the .tar.gz archive.
  4. It prints a message signaling the completion of the restoration.

main Function

The main function runs a menu-based interface where the user can:

  1. Choose to compress a file or directory (calls comprimiBackup).
  2. Choose to restore (extract) a .tar.gz archive (calls ripristinaBackup).
  3. Exit the program.

The main logic:

  • Displays a menu for the user.
  • Reads the user’s choice and executes the appropriate function (comprimiBackup, ripristinaBackup, or exits the program).
  • Repeats until the user chooses the "exit" option.

Notes:

  • Dependencies: The code assumes the availability of the tar command on the system (typically available in Unix/Linux environments).
  • Security: Using system() can expose the program to command injection vulnerabilities if user input is not validated properly. In this case, user input (e.g., fonte or nomeFile) directly contributes to the constructed shell command without any sanitization.
  • Functionality: The tool essentially acts as a wrapper for the tar command to provide a simpler user interface for compressing and extracting .tar.gz archives.

Example Execution:

  1. Compress:

    • User inputs source_folder as the file/folder to compress.
    • User inputs backup_archive.tar.gz as the archive name.
    • Outputs: backup_archive.tar.gz containing source_folder.
  2. Restore:

    • User inputs backup_archive.tar.gz as the archive to extract.
    • Outputs: The extraction of the contents of backup_archive.tar.gz in the current directory.
  3. Exit:

    • User selects "3" to exit the program.
Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node