Created
October 20, 2020 11:43
-
-
Save pczajkowski/73d577ac96699a91b81d2789a2e5f450 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#define _GNU_SOURCE //asks stdio.h to include asprintf | |
#include <archive.h> | |
#include <archive_entry.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include "stopif.h" | |
char* printFileSize(long int size){ | |
char *fileSize; | |
if (size > 1073741824) | |
asprintf(&fileSize, "%.2f GB", size/1073741824.0); | |
else if (size > 1048576) | |
asprintf(&fileSize, "%.2f MB", size/1048576.0); | |
else if (size > 1024) | |
asprintf(&fileSize, "%.2f KB", size/1024.0); | |
else | |
asprintf(&fileSize, "%.2f B", size/1.0); | |
return fileSize; | |
} | |
int readArchive(char const *infile, int details) { | |
struct archive *a; | |
struct archive_entry *entry; | |
a = archive_read_new(); | |
archive_read_support_filter_all(a); | |
archive_read_support_format_all(a); | |
Stopif(archive_read_open_filename(a, infile, 10240) != ARCHIVE_OK, return -1, "Can't read archive %s\n", infile); | |
if (!details) printf("Calculating...\n"); | |
size_t totalSize = 0; | |
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { | |
size_t size = archive_entry_size(entry); | |
if (size > 0 && details) { | |
char *fileSize = printFileSize(size); | |
printf("%s (%s)\n",archive_entry_pathname(entry), fileSize); | |
free(fileSize); | |
} | |
archive_read_data_skip(a); | |
totalSize += size; | |
} | |
Stopif(archive_read_free(a) != ARCHIVE_OK, return -2, "Can't close archive %s\n", infile); | |
char *totalPrint = printFileSize(totalSize); | |
printf("Total size after unpacking: %s\n", totalPrint); | |
free(totalPrint); | |
return 0; | |
} | |
int main(int argc, char **argv) { | |
if (argc < 2) | |
printf("Usage: %s <path_to_archive>\nAdd d after path to display details.\n", argv[0]); | |
if (argc > 2 && strcmp(argv[2], "d") == 0) | |
readArchive(argv[1], 1); | |
else if (argc > 1) | |
readArchive(argv[1], 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment