-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.c
More file actions
125 lines (96 loc) · 2.98 KB
/
extract.c
File metadata and controls
125 lines (96 loc) · 2.98 KB
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
#include "extract.h"
#include "bitio.h"
#include "lz77.h"
#include "bitfilecreation.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int sizeOfFile(FILE *fp, long int offset){
char fileSize[12];
long int fileSizeOffset = offset + 124;
//printf("%ld-----offset\n",fileSizeOffset);
int size;
fseek(fp, fileSizeOffset, SEEK_SET);
if(fread(fileSize, 1, 12, fp)){
//printf("%s\n",fileSize);
size = atoi(fileSize);
return size;
}
else{
printf("Error in reading file");
}
return 0;
}
void listofFilesinArchive(char *tar){
FILE *fp = fopen(tar, "rb"); //reading mode for non text files
if(fp == NULL){
printf("Cannot open tar file");
return;
}
/*
Syntax of fseek -> int fseek(FILE *stream, long int offset, int pos)
SEEK_CUR - current position, SEEK_END - end of the file, SEEK_SET - beginning
*/
fseek(fp, 0L, SEEK_END);
long int lengthoftarfile = ftell(fp); //Length of tar file
char sub[100]; //subparts of tar file
int fileSize;
long int offset = 0;
while(offset < lengthoftarfile){
fileSize = sizeOfFile(fp, offset);
fseek(fp, offset, SEEK_SET);
if(fread(sub, 1, 100, fp)){
printf("%s\n", sub);
offset = offset + 512 + fileSize;
}
else{
printf("Error");
}
}
fclose(fp);
}
char** extractFilesFromArchive(char *tarFile,int var) {
char **l = (char **) malloc(10 * sizeof(char*));
int i;
for (int i =0 ; i < 10; ++i)
l[i] = (char *) malloc(20 * sizeof(char));
i = 0;
FILE *fp = fopen(tarFile, "rb");
fseek(fp, 0L, SEEK_END);
long int lenOfTarFile = ftell(fp); /*Length of tar file*/
if (fp == NULL) {
printf("can not open tar file!");
return NULL;
}
char *name;
name = NULL;
name = (char *)malloc(sizeof(char)*(100));
int fileSize;
long int offset = 0;
while (offset < lenOfTarFile) {
FILE *file;
fileSize = sizeOfFile(fp, offset); /*Find a size of current file*/
fseek(fp, offset, SEEK_SET); /*Point to position that we need*/
if(fread(name, 1, 100, fp)){
file = fopen(name, "wb");
//printf("%s",name);
memcpy(l[i], name,strlen(name));
i+=1;
/*Add content to a new file*/
fseek(fp, offset + 512, SEEK_SET); /*point to the begining of content */
char *buffer = (char*)malloc(fileSize * sizeof(char*) + 1);
size_t bytes = fread( buffer, 1, fileSize, fp );
fwrite( buffer, 1, bytes, file);
free(buffer);
free(name);
fclose(file);
//remove(file);
offset = offset + 512 + fileSize; /*Offset to the next file in tar file*/
}
else{
printf("Error in reading file");
}
}
return l;
fclose(fp);
}