Created
December 16, 2024 19:12
-
-
Save jsmolina/162778cfbb6d4c02c4c37d1c219e5ef2 to your computer and use it in GitHub Desktop.
dat file manager for any size
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#define MAX_FILENAME_LEN 16 | |
// Estructura para almacenar la cabecera de cada archivo (nombre y tamaño) | |
typedef struct { | |
char filename[MAX_FILENAME_LEN]; | |
long long filesize; | |
} FileHeader; | |
void concatenate_files(int argc, char *argv[]) { | |
FILE *output = fopen("DATA.DAT", "wb"); | |
if (!output) { | |
perror("Error al abrir el archivo de salida"); | |
exit(EXIT_FAILURE); | |
} | |
for (int i = 1; i < argc; i++) { | |
printf("\nCompressing %s ... \n", argv[i]); | |
FILE *input = fopen(argv[i], "rb"); | |
if (!input) { | |
perror("Error al abrir el archivo de entrada"); | |
exit(EXIT_FAILURE); | |
} | |
// Obtener el tamaño del archivo de entrada | |
fseek(input, 0, SEEK_END); | |
long filesize = ftell(input); | |
fseek(input, 0, SEEK_SET); | |
// Escribir la cabecera con el nombre y el tamaño del archivo | |
FileHeader header; | |
strncpy(header.filename, argv[i], MAX_FILENAME_LEN); | |
header.filename[MAX_FILENAME_LEN - 1] = '\0'; | |
header.filesize = filesize; | |
printf("Filesize: %lld\n", header.filesize); | |
fwrite(&header, sizeof(FileHeader), 1, output); | |
// Copiar el contenido del archivo al archivo de salida | |
char *buffer = (char *)malloc(filesize); | |
if (!buffer) { | |
perror("Error al asignar memoria"); | |
exit(EXIT_FAILURE); | |
} | |
fread(buffer, 1, filesize, input); | |
fwrite(buffer, 1, filesize, output); | |
free(buffer); | |
fclose(input); | |
} | |
fclose(output); | |
printf("Archivos concatenados en 'DATA.DAT'.\n"); | |
} | |
int main(int argc, char *argv[]) { | |
if (argc < 2) { | |
fprintf(stderr, "Uso: %s <archivo1> <archivo2> ... <archivoN>\n", | |
argv[0]); | |
exit(EXIT_FAILURE); | |
} | |
concatenate_files(argc, argv); | |
return 0; | |
} |
This file contains hidden or 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
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/stat.h> | |
#include <unistd.h> | |
#define MAX_FILENAME_LEN 16 | |
// Estructura para almacenar la cabecera de cada archivo (nombre y tamaño) | |
typedef struct { | |
char filename[MAX_FILENAME_LEN]; | |
long long filesize; | |
} FileHeader; | |
void decompress_files() { | |
FILE *input = fopen("DATA.DAT", "rb"); | |
if (!input) { | |
perror("Error al abrir el archivo de entrada"); | |
exit(EXIT_FAILURE); | |
} | |
struct stat st = {0}; | |
if (stat("data", &st) == -1) { | |
mkdir("data", 0700); | |
} | |
chdir("data"); | |
while (1) { | |
FileHeader header; | |
// Leer la cabecera | |
size_t read_size = fread(&header, sizeof(FileHeader), 1, input); | |
if (read_size != 1) { | |
if (feof(input)) | |
break; | |
perror("Error al leer la cabecera"); | |
exit(EXIT_FAILURE); | |
} | |
// Abrir el archivo de salida con el nombre original | |
FILE *output = fopen(header.filename, "wb"); | |
if (!output) { | |
perror("Error al abrir el archivo de salida"); | |
exit(EXIT_FAILURE); | |
} | |
// Leer y escribir los datos del archivo | |
size_t block_size = 4096; // 4 KB | |
char *buffer = (char *)malloc(block_size); | |
size_t bytes_read; | |
size_t bytes_remaining = header.filesize; | |
while (bytes_remaining > 0) { | |
if (bytes_remaining > block_size) { | |
bytes_read = fread(buffer, 1, block_size, input); | |
} else { | |
bytes_read = fread(buffer, 1, bytes_remaining, input); | |
} | |
printf("Read %zu, ", bytes_read); | |
fwrite(buffer, 1, bytes_read, output); | |
bytes_remaining = bytes_remaining - bytes_read; | |
printf("Remaining %zu, ", bytes_remaining); | |
} | |
free(buffer); | |
fclose(output); | |
// fseek(input, header.filesize, SEEK_CUR); | |
printf("Archivo '%s' descomprimido.\n", header.filename); | |
printf("Seek %lld\n", header.filesize); | |
} | |
fclose(input); | |
} | |
int main() { | |
// Desempaquetar archivos desde 'data.dat' | |
decompress_files(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment