-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
75 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <archive.h> | ||
#include <archive_entry.h> | ||
struct archive *a; | ||
struct archive *ext; | ||
struct archive_entry *entry; | ||
int r; | ||
|
||
int extract_archive_all(const char *archive_path, const char *extract_path) { | ||
|
||
/* Open the archive file */ | ||
a = archive_read_new(); | ||
archive_read_support_format_all(a); | ||
archive_read_support_filter_all(a); | ||
|
||
if ((r = archive_read_open_filename(a, archive_path, 10240))) { | ||
return r; | ||
} | ||
|
||
/* Create the extraction archive */ | ||
ext = archive_write_disk_new(); | ||
archive_write_disk_set_options(ext, ARCHIVE_EXTRACT_TIME); | ||
archive_write_disk_set_standard_lookup(ext); | ||
|
||
/* Extract each entry in the archive */ | ||
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { | ||
const char *entry_path = archive_entry_pathname(entry); | ||
archive_entry_set_pathname(entry, entry_path); /* Ensure path consistency */ | ||
|
||
/* Set the destination path for extraction */ | ||
char dest_path[256]; | ||
snprintf(dest_path, sizeof(dest_path), "%s/%s", extract_path, entry_path); | ||
archive_entry_set_pathname(entry, dest_path); | ||
|
||
/* Extract the entry */ | ||
archive_write_header(ext, entry); | ||
|
||
/* Open the file for writing */ | ||
FILE *dest_file = fopen(dest_path, "wb"); | ||
if (!dest_file) { | ||
fprintf(stderr, "Failed to open destination file: %s\n", dest_path); | ||
return 1; | ||
} | ||
|
||
/* Read and write data to the file */ | ||
char buffer[4096]; | ||
size_t len; | ||
while ((r = archive_read_data(a, buffer, sizeof(buffer))) > 0) { | ||
fwrite(buffer, 1, r, dest_file); | ||
} | ||
|
||
fclose(dest_file); | ||
|
||
archive_write_finish_entry(ext); | ||
} | ||
|
||
/* Close and free resources */ | ||
archive_read_close(a); | ||
archive_read_free(a); | ||
archive_write_close(ext); | ||
archive_write_free(ext); | ||
|
||
return 0; | ||
} | ||
|
||
int extract_archive_file(const char *archive_path, const char *extract_path, const char* file_path) { | ||
|
||
/* Open the archive file */ | ||
a = archive_read_new(); | ||
archive_read_support_format_all(a); | ||
archive_read_support_filter_all(a); | ||
|
||
if ((r = archive_read_open_filename(a, archive_path, 10240))) { | ||
return r; | ||
} | ||
|
||
/* Create the extraction archive */ | ||
ext = archive_write_disk_new(); | ||
archive_write_disk_set_options(ext, ARCHIVE_EXTRACT_TIME); | ||
archive_write_disk_set_standard_lookup(ext); | ||
|
||
/* Extract each entry in the archive */ | ||
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { | ||
const char *entry_path = archive_entry_pathname(entry); | ||
if (entry_path != file_path) | ||
continue; | ||
archive_entry_set_pathname(entry, entry_path); /* Ensure path consistency */ | ||
|
||
/* Set the destination path for extraction */ | ||
char dest_path[256]; | ||
snprintf(dest_path, sizeof(dest_path), "%s/%s", extract_path, entry_path); | ||
archive_entry_set_pathname(entry, dest_path); | ||
|
||
/* Extract the entry */ | ||
archive_write_header(ext, entry); | ||
|
||
/* Open the file for writing */ | ||
FILE *dest_file = fopen(dest_path, "wb"); | ||
if (!dest_file) { | ||
fprintf(stderr, "Failed to open destination file: %s\n", dest_path); | ||
return 1; | ||
} | ||
|
||
/* Read and write data to the file */ | ||
char buffer[4096]; | ||
size_t len; | ||
while ((r = archive_read_data(a, buffer, sizeof(buffer))) > 0) { | ||
fwrite(buffer, 1, r, dest_file); | ||
} | ||
|
||
fclose(dest_file); | ||
|
||
archive_write_finish_entry(ext); | ||
} | ||
|
||
/* Close and free resources */ | ||
archive_read_close(a); | ||
archive_read_free(a); | ||
archive_write_close(ext); | ||
archive_write_free(ext); | ||
|
||
return 0; | ||
} |
This file contains 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
This file contains 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