Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #define UINT64 unsigned long long int
  5.  
  6.  
  7. static void get_length(UINT64 array[], const int argc, char const *argv[]);
  8. static void input_length(FILE* const arch, UINT64 array[], const int argc, char const *argv[]);
  9. static void input_files(FILE* const arch, const int argc, char const *argv[]);
  10.  
  11. static void archive(FILE* const arch, const int argc, char const *argv[]);
  12. static void unarchive(FILE* const arch, char const *argv[]);
  13. static void get_file_list(FILE* const arch);
  14.  
  15. int main(const int argc, char const *argv[]) {
  16. if (argc < 2) {
  17. printf("Error: archive not found.\n");
  18. return 1;
  19. }
  20.  
  21. auto FILE *arch;
  22. if (argc != 2) {
  23. //printf("%s\n", argv[2]);
  24. if ((strcmp(argv[2], "list") == 0) && (argc == 3)){
  25.  
  26. arch = fopen(argv[1], "rb");
  27. if (arch == NULL) return 1;
  28. get_file_list(arch);
  29.  
  30. }
  31. else {
  32. //printf("%s\n", argv[2]);
  33. //printf("else\n");
  34. arch = fopen(argv[1], "wb");
  35. if (arch == NULL) return 1;
  36. archive(arch, argc, argv);
  37. }
  38. }
  39. else {
  40. arch = fopen(argv[1], "rb");
  41. if (arch == NULL) return 1;
  42. unarchive(arch, argv[1]);
  43. }
  44.  
  45. fclose(arch);
  46. //remove(arch);
  47. return 0;
  48. }
  49.  
  50. static void unarchive(FILE* const arch, char const *argv[]) {
  51. auto UINT64 now_position = 0;
  52. auto UINT64 start_position = 0;
  53.  
  54. auto int c;
  55. while ((c = getc(arch)) != EOF) {
  56. start_position++;
  57. if (c == '\n') break;
  58. }
  59. fseek(arch, 0, SEEK_SET);
  60.  
  61. auto char filename[128];
  62. auto UINT64 filesize;
  63.  
  64. auto FILE *file;
  65. while (fscanf(arch, "| %llu = %s |", &filesize, filename) != 0) {
  66. file = fopen(filename, "wb");
  67. if (file == NULL) break;
  68.  
  69. printf("|File: %s = Bytes: %llu|\n", filename, filesize);
  70.  
  71. now_position = ftell(arch);
  72. fseek(arch, start_position, SEEK_SET);
  73.  
  74. start_position += filesize;
  75. while (filesize-- > 0)
  76. putc((c = getc(arch)), file);
  77.  
  78. fseek(arch, now_position, SEEK_SET);
  79.  
  80. fclose(file);
  81. printf("\n%s", argv);
  82. remove(argv);
  83. }
  84. }
  85.  
  86. static void get_file_list(FILE* const arch) {
  87. auto UINT64 now_position = 0;
  88. auto UINT64 start_position = 0;
  89.  
  90. auto int c;
  91. while ((c = getc(arch)) != EOF) {
  92. start_position++;
  93. if (c == '\n') break;
  94. }
  95. fseek(arch, 0, SEEK_SET);
  96.  
  97. auto char filename[128];
  98. auto UINT64 filesize;
  99.  
  100. auto FILE *file;
  101. while (fscanf(arch, "| %llu = %s |", &filesize, filename) != 0) {
  102. file = fopen(filename, "wb");
  103. if (file == NULL) break;
  104.  
  105. printf("|File: %s |\n", filename);
  106.  
  107. now_position = ftell(arch);
  108. fseek(arch, start_position, SEEK_SET);
  109.  
  110. start_position += filesize;
  111.  
  112. fseek(arch, now_position, SEEK_SET);
  113.  
  114. fclose(file);
  115. }
  116.  
  117. }
  118.  
  119.  
  120. static void archive(FILE* const arch, const int argc, char const *argv[]) {
  121. auto UINT64 save_length = (UINT64*)malloc((argc - 2) * sizeof(UINT64));
  122. get_length(save_length, argc, argv);
  123. input_length(arch, save_length, argc, argv);
  124. input_files(arch, argc, argv);
  125. }
  126.  
  127. static void get_length(UINT64 array[], const int argc, char const *argv[]) {
  128. auto FILE *file;
  129. auto unsigned char index;
  130. for (index = 2; index < argc; index++) {
  131. file = fopen(argv[index], "rb");
  132. if (file == NULL) continue;
  133.  
  134. fseek(file, 0, SEEK_END);
  135. array[index - 2] = ftell(file);
  136. fseek(file, 0, SEEK_SET);
  137.  
  138. fclose(file);
  139. }
  140. }
  141.  
  142.  
  143. static void input_length(FILE* const arch, UINT64 array[], const int argc, char const *argv[]) {
  144. auto unsigned char index;
  145. for (index = 0; index < argc - 2; index++)
  146. fprintf(arch, "| %llu = %s |", array[index], argv[index + 2]);
  147. fprintf(arch, "\n");
  148. }
  149.  
  150. static void input_files(FILE* const arch, const int argc, char const *argv[]) {
  151. auto FILE *file;
  152. auto int c;
  153. auto unsigned char index;
  154. for (index = 2; index < argc; index++) {
  155. file = fopen(argv[index], "rb");
  156. if (file == NULL) continue;
  157. while ((c = getc(file)) != EOF)
  158. putc(c, arch);
  159. fclose(file);
  160. printf("%s", argv[index]);
  161. remove(argv[index]);
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement