/
func1PA.c
169 lines (110 loc) · 4.71 KB
/
func1PA.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Parikh Shivam
ICSI 402
Assignment 5
UNIX ID: sp191221
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "constantsPA.h"
#include "structPA.h"
#include "externsPA.h"
#include "prototypesPA.h"
// process 1 makes a header file for the compression file
// take a file name as a parameter
void processCompression1(char *fileName){
unsigned char length; // holds the length of the file name
unsigned int size; // size of the file
length = strlen(fileName); // finds the length
size = fTell(fileName); // get the size of the file
//printf("The string length: %d", length);
//printf(" This is argument: %s", fileName);
//printf(" FileSize %d\n", size);
fwrite(&length, 1, 1, archiveFileP); // write the file name length
fwrite(fileName, 1, length, archiveFileP); // write the file name
fwrite(&size, 1, 4, archiveFileP); // write the file size
}
// this process will take a file name as a parameter
// and copies the file content onto the compress file
void processCompression2(char *fileName){
FILE *fp; // file pointer
int num = 1; // tracker of number of bytes
char buffer[BUFFERSIZE]; // buffer of size of 1000
if((fp=fopen(fileName, "r") )==NULL){ // if open fails
printf("Error opening file \n"); // print error
exit(1); // exit the prog
}
while(num != 0){ // while program doesn't read 0 bytes
num = fread(buffer, 1, BUFFERSIZE, fp); // read 1000 bytes and keep of track of bytes read
fwrite(buffer, 1, num, archiveFileP); // write the number of bytes read
}
}
// this function take a file name as a parameter and it returns an unsigned int
// return the file size
unsigned int fTell(char *fileSize){
FILE *fp; // file pointer for the working file
unsigned int len; // holds the length
if((fp = fopen(fileSize, "r"))==NULL){ // if error in opening file
printf("Error opening file"); // print an error
exit(1); // exit the prog
}
fseek(fp, 0, SEEK_END); // seek till end
len = ftell(fp); // get the length
fclose(fp); // close the file
return len; // return the length of the file
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// this function will take an achive file name as a parameter
// creates a file information data base so we can read enough content to create files
void processExtraction(char *archFile){
int i; // for loop counter
unsigned char nFiles; // holds number of files
char *temp; // temp character pointer
unsigned int sizeFile; // holds size of file
if((xArchiveFileP=fopen(archFile, "r") )==NULL){ // if open fails
printf("Error opening file \n"); // print an error
exit(1); // exit the prog
}
fread(&nFiles,1,1,xArchiveFileP); // read the size of file
numberFiles = nFiles; // copy the number to temp
for(i = 0; i < numberFiles; i++){ // num for number of files times
fread(&nFiles,1,1,xArchiveFileP); // read the size of file name
temp = malloc(nFiles); // malloc space for file name
fread(temp, nFiles, 1,xArchiveFileP); // read the file name
fread(&sizeFile, 4, 1,xArchiveFileP); // read the size of file
addFileInfo(nFiles, temp, sizeFile); // process info to be added onto struct array
}
}
// takes size of file name, file name, file size in bytes
// adds info on to an array
void addFileInfo(unsigned char nameSizeP1, char *fileNameP2, unsigned int fileSizeP3){
fileStore[arrayIndex].nameSize = nameSizeP1; //copy the file name size on it
fileStore[arrayIndex].fileName = malloc(nameSizeP1); // create enough space for file name
strcpy(fileStore[arrayIndex].fileName, fileNameP2); // copy file name on it
fileStore[arrayIndex].fileSize = fileSizeP3; // copy file size
arrayIndex ++; // increment an array index
}
// this will go through the file info storage and create files for extractions
// this will also copy the content to it from compressed file
void extractFileCreation(){
int i; // for loop counter
int count = 0; // for copying the file content
FILE *file; // file pointer
char buffer[1]; // write file byte by byte
int numRead = 0; // keep track of bytes read
for(i = 0; i < numberFiles; i++){ // run for number of files there are
count = 0; // reset count to 0
numRead = 0; // reset numRead to 0
if((file = fopen(fileStore[i].fileName, "w")) == NULL){ // create a file for writing- if fails
printf("error opening the for writing \n"); //print an error
exit(1); // exit the program
}
while(count != fileStore[i].fileSize){ // read byte untill last one
numRead = fread(buffer, 1, 1, xArchiveFileP); //read 1 byte
fwrite(buffer, 1, 1, file); // wite 1 byte
count++; // increment count
}
}
}