Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- const int long long;
- const int N = 2e5;
- void creat_archive(char* name_arc, int count, char* filelochek[]){
- int i;
- FILE* file_arc = fopen(name_arc, "wb");
- FILE* file;
- int name_size[N];
- for (i = 5; i < count; i++){
- file = fopen(filelochek[i], "rb");
- if (file == NULL)
- continue;
- fseek(file, 0, SEEK_END);
- name_size[i - 5] = ftell(file);
- fseek(file, 0, SEEK_SET);
- fclose(file);
- }
- for (i = 0; i < count - 5; i++)
- fprintf(file_arc, "<%s:%llu>", filelochek[i + 5], name_size[i]);
- fprintf(file_arc, "\n");
- for (i = 5; i < count; i++){
- file = fopen(filelochek[i], "rb");
- if (file == NULL){
- printf("file not added");
- } else{
- printf("file added");}
- fclose(file);}
- }
- void extract_archive(char* name_arc){
- int now_time = 0;
- int first_time = 0;
- int size;
- int file_read;
- FILE* file_arc = fopen(name_arc, "rb");
- FILE* file;
- char name_of_file[N];
- while ((file_read = getc(file_arc)) != EOF) { //пока не пустой
- first_time++;
- if (file_read == '\n'){
- break;}
- }
- fseek(file_arc, 0, SEEK_SET);
- while (fscanf(file_arc, "<%s:%llu>", name_of_file, &size) != 0){
- file = fopen(name_of_file, "wb");
- if (file == NULL)
- break;
- now_time = ftell(file_arc);
- fseek(file_arc, first_time, SEEK_SET);
- first_time += size;\
- while (size-- > 0)
- putc((file_read = getc(file_arc)), file);
- fseek(file_arc, now_time, SEEK_SET);
- fclose(file); }
- printf("unzipping");
- }
- void list_archive(char* name_arc){
- FILE* file_arc = fopen(name_arc, "rb");
- char name_of_file[N];
- int read = 0;
- int file_read;
- int i;
- printf("File list_archive:\n");
- while ((file_read = getc(file_arc)) != EOF) {
- if (file_read == ':'){
- read = 0;
- printf("\t%s\n", name_of_file);
- }
- if (read){
- name_of_file[i] = file_read;
- i++;
- }
- if (file_read == '<'){
- read = 1;
- i = 0;
- }
- }
- fclose(file_arc); }
- int main(int argc, char* argv[]){
- FILE* file_arc;
- char* name_arc;
- for (int i = 0; i < argc; i++){
- if(!strcmp("--file", argv[i])){
- name_arc = argv[i + 1];}
- if(!strcmp("--creat", argv[i])){
- creat_archive(name_arc, argc, argv);}
- if(!strcmp("--extract", argv[i])){
- extract_archive(name_arc);}
- if(!strcmp("--list", argv[i])){
- list_archive(name_arc);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement