用C++写一个安装程序(贰)

解压缩可以用一些库,我用了minizip库,我知道大家不喜欢废话,所以

```cpp

#include <Windows.h>
#include <iostream>
#include <fstream>
#include <zip.h>

#pragma comment(lib,"libzip.lib")

void extractArchive(const char* archivePath, const char* outputPath) {
    // 打开压缩包
    zip* archive = zip_open(archivePath, 0, NULL);

    if (archive) {
        // 获取压缩包中的条目数
        int numEntries = zip_get_num_entries(archive, 0);

        // 遍历压缩包中的每个条目并解压缩
        for (int i = 0; i < numEntries; i++) {
            const char* entryName = zip_get_name(archive, i, 0);
            //extractEntry(archive, entryName, outputPath);

            // 获取指定条目在压缩包中的索引
            int entryIndex = zip_name_locate(archive, entryName, 0);
            if (entryIndex >= 0) {
                // 获取指定条目的信息
                struct zip_stat entryStat;
                zip_stat_init(&entryStat);
                zip_stat_index(archive, entryIndex, 0, &entryStat);

                // 判断条目类型
                if (entryStat.name[strlen(entryStat.name) - 1] == '/') {
                    // 如果是文件夹,则创建对应的目录
                    std::string folderPath = outputPath;
                    folderPath += "/";
                    folderPath += entryName;

                    WCHAR wszClassName[256];
                    memset(wszClassName, 0, sizeof(wszClassName));
                    MultiByteToWideChar(CP_ACP, 0, folderPath.c_str(), strlen(folderPath.c_str()) + 1, wszClassName, sizeof(wszClassName) / sizeof(wszClassName[0]));

                    CreateDirectory(wszClassName,NULL);
                }
                else {
                    // 如果是文件,则解压缩该文件
                    zip_file* file = zip_fopen_index(archive, entryIndex, 0);
                    if (file) {
                        // 创建输出文件流
                        std::string filePath = outputPath;
                        filePath += "/";
                        filePath += entryName;
                        std::ofstream outputFile(filePath, std::ios::binary);

                        if (outputFile) {
                            // 创建缓冲区来存储文件内容
                            unsigned char* buffer = new unsigned char[entryStat.size];

                            // 读取文件内容到缓冲区
                            zip_fread(file, buffer, entryStat.size);

                            // 将缓冲区内容写入输出文件流
                            outputFile.write(reinterpret_cast<const char*>(buffer), entryStat.size);
                            outputFile.close();

                            // 删除缓冲区内存
                            delete[] buffer;
                        }

                        // 关闭文件流
                        zip_fclose(file);
                    }
                }
            }
        }

        // 关闭压缩包
        zip_close(archive);
    }
}

```

点赞过5速更下一期

下一期:修改注册表

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值