-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lab6 #214
Open
rebrikartem
wants to merge
1
commit into
somov7:main
Choose a base branch
from
rebrikartem:lab6
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
lab6 #214
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,145 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
#define buffer_size 1048576 // 1Mb | ||
|
||
typedef struct file_metadata | ||
{ | ||
int file_size; | ||
char *file_name; | ||
} file_metadata; | ||
|
||
void create_arc(char *arc_name, int argc, char *argv[]) | ||
{ | ||
FILE *fout = fopen(arc_name, "wb"); | ||
char n = argc - 4; | ||
fputc(n, fout); | ||
|
||
for (int i = 4; i < argc; i++) | ||
{ | ||
FILE *fin = fopen(argv[i], "rb"); | ||
|
||
char name_length = strlen(argv[i]); | ||
fputc(name_length, fout); | ||
|
||
int file_size; | ||
fseek(fin, 0, SEEK_END); | ||
file_size = ftell(fin); | ||
fseek(fin, 0, SEEK_SET); | ||
fwrite(&file_size, sizeof(int), 1, fout); | ||
|
||
fwrite(argv[i], 1, name_length, fout); | ||
|
||
char *data = (char *)malloc(file_size); | ||
fread(data, 1, file_size, fin); | ||
fwrite(data, 1, file_size, fout); | ||
|
||
free(data); | ||
fclose(fin); | ||
} | ||
fclose(fout); | ||
} | ||
|
||
file_metadata get_file_metadata(FILE *fin) | ||
{ | ||
char name_length; | ||
fread(&name_length, 1, 1, fin); | ||
|
||
int file_size; | ||
fread(&file_size, sizeof(int), 1, fin); | ||
|
||
char *file_name = (char *)malloc(name_length + 1); | ||
file_name[name_length] = '\0'; | ||
|
||
fread(file_name, 1, name_length, fin); | ||
|
||
file_metadata data; | ||
data.file_size = file_size; | ||
data.file_name = file_name; | ||
|
||
return data; | ||
} | ||
|
||
void get_list(char *arc_name) | ||
{ | ||
FILE *arc = fopen(arc_name, "rb"); | ||
char n; | ||
fread(&n, 1, 1, arc); | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
file_metadata metadata = get_file_metadata(arc); | ||
|
||
fseek(arc, metadata.file_size, SEEK_CUR); | ||
printf("%s ", metadata.file_name); | ||
free(metadata.file_name); | ||
} | ||
fclose(arc); | ||
} | ||
|
||
void extract_arc(char *arc_name) | ||
{ | ||
FILE *arc = fopen(arc_name, "rb"); | ||
char n; | ||
fread(&n, 1, 1, arc); | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
file_metadata metadata = get_file_metadata(arc); | ||
|
||
FILE *out = fopen(metadata.file_name, "wb"); | ||
char buffer[buffer_size]; | ||
int mod = metadata.file_size; | ||
while (mod > buffer_size) | ||
{ | ||
fread(&buffer, 1, buffer_size, arc); | ||
fwrite(&buffer, 1, buffer_size, out); | ||
mod -= buffer_size; | ||
} | ||
fread(&buffer, 1, mod, arc); | ||
fwrite(&buffer, 1, mod, out); | ||
|
||
free(metadata.file_name); | ||
fclose(out); | ||
} | ||
fclose(arc); | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
if (argc < 4) | ||
{ | ||
printf("Error, not enough arguments\n"); | ||
return -1; | ||
} | ||
|
||
char *arc_name; | ||
|
||
for (int i = 1; i < 4; i += 2) | ||
{ | ||
if (strcmp(argv[i], "--file") == 0) | ||
{ | ||
arc_name = argv[i + 1]; | ||
} | ||
else if (strcmp(argv[i], "--create") == 0) | ||
{ | ||
create_arc(arc_name, argc, argv); | ||
} | ||
else if (strcmp(argv[i], "--extract") == 0) | ||
{ | ||
extract_arc(arc_name); | ||
} | ||
else if (strcmp(argv[i], "--list") == 0) | ||
{ | ||
get_list(arc_name); | ||
} | ||
else | ||
{ | ||
printf("Error, wrong format\n"); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Почему здесь целиком считывается файл, а в extract побуфферно?