windows上如何解压tar包,c++
时间: 2023-11-07 08:19:09 浏览: 51
在Windows上解压tar包可以使用第三方库,例如libarchive和zlib。以下是基于libarchive的解压示例代码:
```cpp
#include <archive.h>
#include <archive_entry.h>
int extract_tar(const char* tar_path, const char* dest_path) {
struct archive* a = archive_read_new();
archive_read_support_filter_all(a);
archive_read_support_format_tar(a);
int r = archive_read_open_filename(a, tar_path, 10240); // 10240 is the read buffer size
if (r != ARCHIVE_OK) {
return r;
}
struct archive_entry* entry;
while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
const char* entry_pathname = archive_entry_pathname(entry);
std::string dest_file_path = dest_path;
dest_file_path += "/";
dest_file_path += entry_pathname;
if (archive_entry_filetype(entry) == AE_IFREG) {
FILE* dest_file = fopen(dest_file_path.c_str(), "wb");
if (dest_file == nullptr) {
return -1;
}
const void* buff;
size_t size;
off_t offset;
while (archive_read_data_block(a, &buff, &size, &offset) == ARCHIVE_OK) {
fwrite(buff, size, 1, dest_file);
}
fclose(dest_file);
}
archive_entry_clear(entry);
}
archive_read_close(a);
archive_read_free(a);
return 0;
}
```
其中,tar_path是tar包的路径,dest_path是解压到的目标文件夹路径。这个函数会将tar包中的所有文件解压到目标文件夹中。
相关推荐
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)