Skip to content
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 #198

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

lab6 #198

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions lab6/Source.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
#include <stdio.h>
#include <string.h>
#include <malloc.h>

union FSize
{
unsigned char Bytes[4];
unsigned int SizeOfFile;
} FSize;

void Create(int argc, char* argv[], char* ArchName)
{
FILE* Arch = fopen(ArchName, "wb");
FILE* F;

for (int i = 5; i < argc; i++)
{
F = fopen(argv[i], "rb");

fseek(F, 0, SEEK_END);

unsigned int FileSize = ftell(F);

fseek(F, 0, SEEK_SET);

unsigned char NameLen = strlen(argv[i]) + 1;

fputc(NameLen, Arch);

FSize.SizeOfFile = FileSize;

for (int j = 3; j >= 0; j--)
fputc(FSize.Bytes[j], Arch);

fwrite(argv[i], sizeof(unsigned char), NameLen, Arch);

unsigned char* Buff = calloc(FileSize, sizeof(unsigned char));

fread(Buff, sizeof(unsigned char), FileSize, F);
fwrite(Buff, sizeof(unsigned char), FileSize, Arch);

free(Buff);
fclose(F);
}

printf("%s created\n", ArchName);

fclose(Arch);
}

void Extract(char* ArchName) {
FILE* Arch = fopen(ArchName, "rb");
int NameLen;

while ((NameLen = fgetc(Arch)) != -1)
{
char FileLen[4];
fread(FileLen, sizeof(unsigned char), 4, Arch);

for (int j = 3; j >= 0; j--)
FSize.Bytes[j] = FileLen[3-j];

unsigned char* FName = calloc(NameLen, sizeof(unsigned char));
for (unsigned char i = 0; i < NameLen; i++) {
FName[i] = fgetc(Arch);
}

FILE* F = fopen(FName, "wb");

unsigned char* Buff = calloc(sizeof(unsigned char), FSize.SizeOfFile);

fread(Buff, sizeof(unsigned char), FSize.SizeOfFile, Arch);
fwrite(Buff, sizeof(unsigned char), FSize.SizeOfFile, F);

printf("%s exctracted\n", FName);

free(FName);
free(Buff);
fclose(F);
}

fclose(Arch);
}

void List(char* ArchName)
{
FILE* Arch = fopen(ArchName, "rb");
unsigned int NameLen;

while ((NameLen = getc(Arch)) != EOF)
{
char FileLen[4];
fread(FileLen, sizeof(unsigned char), 4, Arch);

for (int j = 3; j >= 0; j--)
FSize.Bytes[j] = FileLen[3 - j];

unsigned char* FName = calloc(NameLen, sizeof(unsigned char));

fread(FName, sizeof(unsigned char), NameLen, Arch);

fseek(Arch, FSize.SizeOfFile, SEEK_CUR);

printf("%s\n", FName);
free(FName);
}
fclose(Arch);
}

int main(int argc, char* argv[])
{
char* ArchName = argv[3];

if (strcmp(argv[4], "--create") == 0)
Create(argc, argv, ArchName);
else if (strcmp(argv[4], "--extract") == 0)
Extract(ArchName);
else if (strcmp(argv[4], "--list") == 0)
List(ArchName);

return 0;
}